Skip to content

Commit

Permalink
Implement Default and From<T> for PosValue<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
amirbou committed Jun 29, 2024
1 parent a8016e8 commit 242d465
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
18 changes: 18 additions & 0 deletions binrw/src/pos_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,21 @@ impl<U, T: PartialEq<U>> PartialEq<U> for PosValue<T> {
self.val == *other
}
}

impl<T: Default> Default for PosValue<T> {
fn default() -> Self {
Self {
val: Default::default(),
pos: Default::default(),
}
}
}

impl<T> From<T> for PosValue<T> {
fn from(val: T) -> Self {
Self {
val,
pos: Default::default(),
}
}
}
13 changes: 12 additions & 1 deletion binrw/tests/pos_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
Expand All @@ -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);
}

0 comments on commit 242d465

Please sign in to comment.