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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use std::io::{Read, Result, Write};
pub trait ReadExactExt: Read {
/// Read a byte array of a constant size.
///
/// For further semantics please refer to [`Read::read_exact`].
///
/// # Examples
/// ```
/// use rw_exact_ext::ReadExactExt;
/// use std::io::Cursor;
///
/// let bytes = [0xAB, 0xCD, 0xEF, 0x42];
/// let array: [u8; 4] = Cursor::new(&bytes).read_array_exact().unwrap();
/// assert_eq!(array, bytes);
/// ```
#[allow(clippy::missing_errors_doc)]
fn read_array_exact<const SIZE: usize>(&mut self) -> Result<[u8; SIZE]> {
let mut buffer = [0; SIZE];
self.read_exact(&mut buffer)?;
Ok(buffer)
}
/// Read one byte and interpret it as a `bool`.
///
/// Returns `true` if the read byte is non-zero, or `false` otherwise.
///
/// For further semantics please refer to [`Read::read_exact`].
///
/// # Examples
/// ```
/// use rw_exact_ext::ReadExactExt;
/// use std::io::Cursor;
///
/// let bytes = [0x01, 0x00, 0xEF, 0x42];
/// let mut cursor = Cursor::new(&bytes);
/// assert!(cursor.read_bool().unwrap());
/// assert!(!cursor.read_bool().unwrap());
/// assert!(cursor.read_bool().unwrap());
/// assert!(cursor.read_bool().unwrap());
/// ```
#[allow(clippy::missing_errors_doc)]
fn read_bool(&mut self) -> Result<bool> {
self.read_array_exact::<1>().map(|[byte]| byte != 0)
}
/// Read a `Vec<u8>` of a given size.
///
/// For further semantics please refer to [`Read::read_exact`].
///
/// # Examples
/// ```
/// use rw_exact_ext::ReadExactExt;
/// use std::io::Cursor;
///
/// let bytes = [0xAB, 0xCD, 0xEF, 0x42];
/// let vec = Cursor::new(&bytes).read_vec_exact(bytes.len()).unwrap();
/// assert_eq!(vec, Vec::from(bytes));
/// ```
#[allow(clippy::missing_errors_doc)]
fn read_vec_exact(&mut self, size: usize) -> Result<Vec<u8>> {
let mut buffer = vec![0; size];
self.read_exact(&mut buffer)?;
Ok(buffer)
}
/// Read a number from a byte array in big endian.
///
/// For further semantics please refer to [`Read::read_exact`].
///
/// # Examples
/// ```
/// use rw_exact_ext::ReadExactExt;
/// use std::io::Cursor;
///
/// let bytes = [0xAB, 0xCD, 0xEF, 0x42];
///
/// let unsigned: u32 = Cursor::new(&bytes).read_num_be().unwrap();
/// assert_eq!(unsigned, 0xABCDEF42);
///
/// let signed: i32 = Cursor::new(&bytes).read_num_be().unwrap();
/// assert_eq!(signed, -0x543210BE);
///
/// let float: f32 = Cursor::new(&bytes).read_num_be().unwrap();
/// assert_eq!(float, -1.4632533e-12);
/// ```
#[cfg(feature = "num-traits")]
#[allow(clippy::missing_errors_doc)]
fn read_num_be<N, const SIZE: usize>(&mut self) -> Result<N>
where
N: num_traits::FromBytes<Bytes = [u8; SIZE]>,
{
self.read_array_exact()
.map(|bytes| N::from_be_bytes(&bytes))
}
/// Read a number from a byte array in little endian.
///
/// For further semantics please refer to [`Read::read_exact`].
///
/// # Examples
/// ```
/// use rw_exact_ext::ReadExactExt;
/// use std::io::Cursor;
///
/// let bytes = [0xAB, 0xCD, 0xEF, 0x42];
///
/// let unsigned: u32 = Cursor::new(&bytes).read_num_le().unwrap();
/// assert_eq!(unsigned, 0x42EFCDAB);
///
/// let signed: i32 = Cursor::new(&bytes).read_num_le().unwrap();
/// assert_eq!(signed, 0x42EFCDAB);
///
/// let float: f32 = Cursor::new(&bytes).read_num_le().unwrap();
/// assert_eq!(float, 119.901695);
/// ```
#[cfg(feature = "num-traits")]
#[allow(clippy::missing_errors_doc)]
fn read_num_le<N, const SIZE: usize>(&mut self) -> Result<N>
where
N: num_traits::FromBytes<Bytes = [u8; SIZE]>,
{
self.read_array_exact()
.map(|bytes| N::from_le_bytes(&bytes))
}
/// Read a number from a byte array in native endianness.
///
/// For further semantics please refer to [`Read::read_exact`].
#[cfg(feature = "num-traits")]
#[allow(clippy::missing_errors_doc)]
fn read_num_ne<N, const SIZE: usize>(&mut self) -> Result<N>
where
N: num_traits::FromBytes<Bytes = [u8; SIZE]>,
{
self.read_array_exact()
.map(|bytes| N::from_ne_bytes(&bytes))
}
}
impl<T> ReadExactExt for T where T: Read {}
pub trait WriteAllExt: Write {
/// Write a `bool` as one byte.
///
/// For further semantics please refer to [`Read::read_exact`].
///
/// # Examples
/// ```
/// use rw_exact_ext::WriteAllExt;
/// use std::io::Cursor;
///
/// let mut bytes = vec![0xFF];
///
/// Cursor::new(&mut bytes).write_bool(true).unwrap();
/// assert_eq!(bytes, [0x01]);
///
/// Cursor::new(&mut bytes).write_bool(false).unwrap();
/// assert_eq!(bytes, [0x00]);
/// ```
#[allow(clippy::missing_errors_doc)]
fn write_bool(&mut self, boolean: bool) -> Result<()> {
self.write_all(&[boolean.into()])
}
/// Write a number to bytes in big endian.
///
/// For further semantics please refer to [`Write::write_all`].
///
/// # Examples
/// ```
/// use rw_exact_ext::WriteAllExt;
/// use std::io::Cursor;
///
/// let mut bytes = vec![0u8; 4];
///
/// let unsigned: u32 = 1337;
/// Cursor::new(&mut bytes).write_num_be(unsigned).unwrap();
/// assert_eq!(bytes, vec![0x00, 0x00, 0x05, 0x39]);
///
/// let signed: i32 = -1337;
/// Cursor::new(&mut bytes).write_num_be(signed).unwrap();
/// assert_eq!(bytes, vec![0xFF, 0xFF, 0xFA, 0xC7]);
///
/// let float: f32 = 133.7;
/// Cursor::new(&mut bytes).write_num_be(float).unwrap();
/// assert_eq!(bytes, vec![0x43, 0x05, 0xB3, 0x33]);
/// ```
#[cfg(feature = "num-traits")]
#[allow(clippy::missing_errors_doc)]
fn write_num_be<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
where
N: num_traits::ToBytes<Bytes = [u8; SIZE]>,
{
self.write_all(&num.to_be_bytes())
}
/// Write a number to bytes in little endian.
///
/// For further semantics please refer to [`Write::write_all`].
///
/// # Examples
/// ```
/// use rw_exact_ext::WriteAllExt;
/// use std::io::Cursor;
///
/// let mut bytes = vec![0u8; 4];
///
/// let unsigned: u32 = 1337;
/// Cursor::new(&mut bytes).write_num_le(unsigned).unwrap();
/// assert_eq!(bytes, vec![0x39, 0x05, 0x00, 0x00]);
///
/// let signed: i32 = -1337;
/// Cursor::new(&mut bytes).write_num_le(signed).unwrap();
/// assert_eq!(bytes, vec![0xC7, 0xFA, 0xFF, 0xFF]);
///
/// let float: f32 = 133.7;
/// Cursor::new(&mut bytes).write_num_le(float).unwrap();
/// assert_eq!(bytes, vec![0x33, 0xB3, 0x05, 0x43]);
/// ```
#[cfg(feature = "num-traits")]
#[allow(clippy::missing_errors_doc)]
fn write_num_le<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
where
N: num_traits::ToBytes<Bytes = [u8; SIZE]>,
{
self.write_all(&num.to_le_bytes())
}
/// Write a number to bytes in native endianness.
///
/// For further semantics please refer to [`Write::write_all`].
#[cfg(feature = "num-traits")]
#[allow(clippy::missing_errors_doc)]
fn write_num_ne<N, const SIZE: usize>(&mut self, num: N) -> Result<()>
where
N: num_traits::ToBytes<Bytes = [u8; SIZE]>,
{
self.write_all(&num.to_ne_bytes())
}
}
impl<T> WriteAllExt for T where T: Write {}