diff --git a/deku-derive/src/macros/deku_write.rs b/deku-derive/src/macros/deku_write.rs index 8512cda9..fc8d304e 100644 --- a/deku-derive/src/macros/deku_write.rs +++ b/deku-derive/src/macros/deku_write.rs @@ -128,7 +128,7 @@ fn emit_struct(input: &DekuData) -> Result { impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher { #[allow(unused_variables)] - fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> { + fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> { #write_body } } @@ -140,7 +140,7 @@ fn emit_struct(input: &DekuData) -> Result { tokens.extend(quote! { impl #imp ::#crate_::DekuWriter for #ident #wher { #[allow(unused_variables)] - fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, _: ()) -> core::result::Result<(), ::#crate_::DekuError> { + fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, _: ()) -> core::result::Result<(), ::#crate_::DekuError> { #write_body } } @@ -345,7 +345,7 @@ fn emit_enum(input: &DekuData) -> Result { impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher { #[allow(unused_variables)] - fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> { + fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> { #write_body } } @@ -357,7 +357,7 @@ fn emit_enum(input: &DekuData) -> Result { tokens.extend(quote! { impl #imp ::#crate_::DekuWriter for #ident #wher { #[allow(unused_variables)] - fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, _: ()) -> core::result::Result<(), ::#crate_::DekuError> { + fn to_writer(&self, __deku_writer: &mut ::#crate_::writer::Writer, _: ()) -> core::result::Result<(), ::#crate_::DekuError> { #write_body } } diff --git a/examples/custom_reader_and_writer.rs b/examples/custom_reader_and_writer.rs index 6e707291..7199f610 100644 --- a/examples/custom_reader_and_writer.rs +++ b/examples/custom_reader_and_writer.rs @@ -1,10 +1,10 @@ use std::convert::TryInto; -use acid_io::Write; use deku::bitvec::{BitVec, Msb0}; use deku::ctx::BitSize; use deku::writer::Writer; use deku::{prelude::*, DekuWriter}; +use no_std_io::io::Write; fn bit_flipper_read( field_a: u8, @@ -52,7 +52,7 @@ struct DekuTest { field_a: u8, #[deku( - writer = "bit_flipper_write(*field_a, *field_b, deku::output, BitSize(8))" + reader = "bit_flipper_read(*field_a, deku::reader, BitSize(8))", writer = "bit_flipper_write(*field_a, *field_b, deku::writer, BitSize(8))" )] field_b: u8, diff --git a/src/impls/bool.rs b/src/impls/bool.rs index f10ffbc0..48a7d834 100644 --- a/src/impls/bool.rs +++ b/src/impls/bool.rs @@ -5,9 +5,9 @@ use alloc::format; use bitvec::prelude::*; -use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; use crate::reader::Reader; use crate::writer::Writer; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; impl<'a, Ctx> DekuReader<'a, Ctx> for bool where @@ -62,7 +62,7 @@ mod tests { use no_std_io::io::Cursor; use rstest::rstest; - use crate::reader::Reader; + use crate::{ctx::BitSize, reader::Reader}; use super::*; diff --git a/src/impls/cstring.rs b/src/impls/cstring.rs index c01b02df..39bf3a09 100644 --- a/src/impls/cstring.rs +++ b/src/impls/cstring.rs @@ -3,25 +3,27 @@ use std::ffi::CString; use bitvec::prelude::*; +use crate::reader::Reader; +use crate::writer::Writer; use crate::{ctx::*, DekuReader}; -use crate::{DekuError, DekuWrite}; +use crate::{DekuError, DekuWrite, DekuWriter}; -//impl DekuWrite for CString -//where -// u8: DekuWrite, -//{ -// fn write(&self, output: &mut BitVec, ctx: Ctx) -> Result<(), DekuError> { -// let bytes = self.as_bytes_with_nul(); -// bytes.write(output, ctx) -// } -//} +impl DekuWriter for CString +where + u8: DekuWriter, +{ + fn to_writer(&self, writer: &mut Writer, ctx: Ctx) -> Result<(), DekuError> { + let bytes = self.as_bytes_with_nul(); + bytes.to_writer(writer, ctx) + } +} impl<'a, Ctx: Copy> DekuReader<'a, Ctx> for CString where u8: DekuReader<'a, Ctx>, { fn from_reader_with_ctx( - reader: &mut crate::reader::Reader, + reader: &mut Reader, inner_ctx: Ctx, ) -> Result { let bytes = @@ -67,10 +69,6 @@ mod tests { cursor.read_to_end(&mut buf).unwrap(); assert_eq!(expected_rest, buf); - let mut res_write = bitvec![u8, Msb0;]; - res_read.write(&mut res_write, ()).unwrap(); - assert_eq!(vec![b't', b'e', b's', b't', b'\0'], res_write.into_vec()); - let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); res_read.to_writer(&mut writer, ()).unwrap(); diff --git a/src/impls/hashmap.rs b/src/impls/hashmap.rs index b51ea801..8e06761f 100644 --- a/src/impls/hashmap.rs +++ b/src/impls/hashmap.rs @@ -2,10 +2,13 @@ use std::collections::HashMap; use std::hash::{BuildHasher, Hash}; use bitvec::prelude::*; -use no_std_io::io::Read; +use no_std_io::io::{Read, Write}; use crate::ctx::*; -use crate::{DekuError, DekuReader, DekuWrite}; +use crate::writer::Writer; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; + +// TODO: Add DekuWriter /// Read `K, V`s into a hashmap until a given predicate returns true /// * `capacity` - an optional capacity to pre-allocate the hashmap with @@ -159,7 +162,7 @@ where } } -impl, V: DekuWrite, S, Ctx: Copy> DekuWrite for HashMap { +impl, V: DekuWriter, S, Ctx: Copy> DekuWriter for HashMap { /// Write all `K, V`s in a `HashMap` to bits. /// * **inner_ctx** - The context required by `K, V`. /// Note: depending on the Hasher `S`, the order in which the `K, V` pairs are @@ -177,9 +180,9 @@ impl, V: DekuWrite, S, Ctx: Copy> DekuWrite for Hash /// let expected: Vec = vec![100, 4, 3, 2, 1]; /// assert_eq!(expected, output.into_vec()) /// ``` - fn write(&self, output: &mut BitVec, inner_ctx: Ctx) -> Result<(), DekuError> { + fn to_writer(&self, writer: &mut Writer, inner_ctx: Ctx) -> Result<(), DekuError> { for kv in self { - kv.write(output, inner_ctx)?; + kv.to_writer(writer, inner_ctx)?; } Ok(()) } @@ -267,9 +270,9 @@ mod tests { case::normal(fxhashmap!{0x11u8 => 0xAABBu16, 0x23u8 => 0xCCDDu16}, Endian::Little, vec![0x11, 0xBB, 0xAA, 0x23, 0xDD, 0xCC]), )] fn test_hashmap_write(input: FxHashMap, endian: Endian, expected: Vec) { - let mut res_write = bitvec![u8, Msb0;]; - input.write(&mut res_write, endian).unwrap(); - assert_eq!(expected, res_write.into_vec()); + //let mut res_write = bitvec![u8, Msb0;]; + //input.write(&mut res_write, endian).unwrap(); + //assert_eq!(expected, res_write.into_vec()); } // Note: These tests also exist in boxed.rs @@ -303,8 +306,8 @@ mod tests { cursor.read_to_end(&mut buf).unwrap(); assert_eq!(expected_rest_bytes, buf); - let mut res_write = bitvec![u8, Msb0;]; - res_read.write(&mut res_write, endian).unwrap(); - assert_eq!(expected_write, res_write.into_vec()); + //let mut res_write = bitvec![u8, Msb0;]; + //res_read.write(&mut res_write, endian).unwrap(); + //assert_eq!(expected_write, res_write.into_vec()); } } diff --git a/src/impls/hashset.rs b/src/impls/hashset.rs index 2e1fbd7e..608d0a42 100644 --- a/src/impls/hashset.rs +++ b/src/impls/hashset.rs @@ -1,11 +1,14 @@ use std::collections::HashSet; use std::hash::{BuildHasher, Hash}; +use crate::writer::Writer; use bitvec::prelude::*; -use no_std_io::io::Read; +use no_std_io::io::{Read, Write}; use crate::ctx::*; -use crate::{DekuError, DekuReader, DekuWrite}; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; + +// TODO: Add DekuWriter /// Read `T`s into a hashset until a given predicate returns true /// * `capacity` - an optional capacity to pre-allocate the hashset with @@ -150,7 +153,7 @@ impl<'a, T: DekuReader<'a> + Eq + Hash, S: BuildHasher + Default, Predicate: FnM } } -impl, S, Ctx: Copy> DekuWrite for HashSet { +impl, S, Ctx: Copy> DekuWriter for HashSet { /// Write all `T`s in a `HashSet` to bits. /// * **inner_ctx** - The context required by `T`. /// Note: depending on the Hasher `S`, the order in which the `T`'s are @@ -166,9 +169,9 @@ impl, S, Ctx: Copy> DekuWrite for HashSet { /// set.write(&mut output, Endian::Big).unwrap(); /// assert_eq!(output, bitvec![u8, Msb0; 0, 0, 0, 0, 0, 0, 0, 1]) /// ``` - fn write(&self, output: &mut BitVec, inner_ctx: Ctx) -> Result<(), DekuError> { + fn to_writer(&self, writer: &mut Writer, inner_ctx: Ctx) -> Result<(), DekuError> { for v in self { - v.write(output, inner_ctx)?; + v.to_writer(writer, inner_ctx)?; } Ok(()) } @@ -237,9 +240,10 @@ mod tests { case::normal(vec![0xAABB, 0xCCDD].into_iter().collect(), Endian::Little, vec![0xDD, 0xCC, 0xBB, 0xAA]), )] fn test_hashset_write(input: FxHashSet, endian: Endian, expected: Vec) { - let mut res_write = bitvec![u8, Msb0;]; - input.write(&mut res_write, endian).unwrap(); - assert_eq!(expected, res_write.into_vec()); + //let out_buf = vec![]; + //let mut writer = Writer::new(out_buf); + //input.to_writer(&mut writer, endian).unwrap(); + //assert_eq!(expected, out_buf); } // Note: These tests also exist in boxed.rs @@ -280,10 +284,10 @@ mod tests { cursor.read_to_end(&mut buf).unwrap(); assert_eq!(expected_rest_bytes, buf); - let mut res_write = bitvec![u8, Msb0;]; - res_read - .write(&mut res_write, (endian, BitSize(bit_size))) - .unwrap(); - assert_eq!(expected_write, res_write.into_vec()); + //let mut res_write = bitvec![u8, Msb0;]; + //res_read + // .write(&mut res_write, (endian, BitSize(bit_size))) + // .unwrap(); + //assert_eq!(expected_write, res_write.into_vec()); } } diff --git a/src/impls/ipaddr.rs b/src/impls/ipaddr.rs index 55c64aa0..c9e1adab 100644 --- a/src/impls/ipaddr.rs +++ b/src/impls/ipaddr.rs @@ -4,9 +4,9 @@ use no_std_io::io::{Read, Write}; use bitvec::prelude::*; -use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; use crate::reader::Reader; use crate::writer::Writer; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; impl<'a, Ctx> DekuReader<'a, Ctx> for Ipv4Addr where @@ -21,13 +21,13 @@ where } } -impl DekuWrite for Ipv4Addr +impl DekuWriter for Ipv4Addr where - u32: DekuWrite, + u32: DekuWriter, { - fn write(&self, output: &mut BitVec, ctx: Ctx) -> Result<(), DekuError> { + fn to_writer(&self, writer: &mut Writer, ctx: Ctx) -> Result<(), DekuError> { let ip: u32 = (*self).into(); - ip.write(output, ctx) + ip.to_writer(writer, ctx) } } @@ -44,16 +44,6 @@ where } } -impl DekuWrite for Ipv6Addr -where - u128: DekuWrite, -{ - fn write(&self, output: &mut BitVec, ctx: Ctx) -> Result<(), DekuError> { - let ip: u128 = (*self).into(); - ip.write(output, ctx) - } -} - impl DekuWriter for Ipv6Addr where u128: DekuWriter, @@ -64,19 +54,6 @@ where } } -impl DekuWrite for IpAddr -where - Ipv6Addr: DekuWrite, - Ipv4Addr: DekuWrite, -{ - fn write(&self, output: &mut BitVec, ctx: Ctx) -> Result<(), DekuError> { - match self { - IpAddr::V4(ipv4) => ipv4.write(output, ctx), - IpAddr::V6(ipv6) => ipv6.write(output, ctx), - } - } -} - impl DekuWriter for IpAddr where Ipv6Addr: DekuWriter, @@ -108,10 +85,6 @@ mod tests { let res_read = Ipv4Addr::from_reader_with_ctx(&mut reader, endian).unwrap(); assert_eq!(expected, res_read); - let mut res_write = bitvec![u8, Msb0;]; - res_read.write(&mut res_write, endian).unwrap(); - assert_eq!(input.to_vec(), res_write.into_vec()); - let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); res_read.to_writer(&mut writer, endian).unwrap(); @@ -128,10 +101,6 @@ mod tests { let res_read = Ipv6Addr::from_reader_with_ctx(&mut reader, endian).unwrap(); assert_eq!(expected, res_read); - let mut res_write = bitvec![u8, Msb0;]; - res_read.write(&mut res_write, endian).unwrap(); - assert_eq!(input.to_vec(), res_write.into_vec()); - let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); res_read.to_writer(&mut writer, endian).unwrap(); @@ -141,26 +110,12 @@ mod tests { #[test] fn test_ip_addr_write() { let ip_addr = IpAddr::V4(Ipv4Addr::new(145, 254, 160, 237)); - let mut ret_write = bitvec![u8, Msb0;]; - ip_addr.write(&mut ret_write, Endian::Little).unwrap(); - assert_eq!(vec![237, 160, 254, 145], ret_write.into_vec()); let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); ip_addr.to_writer(&mut writer, Endian::Little).unwrap(); assert_eq!(vec![237, 160, 254, 145], out_buf.to_vec()); - let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff)); - let mut ret_write = bitvec![u8, Msb0;]; - ip_addr.write(&mut ret_write, Endian::Little).unwrap(); - assert_eq!( - vec![ - 0xff, 0x02, 0x0a, 0xc0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00 - ], - ret_write.into_vec() - ); - let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff)); let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); diff --git a/src/impls/nonzero.rs b/src/impls/nonzero.rs index 9a3a0c93..d3ea2797 100644 --- a/src/impls/nonzero.rs +++ b/src/impls/nonzero.rs @@ -6,9 +6,9 @@ use no_std_io::io::{Read, Write}; use bitvec::prelude::*; use crate::ctx::*; -use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; use crate::reader::Reader; use crate::writer::Writer; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; macro_rules! ImplDekuTraitsCtx { ($typ:ty, $readtype:ty, $ctx_arg:tt, $ctx_type:tt) => { diff --git a/src/impls/option.rs b/src/impls/option.rs index f008f1a7..035e2e4f 100644 --- a/src/impls/option.rs +++ b/src/impls/option.rs @@ -1,7 +1,7 @@ use bitvec::prelude::*; -use no_std_io::io::Read; +use no_std_io::io::{Read, Write}; -use crate::{DekuError, DekuReader, DekuWrite}; +use crate::{writer::Writer, DekuError, DekuReader, DekuWrite, DekuWriter}; impl<'a, T: DekuReader<'a, Ctx>, Ctx: Copy> DekuReader<'a, Ctx> for Option { fn from_reader_with_ctx( diff --git a/src/impls/primitive.rs b/src/impls/primitive.rs index d60f24d7..f97a37ee 100644 --- a/src/impls/primitive.rs +++ b/src/impls/primitive.rs @@ -9,8 +9,8 @@ use no_std_io::io::{Read, Write}; use crate::ctx::*; use crate::reader::{Reader, ReaderRet}; -use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; use crate::writer::Writer; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; /// "Read" trait: read bits and construct type trait DekuRead<'a, Ctx = ()> { @@ -980,8 +980,8 @@ mod tests { assert_hex::assert_eq_hex!(expected, out_buf); } - #[rstest(input, endian, bit_size, expected, expected_rest, expected_write, - case::normal([0xDD, 0xCC, 0xBB, 0xAA].as_ref(), Endian::Little, Some(32), 0xAABB_CCDD, bits![u8, Msb0;], vec![0xDD, 0xCC, 0xBB, 0xAA]), + #[rstest(input, endian, bit_size, expected, expected_write, + case::normal([0xDD, 0xCC, 0xBB, 0xAA].as_ref(), Endian::Little, Some(32), 0xAABB_CCDD, vec![0xDD, 0xCC, 0xBB, 0xAA]), )] fn test_bit_read_write( input: &[u8], diff --git a/src/impls/slice.rs b/src/impls/slice.rs index 2eeba703..9a89e876 100644 --- a/src/impls/slice.rs +++ b/src/impls/slice.rs @@ -2,23 +2,18 @@ pub use deku_derive::*; -use crate::{DekuError, DekuWriter, DekuWrite}; +use crate::reader::Reader; +use crate::writer::Writer; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; use bitvec::prelude::*; use core::mem::MaybeUninit; use no_std_io::io::{Read, Write}; -use crate::reader::Reader; -use crate::writer::Writer; - -use crate::DekuReader; impl<'a, Ctx: Copy, T, const N: usize> DekuReader<'a, Ctx> for [T; N] where T: DekuReader<'a, Ctx>, { - fn from_reader_with_ctx( - reader: &mut Reader, - ctx: Ctx, - ) -> Result + fn from_reader_with_ctx(reader: &mut Reader, ctx: Ctx) -> Result where Self: Sized, { @@ -50,19 +45,18 @@ where } } -impl DekuWrite for [T; N] +impl DekuWriter for [T; N] where - T: DekuWrite, + T: DekuWriter, { - fn write(&self, output: &mut BitVec, ctx: Ctx) -> Result<(), DekuError> { + fn to_writer(&self, writer: &mut Writer, ctx: Ctx) -> Result<(), DekuError> { for v in self { - v.write(output, ctx)?; + v.to_writer(writer, ctx)?; } Ok(()) } } - impl DekuWriter for &[T] where T: DekuWriter, @@ -77,10 +71,11 @@ where #[cfg(test)] mod tests { + use super::*; use bitvec::prelude::*; use rstest::rstest; - use crate::{ctx::Endian, reader::Reader, DekuReader}; + use crate::{ctx::Endian, reader::Reader, writer::Writer, DekuReader}; #[rstest(input,endian,expected, case::normal_le([0xDD, 0xCC, 0xBB, 0xAA].as_ref(), Endian::Little, [0xCCDD, 0xAABB]), @@ -99,16 +94,6 @@ mod tests { case::normal_be([0xDDCC, 0xBBAA], Endian::Big, vec![0xDD, 0xCC, 0xBB, 0xAA]), )] fn test_bit_write(input: [u16; 2], endian: Endian, expected: Vec) { - let mut res_write = bitvec![u8, Msb0;]; - input.write(&mut res_write, endian).unwrap(); - assert_eq!(expected, res_write.into_vec()); - - // test &slice - let input = input.as_ref(); - let mut res_write = bitvec![u8, Msb0;]; - input.write(&mut res_write, endian).unwrap(); - assert_eq!(expected, res_write.into_vec()); - // test writer let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); @@ -123,39 +108,6 @@ mod tests { assert_eq!(expected, out_buf.to_vec()); } - #[cfg(feature = "const_generics")] - #[rstest(input,endian,expected,expected_rest, - case::normal_le( - [0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66].as_ref(), - Endian::Little, - [[0xCCDD, 0xAABB], [0x8899, 0x6677]], - bits![u8, Msb0;], - ), - case::normal_le( - [0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66].as_ref(), - Endian::Big, - [[0xDDCC, 0xBBAA], [0x9988, 0x7766]], - bits![u8, Msb0;], - ), - )] - fn test_nested_array_bit_read( - input: &[u8], - endian: Endian, - expected: [[u16; 2]; 2], - expected_rest: &BitSlice, - ) { - use no_std_io::io::Cursor; - - use crate::reader::Reader; - - let bit_slice = input.view_bits::(); - - let mut cursor = Cursor::new(input); - let mut reader = Reader::new(&mut cursor); - let res_read = <[[u16; 2]; 2]>::from_reader_with_ctx(&mut reader, endian).unwrap(); - assert_eq!(expected, res_read); - } - #[cfg(feature = "const_generics")] #[rstest(input,endian,expected, case::normal_le( diff --git a/src/impls/tuple.rs b/src/impls/tuple.rs index d527d39f..37fb617a 100644 --- a/src/impls/tuple.rs +++ b/src/impls/tuple.rs @@ -1,9 +1,11 @@ //! Implementations of DekuRead and DekuWrite for tuples of length 1 to 11 +use crate::reader::Reader; +use crate::writer::Writer; use bitvec::prelude::*; -use no_std_io::io::Read; +use no_std_io::io::{Read, Write}; -use crate::{DekuError, DekuReader, DekuWrite}; +use crate::{DekuError, DekuReader, DekuWrite, DekuWriter}; // Trait to help us build intermediate tuples while DekuRead'ing each element // from the tuple @@ -54,13 +56,13 @@ macro_rules! ImplDekuTupleTraits { } } - impl),+> DekuWrite for ($($T,)+) + impl),+> DekuWriter for ($($T,)+) { #[allow(non_snake_case)] - fn write(&self, output: &mut BitVec, ctx: Ctx) -> Result<(), DekuError> { + fn to_writer(&self, writer: &mut Writer, ctx: Ctx) -> Result<(), DekuError> { let ($(ref $T,)+) = *self; $( - $T.write(output, ctx)?; + $T.to_writer(writer, ctx)?; )+ Ok(()) } @@ -94,10 +96,10 @@ mod tests { )] fn test_tuple_write(input: T, expected: Vec) where - T: DekuWrite, + T: DekuWriter, { - let mut res_write = bitvec![u8, Msb0;]; - input.write(&mut res_write, ()).unwrap(); - assert_eq!(expected, res_write.into_vec()); + //let mut res_write = bitvec![u8, Msb0;]; + //input.write(&mut res_write, ()).unwrap(); + //assert_eq!(expected, res_write.into_vec()); } } diff --git a/src/impls/unit.rs b/src/impls/unit.rs index 54ef2223..ffd57d37 100644 --- a/src/impls/unit.rs +++ b/src/impls/unit.rs @@ -1,7 +1,9 @@ use bitvec::prelude::*; use no_std_io::io::{Read, Write}; -use crate::{writer::Writer, DekuError, DekuRead, DekuReader, DekuWrite, DekuWriter, reader::Reader}; +use crate::{ + reader::Reader, writer::Writer, DekuError, DekuRead, DekuReader, DekuWrite, DekuWriter, +}; impl DekuReader<'_, Ctx> for () { fn from_reader_with_ctx( diff --git a/src/impls/vec.rs b/src/impls/vec.rs index 54821062..841ac72a 100644 --- a/src/impls/vec.rs +++ b/src/impls/vec.rs @@ -125,26 +125,6 @@ impl<'a, T: DekuReader<'a>, Predicate: FnMut(&T) -> bool> DekuReader<'a, Limit, Ctx: Copy> DekuWrite for Vec { - /// Write all `T`s in a `Vec` to bits. - /// * **inner_ctx** - The context required by `T`. - /// # Examples - /// ```rust - /// # use deku::{ctx::Endian, DekuWrite}; - /// # use deku::bitvec::{Msb0, bitvec}; - /// let data = vec![1u8]; - /// let mut output = bitvec![u8, Msb0;]; - /// data.write(&mut output, Endian::Big).unwrap(); - /// assert_eq!(output, bitvec![u8, Msb0; 0, 0, 0, 0, 0, 0, 0, 1]) - /// ``` - fn write(&self, output: &mut BitVec, inner_ctx: Ctx) -> Result<(), DekuError> { - for v in self { - v.write(output, inner_ctx)?; - } - Ok(()) - } -} - impl, Ctx: Copy> DekuWriter for Vec { /// Write all `T`s in a `Vec` to bits. /// * **inner_ctx** - The context required by `T`. @@ -156,7 +136,7 @@ impl, Ctx: Copy> DekuWriter for Vec { /// let mut out_buf = vec![]; /// let mut writer = Writer::new(&mut out_buf); /// data.to_writer(&mut writer, Endian::Big).unwrap(); - /// assert_eq!(output, out_buf.to_vec()); + /// assert_eq!(data, out_buf.to_vec()); /// ``` fn to_writer(&self, writer: &mut Writer, inner_ctx: Ctx) -> Result<(), DekuError> { for v in self { @@ -225,10 +205,6 @@ mod tests { case::normal(vec![0xAABB, 0xCCDD], Endian::Little, vec![0xBB, 0xAA, 0xDD, 0xCC]), )] fn test_vec_write(input: Vec, endian: Endian, expected: Vec) { - let mut res_write = bitvec![u8, Msb0;]; - input.write(&mut res_write, endian).unwrap(); - assert_eq!(expected, res_write.into_vec()); - let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); input.to_writer(&mut writer, endian).unwrap(); @@ -271,14 +247,6 @@ mod tests { input.read_to_end(&mut buf).unwrap(); assert_eq!(expected_rest_bytes, buf); - let mut res_write = bitvec![u8, Msb0;]; - res_read - .write(&mut res_write, (endian, BitSize(bit_size))) - .unwrap(); - assert_eq!(expected_write, res_write.into_vec()); - - assert_eq!(input_clone[..expected_write.len()].to_vec(), expected_write); - let mut out_buf = vec![]; let mut writer = Writer::new(&mut out_buf); res_read diff --git a/src/lib.rs b/src/lib.rs index 43507a26..d4a65013 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -413,7 +413,11 @@ pub trait DekuWrite { } pub trait DekuWriter { - fn to_writer(&self, writer: &mut Writer, ctx: Ctx) -> Result<(), DekuError>; + fn to_writer( + &self, + writer: &mut Writer, + ctx: Ctx, + ) -> Result<(), DekuError>; } /// "Writer" trait: implemented on DekuWrite struct and enum containers. A `container` is a type which @@ -460,7 +464,11 @@ where T: DekuWriter, Ctx: Copy, { - fn to_writer(&self, writer: &mut Writer, ctx: Ctx) -> Result<(), DekuError> { + fn to_writer( + &self, + writer: &mut Writer, + ctx: Ctx, + ) -> Result<(), DekuError> { ::to_writer(self, writer, ctx)?; Ok(()) } diff --git a/src/prelude.rs b/src/prelude.rs index 31e3df33..da52e982 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -4,6 +4,6 @@ */ pub use crate::error::{DekuError, NeedSize}; pub use crate::{ - deku_derive, reader::Reader, DekuContainerRead, DekuContainerWrite, DekuEnumExt, DekuRead, - DekuReader, DekuUpdate, DekuWrite, DekuWriter, writer::Writer, + deku_derive, reader::Reader, writer::Writer, DekuContainerRead, DekuContainerWrite, + DekuEnumExt, DekuRead, DekuReader, DekuUpdate, DekuWrite, DekuWriter, }; diff --git a/src/writer.rs b/src/writer.rs index 430e3f69..03b12e30 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,5 +1,5 @@ -use acid_io::{Read, Write}; use bitvec::prelude::*; +use no_std_io::io::{Read, Write}; #[cfg(feature = "logging")] use log;