Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - BinWrite for PosValue<T> #273

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions binrw/src/pos_value.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{
io::{Read, Seek},
BinRead, BinResult, Endian,
io::{Read, Seek, Write},
BinRead, BinResult, BinWrite, Endian,
};
use core::fmt;

/// A wrapper that stores a value’s position alongside the value.
/// Serializing a `PosValue` will ignore the `pos` field.
///
/// # Examples
///
Expand Down Expand Up @@ -46,6 +47,19 @@ impl<T: BinRead> BinRead for PosValue<T> {
}
}

impl<T: BinWrite> BinWrite for PosValue<T> {
type Args<'a> = T::Args<'a>;

fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
endian: Endian,
args: Self::Args<'_>,
) -> BinResult<()> {
self.val.write_options(writer, endian, args)
}
}

impl<T> core::ops::Deref for PosValue<T> {
type Target = T;

Expand Down Expand Up @@ -80,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(),
}
}
}
22 changes: 19 additions & 3 deletions binrw/tests/pos_value.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
extern crate alloc;

use alloc::format;
use binrw::{io::Cursor, BinRead, BinReaderExt, PosValue};
use binrw::{io::Cursor, BinRead, BinReaderExt, BinWrite, PosValue};

#[test]
fn pos_value() {
#[derive(BinRead)]
#[derive(BinRead, BinWrite, Default)]
struct MyType {
a: u16,
b: PosValue<u8>,
}

let mut val = Cursor::new(b"\xFF\xFE\xFD").read_be::<MyType>().unwrap();
let mut val: MyType = Cursor::new(b"\xFF\xFE\xFD").read_be::<MyType>().unwrap();
assert_eq!(val.a, 0xFFFE);
assert_eq!(val.b.pos, 2);
assert_eq!(*val.b, 0xFD);
Expand All @@ -23,4 +23,20 @@ fn pos_value() {
let clone = val.b.clone();
assert_eq!(*clone, *val.b);
assert_eq!(clone.pos, val.b.pos);

let mut output = Vec::new();
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);
}