Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement BinRead for all array lengths using const generics. #41

Merged
merged 5 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions binread/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ description = "A Rust crate for helping read structs from binary data using ✨m
readme = "../README.md"
documentation = "https://docs.rs/binread"

[[test]]
name = "const_generic"
required-features = ["const_generics"]

[dependencies]
array-init = { version = "1.1.0", optional = true }
binread_derive = { version = "2.0.0", path = "../binread_derive" }
lazy_static = { version = "1.4", optional = true }

Expand All @@ -19,6 +24,7 @@ rustversion = "1.0"
trybuild = "1.0"

[features]
const_generics = ["array-init"]
default = ["std"]
std = []
debug_template = ["std", "lazy_static", "binread_derive/debug_template"]
45 changes: 45 additions & 0 deletions binread/src/binread_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl<C: Copy + 'static, B: BinRead<Args = C>> BinRead for Vec<B> {
}
}

#[cfg(not(feature = "const_generics"))]
macro_rules! binread_array_impl {
($($size:literal),*$(,)?) => {
$(
Expand Down Expand Up @@ -165,8 +166,52 @@ macro_rules! binread_array_impl {
}
}

#[cfg(not(feature = "const_generics"))]
binread_array_impl!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32);

#[cfg(feature = "const_generics")]
impl<C: Copy + 'static, B: BinRead<Args = C>, const N: usize> BinRead for [B; N] {
type Args = B::Args;

fn read_options<R: Read + Seek>(
reader: &mut R,
options: &ReadOptions,
args: Self::Args,
) -> BinResult<Self> {
#[cfg(feature = "debug_template")]
{
let pos = reader.seek(SeekFrom::Current(0))?;
let type_name = core::any::type_name::<B>().rsplitn(1, "::").nth(0).unwrap();

if let Some(name) = options.variable_name {
binary_template::write_vec_named(options.endian, pos, type_name, N, name);
} else {
binary_template::write_vec(options.endian, pos, type_name, N);
}
}

#[cfg(feature = "debug_template")]
let options = &ReadOptions {
dont_output_to_template: true,
..*options
};

let arr = array_init::try_array_init(|_| BinRead::read_options(reader, options, args))?;
Ok(arr)
}

fn after_parse<R>(&mut self, reader: &mut R, ro: &ReadOptions, args: B::Args) -> BinResult<()>
where
R: Read + Seek,
{
for val in self.iter_mut() {
val.after_parse(reader, ro, args)?;
}

Ok(())
}
}

/// Internal macro to recursively implement BinRead for every size tuple given
/// in the invocation
macro_rules! binread_tuple_impl {
Expand Down
11 changes: 11 additions & 0 deletions binread/tests/const_generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use binread::{BinRead, BinReaderExt, io::Cursor};

#[test]
fn const_generic_test() {
const IN: [u8; 300] = [6; 300];

let mut reader = Cursor::new(&IN);
let out: [u8; 300] = reader.read_be().unwrap();

assert_eq!(IN, out);
}