diff --git a/examples/many.rs b/examples/many.rs new file mode 100644 index 00000000..88f96490 --- /dev/null +++ b/examples/many.rs @@ -0,0 +1,30 @@ +use deku::{bitvec::BitView, container, ctx::Limit, prelude::*, DekuRead, DekuWrite}; +use std::io::Write; + +#[derive(Debug, DekuRead, DekuWrite)] +struct Test { + pub a: u64, + pub b: u64, + pub c: u64, +} + +fn main() { + let input: Vec<_> = (0..10_0000) + .map(|i| Test { + a: i, + b: i + 1, + c: i + 2, + }) + .collect(); + let custom: Vec = input + .iter() + .flat_map(|x| x.to_bytes().unwrap().into_iter()) + .collect(); + let mut container = Container::new(std::io::Cursor::new(custom.clone())); + let ret = as DekuRead>>::from_reader( + &mut container, + Limit::new_count(10_0000), + ); + + println!("{:?}", ret); +} diff --git a/src/impls/vec.rs b/src/impls/vec.rs index fe5289ce..4bae4630 100644 --- a/src/impls/vec.rs +++ b/src/impls/vec.rs @@ -1,3 +1,5 @@ +use std::io::Read; + #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -148,19 +150,6 @@ where } } - /// Read `T`s until the given limit - /// * `limit` - the limiting factor on the amount of `T`s to read - /// * `inner_ctx` - The context required by `T`. It will be passed to every `T`s when constructing. - /// # Examples - /// ```rust - /// # use deku::ctx::*; - /// # use deku::DekuRead; - /// # use deku::bitvec::BitView; - /// let input = vec![1u8, 2, 3, 4]; - /// let (amt_read, v) = Vec::::read(input.view_bits(), (1.into(), Endian::Little)).unwrap(); - /// assert_eq!(amt_read, 32); - /// assert_eq!(vec![0x04030201], v) - /// ``` fn from_reader( container: &mut crate::container::Container, (limit, inner_ctx): (Limit, Ctx), @@ -222,6 +211,17 @@ impl<'a, T: DekuRead<'a>, Predicate: FnMut(&T) -> bool> DekuRead<'a, Limit( + container: &mut crate::container::Container, + limit: Limit, + ) -> Result + where + Self: Sized, + { + Vec::from_reader(container, (limit, ())) + } } impl, Ctx: Copy> DekuWrite for Vec {