Skip to content

Commit

Permalink
Simplify trait bounds for generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatNintendoNerd committed Dec 31, 2024
1 parent 3b71f28 commit 343b5cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
13 changes: 3 additions & 10 deletions lvd_lib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This module contains the [`LvdArray`] type.
use binrw::{binrw, BinRead, BinWrite};
use binrw::binrw;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand All @@ -14,12 +14,7 @@ use crate::version::{Version, Versioned};
#[br(import(version: u8))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub enum LvdArray<T: 'static>
where
T: BinRead + BinWrite + Version,
T: for<'a> BinRead<Args<'a> = (u8,)>,
T: for<'a> BinWrite<Args<'a> = ()>,
{
pub enum LvdArray<T: Version + 'static> {
/// `LvdArray` version 1.
#[br(pre_assert(version == 1))]
V1 {
Expand All @@ -35,9 +30,7 @@ where

impl<T> Version for LvdArray<T>
where
T: BinRead + BinWrite + Version,
T: for<'a> BinRead<Args<'a> = (u8,)>,
T: for<'a> BinWrite<Args<'a> = ()>,
T: Version,
{
fn version(&self) -> u8 {
match self {
Expand Down
14 changes: 7 additions & 7 deletions lvd_lib/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[derive(Debug)]
pub struct Versioned<T>
where
T: BinRead + BinWrite + Version,
T: for<'a> BinRead<Args<'a> = (u8,)>,
T: for<'a> BinWrite<Args<'a> = ()>,
{
pub struct Versioned<T: Version> {
/// The version number of the wrapped value.
#[br(temp)]
#[bw(calc = inner.version())]
Expand All @@ -27,7 +22,12 @@ where
}

/// A trait for determining a type's version.
pub trait Version {
pub trait Version
where
Self: BinRead + BinWrite,
Self: for<'a> BinRead<Args<'a> = (u8,)>,
Self: for<'a> BinWrite<Args<'a> = ()>,
{
/// Returns the version number from `self`.
fn version(&self) -> u8;
}

0 comments on commit 343b5cc

Please sign in to comment.