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
use ;
use crate;
/// An extension trait to `std::io::Read` streams; mainly targeted at reading primitive types with
/// a known size.
///
/// Requires types to implement [`FromCtx`](ctx/trait.FromCtx.html) and [`SizeWith`](ctx/trait.SizeWith.html).
///
/// **NB** You should probably add `repr(C)` and be very careful how you implement
/// [`SizeWith`](ctx/trait.SizeWith.html), otherwise you will get IO errors failing to fill entire
/// buffer (the size you specified in `SizeWith`), or out of bound errors (depending on your impl)
/// in `from_ctx`.
///
/// Warning: Currently ioread/write uses a small 256-byte buffer and can not read/write larger types
///
/// # Example
/// ```rust
/// use std::io::Cursor;
/// use scroll::{self, ctx, LE, Pread, IOread};
///
/// #[repr(packed)]
/// struct Foo {
/// foo: i64,
/// bar: u32,
/// }
///
/// impl ctx::FromCtx<scroll::Endian> for Foo {
/// fn from_ctx(bytes: &[u8], ctx: scroll::Endian) -> Self {
/// Foo { foo: bytes.pread_with::<i64>(0, ctx).unwrap(), bar: bytes.pread_with::<u32>(8, ctx).unwrap() }
/// }
/// }
///
/// impl ctx::SizeWith<scroll::Endian> for Foo {
/// // our parsing context doesn't influence our size
/// fn size_with(_: &scroll::Endian) -> usize {
/// ::std::mem::size_of::<Foo>()
/// }
/// }
///
/// let bytes_ = [0x0b,0x0b,0x00,0x00,0x00,0x00,0x00,0x00, 0xef,0xbe,0x00,0x00,];
/// let mut bytes = Cursor::new(bytes_);
/// let foo = bytes.ioread_with::<i64>(LE).unwrap();
/// let bar = bytes.ioread_with::<u32>(LE).unwrap();
/// assert_eq!(foo, 0xb0b);
/// assert_eq!(bar, 0xbeef);
/// let error = bytes.ioread_with::<f64>(LE);
/// assert!(error.is_err());
/// let mut bytes = Cursor::new(bytes_);
/// let foo_ = bytes.ioread_with::<Foo>(LE).unwrap();
/// // Remember that you need to copy out fields from packed structs
/// // with a `{}` block instead of borrowing them directly
/// // ref: https://github.com/rust-lang/rust/issues/46043
/// assert_eq!({foo_.foo}, foo);
/// assert_eq!({foo_.bar}, bar);
/// ```
///
/// Types that implement `Read` get methods defined in `IOread`
/// for free.
/// An extension trait to `std::io::Write` streams; this only serializes simple types, like `u8`, `i32`, `f32`, `usize`, etc.
///
/// To write custom types with a single `iowrite::<YourType>` call, implement [`IntoCtx`](ctx/trait.IntoCtx.html) and [`SizeWith`](ctx/trait.SizeWith.html) for `YourType`.
/// Types that implement `Write` get methods defined in `IOwrite`
/// for free.