Repository: sharksforarms/deku
Pull request #432 (2024-04-30 02:16:30)

Update changelog


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2068fe0..7f38406 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,9 @@
 ## [Unreleased]
 
 ## Changes
+- Add DekuWrite impl for `[T]` ([#416](https://github.com/sharksforarms/deku/pull/416))
+- Add `no-assert-string` feature to remove panic string on failed assertion ([#405](https://github.com/sharksforarms/deku/pull/405))
+- Add `read_all` attribute to read until `reader.end()` ([#387](https://github.com/sharksforarms/deku/pull/387))
 - Added MSRV for `1.67.1` ([#390](https://github.com/sharksforarms/deku/pull/390))
 - Changed edition to 2021 ([#389](https://github.com/sharksforarms/deku/pull/389))
 - Refactored `logging` feature with massive usability increases ([#352](https://github.com/sharksforarms/deku/pull/352)), ([#355](https://github.com/sharksforarms/deku/pull/355))
@@ -221,6 +224,9 @@ struct DekuTest {
 ```
 - Added `DekuError::Write` to denote `io::Write` errors
 
+## Bug fix
+- Fix error for invalid deku_id generation on generic enum ([#411](https://github.com/sharksforarms/deku/pull/411))
+
 ## [0.16.0] - 2023-02-28
 
 ### Changes
diff --git a/src/reader.rs b/src/reader.rs
index c70e0f3..0846802 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -19,6 +19,9 @@ pub enum ReaderRet {
     Bits(Option<BitVec<u8, Msb0>>),
 }
 
+/// Max bits requested from [`Reader::read_bits`] during one call
+pub const MAX_BITS_AMT: usize = 128;
+
 /// Reader to use with `from_reader_with_ctx`
 pub struct Reader<'a, R: Read> {
     inner: &'a mut R,
@@ -28,9 +31,6 @@ pub struct Reader<'a, R: Read> {
     pub bits_read: usize,
 }
 
-/// Max bits requested from [`Reader::read_bits`] during one call
-pub const MAX_BITS_AMT: usize = 128;
-
 impl<'a, R: Read> Reader<'a, R> {
     /// Create a new `Reader`
     #[inline]
diff --git a/src/writer.rs b/src/writer.rs
index 362a29e..7e02d69 100644
--- a/src/writer.rs
+++ b/src/writer.rs
@@ -13,6 +13,9 @@ const fn bits_of<T>() -> usize {
     core::mem::size_of::<T>().saturating_mul(<u8>::BITS as usize)
 }
 
+/// Max bits written to [`Reader::write_bits`] during one call
+pub const MAX_BITS_AMT: usize = 128;
+
 /// Container to use with `to_writer`
 pub struct Writer<W: Write> {
     pub(crate) inner: W,
@@ -43,6 +46,9 @@ impl<W: Write> Writer<W> {
     ///
     /// Any leftover bits will be written before `bits`, and non-written bits will
     /// be stored in `self.leftover`.
+    ///
+    /// # Params
+    /// `bits`    - Amount of bits that will be written. length must be <= [`MAX_BITS_AMT`].
     #[inline(never)]
     pub fn write_bits(&mut self, bits: &BitSlice<u8, Msb0>) -> Result<(), DekuError> {
         #[cfg(feature = "logging")]
@@ -66,7 +72,8 @@ impl<W: Write> Writer<W> {
         };
 
         // one shot impl of BitSlice::read(no read_exact), but for no_std
-        let mut buf = alloc::vec![0x00; bits.len() / 8];
+        let mut buf = [0; MAX_BITS_AMT];
+        let buf = &mut buf[..bits.len() / 8];
         let mut count = 0;
         bits.chunks_exact(bits_of::<u8>())
             .zip(buf.iter_mut())
@@ -81,7 +88,7 @@ impl<W: Write> Writer<W> {
         bits = unsafe { bits.get_unchecked(count * bits_of::<u8>()..) };
 
         self.leftover = bits.to_bitvec();
-        if self.inner.write_all(&buf).is_err() {
+        if self.inner.write_all(buf).is_err() {
             return Err(DekuError::Write);
         }
 


Test patch:
diff --git a/tests/test_alloc.rs b/tests/test_alloc.rs
index 0ac56ab..8da0463 100644
--- a/tests/test_alloc.rs
+++ b/tests/test_alloc.rs
@@ -63,4 +63,19 @@ mod tests {
             (5, 0, 5)
         );
     }
+
+    #[test]
+    #[cfg_attr(miri, ignore)]
+    fn test_simple_write() {
+        let input = hex!("aa_bbbb_cc_0102_dd_ffffff_aa_0100ff");
+        let t = TestDeku::from_reader((&mut input.as_slice(), 0)).unwrap().1;
+
+        assert_eq!(
+            count_alloc(|| {
+                t.to_bytes().unwrap();
+            })
+            .0,
+            (2, 1, 2)
+        );
+    }
 }