Skip to content

Commit

Permalink
Impl DekuWriter for Box<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Jan 7, 2024
1 parent 26c00d3 commit 2afd5c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/impls/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ where
}
}

impl<T, Ctx> DekuWriter<Ctx> for Box<T>
where
T: DekuWriter<Ctx>,
Ctx: Copy,
{
/// Write all `T`s to bits
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, ctx: Ctx) -> Result<(), DekuError> {
self.as_ref().to_writer(writer, ctx)?;
Ok(())
}
}

#[cfg(test)]
mod tests {
use no_std_io::io::Cursor;
Expand Down
14 changes: 14 additions & 0 deletions tests/test_box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use deku::prelude::*;

#[derive(DekuRead, DekuWrite)]
struct TestStruct {
field: Box<u8>,
}

#[test]
fn test_box_smoke_test() {
let test_data: &[u8] = &[0u8; 100];
let a = TestStruct::try_from(test_data).unwrap();
let new_bytes = a.to_bytes().unwrap();
assert_eq!(test_data, &*new_bytes);
}

0 comments on commit 2afd5c9

Please sign in to comment.