From ce32ad7bb77a756dae77a71cba04b79344b0ac41 Mon Sep 17 00:00:00 2001 From: wcampbell Date: Wed, 27 Dec 2023 22:56:18 -0500 Subject: [PATCH] Make DekuContainerWrite a default impl - Reduce codegen by making this a default impl --- deku-derive/src/macros/deku_write.rs | 21 +-------------------- src/lib.rs | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/deku-derive/src/macros/deku_write.rs b/deku-derive/src/macros/deku_write.rs index 138ff51c..386d370f 100644 --- a/deku-derive/src/macros/deku_write.rs +++ b/deku-derive/src/macros/deku_write.rs @@ -268,26 +268,7 @@ fn emit_enum(input: &DekuData) -> Result { } } - impl #imp DekuContainerWrite for #ident #wher { - fn to_bytes(&self) -> core::result::Result, ::#crate_::DekuError> { - let mut out_buf = vec![]; - let mut __deku_writer = ::#crate_::writer::Writer::new(&mut out_buf); - ::#crate_::DekuWriter::to_writer(self, &mut __deku_writer, ())?; - __deku_writer.finalize()?; - Ok(out_buf) - } - - #[allow(unused_variables)] - fn to_bits(&self) -> core::result::Result<::#crate_::bitvec::BitVec, ::#crate_::DekuError> { - let mut out_buf = vec![]; - let mut __deku_writer = ::#crate_::writer::Writer::new(&mut out_buf); - ::#crate_::DekuWriter::to_writer(self, &mut __deku_writer, ())?; - let mut leftover = __deku_writer.leftover; - let mut bv = ::#crate_::bitvec::BitVec::from_slice(&out_buf); - bv.append(&mut leftover); - Ok(bv) - } - } + impl #imp DekuContainerWrite for #ident #wher {} }) } diff --git a/src/lib.rs b/src/lib.rs index 0146ce27..739ca361 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -472,7 +472,13 @@ pub trait DekuContainerWrite: DekuWriter<()> { /// let bytes = s.to_bytes().unwrap(); /// assert_eq!(bytes, [0x01, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00]); /// ```` - fn to_bytes(&self) -> Result, DekuError>; + fn to_bytes(&self) -> Result, DekuError> { + let mut out_buf = Vec::new(); + let mut __deku_writer = Writer::new(&mut out_buf); + DekuWriter::to_writer(self, &mut __deku_writer, ())?; + __deku_writer.finalize()?; + Ok(out_buf) + } /// Write struct/enum to BitVec /// @@ -495,7 +501,15 @@ pub trait DekuContainerWrite: DekuWriter<()> { /// let bits = test.to_bits().unwrap(); /// assert_eq!(deku::bitvec::bitvec![1, 1, 1, 1, 0, 0, 0, 1, 1], bits); /// ``` - fn to_bits(&self) -> Result, DekuError>; + fn to_bits(&self) -> Result, DekuError> { + let mut out_buf = Vec::new(); + let mut __deku_writer = Writer::new(&mut out_buf); + DekuWriter::to_writer(self, &mut __deku_writer, ())?; + let mut leftover = __deku_writer.leftover; + let mut bv = bitvec::BitVec::from_slice(&out_buf); + bv.append(&mut leftover); + Ok(bv) + } } /// "Updater" trait: apply mutations to a type