Skip to content

Commit 2afd5c9

Browse files
committed
Impl DekuWriter for Box<T>
1 parent 26c00d3 commit 2afd5c9

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/impls/boxed.rs

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ where
5252
}
5353
}
5454

55+
impl<T, Ctx> DekuWriter<Ctx> for Box<T>
56+
where
57+
T: DekuWriter<Ctx>,
58+
Ctx: Copy,
59+
{
60+
/// Write all `T`s to bits
61+
fn to_writer<W: Write>(&self, writer: &mut Writer<W>, ctx: Ctx) -> Result<(), DekuError> {
62+
self.as_ref().to_writer(writer, ctx)?;
63+
Ok(())
64+
}
65+
}
66+
5567
#[cfg(test)]
5668
mod tests {
5769
use no_std_io::io::Cursor;

tests/test_box.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use deku::prelude::*;
2+
3+
#[derive(DekuRead, DekuWrite)]
4+
struct TestStruct {
5+
field: Box<u8>,
6+
}
7+
8+
#[test]
9+
fn test_box_smoke_test() {
10+
let test_data: &[u8] = &[0u8; 100];
11+
let a = TestStruct::try_from(test_data).unwrap();
12+
let new_bytes = a.to_bytes().unwrap();
13+
assert_eq!(test_data, &*new_bytes);
14+
}

0 commit comments

Comments
 (0)