1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
use scroll::{Cread, Cwrite, Pread, Pwrite, LE};
use scroll_derive::{IOread, IOwrite, Pread, Pwrite, SizeWith};
use scroll::ctx::SizeWith;
macro_rules! test {
( struct $name:ident { $( $field:ident: $t:ty, )* } ) => {
// check we can exist inside a macro_rules
// https://github.com/m4b/scroll/pull/75
#[derive(Pread, Pwrite)]
pub struct $name {
$( pub $field: $t, )*
}
};
}
test! {
struct Test {
field: [u16; 40],
}
}
#[derive(Debug, PartialEq, Pread, Pwrite)]
struct Data {
id: u32,
timestamp: f64,
}
#[test]
fn test_data() {
let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63];
let data: Data = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
assert_eq!(data.id, 0xdeadbeefu32);
assert_eq!(data.timestamp, 0.5f64);
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);
}
#[derive(Debug, PartialEq, Pread, Pwrite)]
struct Data2 {
name: [u8; 32],
}
#[test]
fn test_array() {
let bytes = [0u8; 64];
let data: Data2 = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
}
#[derive(Debug, PartialEq, Pread, Pwrite, SizeWith)]
struct Data3 {
name: u32,
}
#[test]
fn test_sizewith() {
let bytes = [0u8; 64];
let data: Data3 = bytes.gread_with(&mut 0, LE).unwrap();
println!("data: {:?}", &data);
}
#[derive(Debug, PartialEq, IOread, IOwrite, SizeWith)]
struct Data4 {
name: u32,
j: u16,
arr: [u8; 2],
}
#[test]
fn test_ioread() {
let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf];
let data: Data4 = bytes.cread_with(0, LE);
println!("data: {:?}", &data);
assert_eq!(data.name, 50462976);
assert_eq!(data.j, 0xedde);
assert_eq!(data.arr, [0xbe, 0xaf]);
}
#[test]
fn test_iowrite() {
let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf];
let data: Data4 = bytes.cread_with(0, LE);
println!("data: {:?}", &data);
assert_eq!(data.name, 50462976);
assert_eq!(data.j, 0xedde);
assert_eq!(data.arr, [0xbe, 0xaf]);
let mut bytes_null = [0u8; 8];
bytes_null.cwrite_with(&data, 0, LE);
println!("bytes_null: {:?}", &bytes_null);
println!("bytes : {:?}", &bytes);
assert_eq!(bytes_null, bytes);
let mut bytes_null = [0u8; 8];
bytes_null.cwrite_with(data, 0, LE);
println!("bytes_null: {:?}", &bytes_null);
println!("bytes : {:?}", &bytes);
assert_eq!(bytes_null, bytes);
}
#[derive(Debug, PartialEq, Pread, SizeWith)]
#[repr(C)]
struct Data5 {
name: u32,
j: u16,
arr1: [u8; 2],
arr2: [u16; 2],
}
#[test]
fn test_pread_arrays() {
let bytes = [0, 1, 2, 3, 0, 0, 0xde, 0xed, 0xad, 0xde, 0xef, 0xbe];
let data: Data5 = bytes.pread_with(0, LE).unwrap();
println!("data: {:?}", &data);
assert_eq!(data.name, 50462976);
assert_eq!(data.arr1, [0xde, 0xed]);
assert_eq!(data.arr2, [0xdead, 0xbeef]);
let offset = &mut 0;
let data: Data5 = bytes.gread_with(offset, LE).unwrap();
println!("data: {:?}", &data);
assert_eq!(data.name, 50462976);
assert_eq!(data.arr1, [0xde, 0xed]);
assert_eq!(data.arr2, [0xdead, 0xbeef]);
assert_eq!(*offset, ::std::mem::size_of::<Data5>());
}
#[derive(Debug, PartialEq, Pread, SizeWith)]
#[repr(C)]
struct Data6 {
id: u32,
name: [u8; 5],
}
#[test]
fn test_array_copy() {
let bytes = [0xde, 0xed, 0xef, 0xbe, 0x68, 0x65, 0x6c, 0x6c, 0x0];
let data: Data6 = bytes.pread_with(0, LE).unwrap();
let name: &str = data.name.pread(0).unwrap();
println!("data: {:?}", &data);
println!("data.name: {:?}", name);
assert_eq!(data.id, 0xbeefedde);
assert_eq!(name, "hell");
}
#[derive(Debug, PartialEq, Eq, Pread, Pwrite, SizeWith)]
struct Data7A {
pub y: u64,
pub x: u32,
}
#[derive(Debug, PartialEq, Eq, Pread, Pwrite, SizeWith)]
struct Data7B {
pub a: Data7A,
}
#[test]
fn test_nested_struct() {
let b = Data7B {
a: Data7A { y: 1, x: 2 },
};
let size = Data7B::size_with(&LE);
assert_eq!(size, 12);
let mut bytes = vec![0; size];
let written = bytes.pwrite_with(&b, 0, LE).unwrap();
assert_eq!(written, size);
let mut read = 0;
let b2: Data7B = bytes.gread_with(&mut read, LE).unwrap();
assert_eq!(read, size);
assert_eq!(b, b2);
}
#[derive(Debug, PartialEq, Eq, Pread, Pwrite, IOread, IOwrite, SizeWith)]
#[repr(C)]
struct Data8<T, Y> {
ids: [T; 3],
xyz: Y,
}
#[test]
fn test_generics() {
let mut bytes = [0xde, 0xed, 0xef, 0x10, 0x10];
let data: Data8<u8, u16> = bytes.pread_with(0, LE).unwrap();
assert_eq!(data.ids, [0xde, 0xed, 0xef]);
assert_eq!(data.xyz, 0x1010);
let data: Data8<u8, u16> = bytes.cread_with(0, LE);
assert_eq!(data.ids, [0xde, 0xed, 0xef]);
assert_eq!(data.xyz, 0x1010);
let size = Data8::<u8, u16>::size_with(&LE);
let written = bytes.pwrite_with(&data, 0, LE).unwrap();
assert_eq!(written, size);
}
#[derive(Debug, PartialEq, Eq, Pread, Pwrite, IOread, IOwrite, SizeWith)]
struct Data9(u8, u16);
#[test]
fn test_newtype() {
let mut bytes = [0xde, 0x10, 0x10];
let data: Data9 = bytes.pread_with(0, LE).unwrap();
assert_eq!(data.0, 0xde);
assert_eq!(data.1, 0x1010);
let data: Data9 = bytes.cread_with(0, LE);
assert_eq!(data.0, 0xde);
assert_eq!(data.1, 0x1010);
let size = Data9::size_with(&LE);
let written = bytes.pwrite_with(&data, 0, LE).unwrap();
assert_eq!(written, size);
}
|