Skip to content

Commit

Permalink
Simplify DynArray implementation using derive_more
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Jul 14, 2023
1 parent 1e6c732 commit dc6dfd4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
4 changes: 1 addition & 3 deletions bff-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ enum Commands {
directory: PathBuf,
},
#[clap(alias = "t")]
Info {
bigfile: PathBuf,
},
Info { bigfile: PathBuf },
Crc32 {
string: Option<String>,
#[arg(
Expand Down
42 changes: 6 additions & 36 deletions bff/src/dynarray.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::convert::TryFrom;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::Deref;

use binrw::{binread, BinRead};
use serde::{Serialize, Serializer};
use derive_more::Deref;
use serde::Serialize;

#[binread]
#[derive(Debug)]
#[derive(Debug, Serialize, Deref)]
#[serde(transparent)]
pub struct DynArray<InnerType, SizeType = u32>
where
// This code is ugly but the pretty syntax isn't stable yet
Expand All @@ -21,40 +22,9 @@ where
{
#[br(temp)]
size: SizeType,
#[deref]
#[br(count = size)]
data: Vec<InnerType>,
#[serde(skip)]
_phantom: PhantomData<SizeType>,
}

impl<InnerType, SizeType> Serialize for DynArray<InnerType, SizeType>
where
for<'a> InnerType: BinRead + Serialize + 'a,
for<'a> <InnerType as BinRead>::Args<'a>: Clone + Default,

SizeType: BinRead + Debug + Copy,
for<'a> <SizeType as BinRead>::Args<'a>: Default,
usize: TryFrom<SizeType>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.data.serialize(serializer)
}
}

impl<InnerType, SizeType> Deref for DynArray<InnerType, SizeType>
where
for<'a> InnerType: BinRead + Serialize + 'a,
for<'a> <InnerType as BinRead>::Args<'a>: Clone + Default,

SizeType: BinRead + Debug + Copy,
for<'a> <SizeType as BinRead>::Args<'a>: Default,
usize: TryFrom<SizeType>,
{
type Target = Vec<InnerType>;

fn deref(&self) -> &Self::Target {
&self.data
}
}

0 comments on commit dc6dfd4

Please sign in to comment.