diff --git a/binrw/src/pos_value.rs b/binrw/src/pos_value.rs index 619489c7..dbc663f8 100644 --- a/binrw/src/pos_value.rs +++ b/binrw/src/pos_value.rs @@ -94,3 +94,21 @@ impl> PartialEq for PosValue { self.val == *other } } + +impl Default for PosValue { + fn default() -> Self { + Self { + val: Default::default(), + pos: Default::default(), + } + } +} + +impl From for PosValue { + fn from(val: T) -> Self { + Self { + val, + pos: Default::default(), + } + } +} diff --git a/binrw/tests/pos_value.rs b/binrw/tests/pos_value.rs index e8caa6d6..4233d650 100644 --- a/binrw/tests/pos_value.rs +++ b/binrw/tests/pos_value.rs @@ -5,7 +5,7 @@ use binrw::{io::Cursor, BinRead, BinReaderExt, BinWrite, PosValue}; #[test] fn pos_value() { - #[derive(BinRead, BinWrite)] + #[derive(BinRead, BinWrite, Default)] struct MyType { a: u16, b: PosValue, @@ -28,4 +28,15 @@ fn pos_value() { val.write_be(&mut Cursor::new(&mut output)).unwrap(); assert_eq!(output, b"\xFF\xFE\x01"); + let default_val = MyType::default(); + assert_eq!(default_val.a, u16::default()); + assert_eq!(*default_val.b, u8::default()); + assert_eq!(default_val.b.pos, u64::default()); + + let from = MyType { + a: val.a, + b: (*val.b).into(), + }; + assert_eq!(from.a, val.a); + assert_eq!(from.b, *val.b); }