diff --git a/deku-derive/src/macros/deku_write.rs b/deku-derive/src/macros/deku_write.rs index 3f3b90fe..871d1623 100644 --- a/deku-derive/src/macros/deku_write.rs +++ b/deku-derive/src/macros/deku_write.rs @@ -34,6 +34,7 @@ fn emit_struct(input: &DekuData) -> Result { let field_updates = emit_field_updates(&fields, Some(quote! { self. })); let named = fields.style.is_struct(); + let unit = fields.style.is_unit(); let field_idents = fields.iter().enumerate().filter_map(|(i, f)| { if !f.temp { @@ -43,7 +44,7 @@ fn emit_struct(input: &DekuData) -> Result { } }); - let destructured = gen_struct_destruction(named, &input.ident, field_idents); + let destructured = gen_struct_destruction(named, unit, &input.ident, field_idents); // Implement `DekuContainerWrite` for types that don't need a context if input.ctx.is_none() || (input.ctx.is_some() && input.ctx_default.is_some()) { diff --git a/deku-derive/src/macros/mod.rs b/deku-derive/src/macros/mod.rs index 8853dc45..847db153 100644 --- a/deku-derive/src/macros/mod.rs +++ b/deku-derive/src/macros/mod.rs @@ -95,10 +95,15 @@ fn gen_struct_init( /// - Unnamed: `#ident ( ref fields )` fn gen_struct_destruction( named: bool, + unit: bool, ident: I, field_idents: impl Iterator, ) -> TokenStream { - if named { + if unit { + quote! { + #ident + } + } else if named { quote! { #ident { #(ref #field_idents),* diff --git a/tests/test_struct.rs b/tests/test_struct.rs index 41e3b3d4..c5bb117b 100644 --- a/tests/test_struct.rs +++ b/tests/test_struct.rs @@ -204,3 +204,19 @@ fn test_big_endian() { let new_bytes = a.to_bytes().unwrap(); assert_eq!(&bytes[..2], &*new_bytes); } + +#[test] +fn test_units() { + #[derive(DekuRead, DekuWrite)] + #[deku(magic = b"\xf0")] + struct Unit; + + let bytes = [0xf0]; + let a = Unit::from_bytes((&bytes, 0)).unwrap().1; + let new_bytes = a.to_bytes().unwrap(); + assert_eq!(bytes, &*new_bytes); + + let a = Unit; + let new_bytes = a.to_bytes().unwrap(); + assert_eq!(bytes, &*new_bytes); +}