diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/scroll_derive/examples | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | third_party/rust/scroll_derive/examples/main.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/third_party/rust/scroll_derive/examples/main.rs b/third_party/rust/scroll_derive/examples/main.rs new file mode 100644 index 0000000000..3f2c721eeb --- /dev/null +++ b/third_party/rust/scroll_derive/examples/main.rs @@ -0,0 +1,26 @@ +use scroll_derive::{Pread, Pwrite, IOread, IOwrite, SizeWith}; + +#[derive(Debug, PartialEq, Pread, Pwrite, IOread, IOwrite, SizeWith)] +#[repr(C)] +struct Data { + id: u32, + timestamp: f64, + arr: [u16; 2], +} + +use scroll::{Pread, Pwrite, Cread, LE}; + +fn main () { + let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63, 0xad, 0xde, 0xef, 0xbe]; + let data: Data = bytes.pread_with(0, LE).unwrap(); + println!("data: {:?}", &data); + assert_eq!(data.id, 0xdeadbeefu32); + let mut bytes2 = vec![0; ::std::mem::size_of::<Data>()]; + bytes2.pwrite_with(data, 0, LE).unwrap(); + let data: Data = bytes.pread_with(0, LE).unwrap(); + let data2: Data = bytes2.pread_with(0, LE).unwrap(); + assert_eq!(data, data2); + + let data: Data = bytes.cread_with(0, LE); + assert_eq!(data, data2); +} |