/rust/registry/src/index.crates.io-6f17d22bba15001f/bincode-1.3.3/src/byteorder.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2015 Andrew Gallant |
2 | | |
3 | | use std::io; |
4 | | use std::io::Result; |
5 | | use std::ptr::copy_nonoverlapping; |
6 | | |
7 | | #[derive(Copy, Clone)] |
8 | | pub struct LittleEndian; |
9 | | |
10 | | #[derive(Copy, Clone)] |
11 | | pub struct BigEndian; |
12 | | |
13 | | #[cfg(target_endian = "little")] |
14 | | pub type NativeEndian = LittleEndian; |
15 | | |
16 | | #[cfg(target_endian = "big")] |
17 | | pub type NativeEndian = BigEndian; |
18 | | |
19 | | macro_rules! read_num_bytes { |
20 | | ($ty:ty, $size:expr, $src:expr, $which:ident) => {{ |
21 | | assert!($size == ::std::mem::size_of::<$ty>()); |
22 | | assert!($size <= $src.len()); |
23 | | let mut data: $ty = 0; |
24 | | unsafe { |
25 | | copy_nonoverlapping($src.as_ptr(), &mut data as *mut $ty as *mut u8, $size); |
26 | | } |
27 | | data.$which() |
28 | | }}; |
29 | | } |
30 | | |
31 | | macro_rules! write_num_bytes { |
32 | | ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => {{ |
33 | | assert!($size <= $dst.len()); |
34 | | unsafe { |
35 | | // N.B. https://github.com/rust-lang/rust/issues/22776 |
36 | | let bytes = *(&$n.$which() as *const _ as *const [u8; $size]); |
37 | | copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size); |
38 | | } |
39 | | }}; |
40 | | } |
41 | | |
42 | | impl ByteOrder for LittleEndian { |
43 | | #[inline] |
44 | 0 | fn read_u16(buf: &[u8]) -> u16 { |
45 | 0 | read_num_bytes!(u16, 2, buf, to_le) |
46 | 0 | } |
47 | | |
48 | | #[inline] |
49 | 89.1k | fn read_u32(buf: &[u8]) -> u32 { |
50 | 89.1k | read_num_bytes!(u32, 4, buf, to_le) |
51 | 89.1k | } <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::read_u32 Line | Count | Source | 49 | 89.1k | fn read_u32(buf: &[u8]) -> u32 { | 50 | 89.1k | read_num_bytes!(u32, 4, buf, to_le) | 51 | 89.1k | } |
Unexecuted instantiation: <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::read_u32 |
52 | | |
53 | | #[inline] |
54 | 271k | fn read_u64(buf: &[u8]) -> u64 { |
55 | 271k | read_num_bytes!(u64, 8, buf, to_le) |
56 | 271k | } <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::read_u64 Line | Count | Source | 54 | 271k | fn read_u64(buf: &[u8]) -> u64 { | 55 | 271k | read_num_bytes!(u64, 8, buf, to_le) | 56 | 271k | } |
Unexecuted instantiation: <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::read_u64 |
57 | | |
58 | | #[inline] |
59 | 0 | fn write_u16(buf: &mut [u8], n: u16) { |
60 | 0 | write_num_bytes!(u16, 2, n, buf, to_le); |
61 | 0 | } |
62 | | |
63 | | #[inline] |
64 | 0 | fn write_u32(buf: &mut [u8], n: u32) { |
65 | 0 | write_num_bytes!(u32, 4, n, buf, to_le); |
66 | 0 | } Unexecuted instantiation: <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::write_u32 Unexecuted instantiation: <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::write_u32 |
67 | | |
68 | | #[inline] |
69 | 0 | fn write_u64(buf: &mut [u8], n: u64) { |
70 | 0 | write_num_bytes!(u64, 8, n, buf, to_le); |
71 | 0 | } Unexecuted instantiation: <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::write_u64 Unexecuted instantiation: <bincode::byteorder::LittleEndian as bincode::byteorder::ByteOrder>::write_u64 |
72 | | |
73 | | serde_if_integer128! { |
74 | | #[inline] |
75 | 0 | fn write_u128(buf: &mut [u8], n: u128) { |
76 | 0 | write_num_bytes!(u128, 16, n, buf, to_le); |
77 | 0 | } |
78 | | |
79 | | #[inline] |
80 | 0 | fn read_u128(buf: &[u8]) -> u128 { |
81 | 0 | read_num_bytes!(u128, 16, buf, to_le) |
82 | 0 | } |
83 | | } |
84 | | } |
85 | | |
86 | | impl ByteOrder for BigEndian { |
87 | | #[inline] |
88 | 0 | fn read_u16(buf: &[u8]) -> u16 { |
89 | 0 | read_num_bytes!(u16, 2, buf, to_be) |
90 | 0 | } |
91 | | |
92 | | #[inline] |
93 | 0 | fn read_u32(buf: &[u8]) -> u32 { |
94 | 0 | read_num_bytes!(u32, 4, buf, to_be) |
95 | 0 | } |
96 | | |
97 | | #[inline] |
98 | 0 | fn read_u64(buf: &[u8]) -> u64 { |
99 | 0 | read_num_bytes!(u64, 8, buf, to_be) |
100 | 0 | } |
101 | | |
102 | | #[inline] |
103 | 0 | fn write_u16(buf: &mut [u8], n: u16) { |
104 | 0 | write_num_bytes!(u16, 2, n, buf, to_be); |
105 | 0 | } |
106 | | |
107 | | #[inline] |
108 | 0 | fn write_u32(buf: &mut [u8], n: u32) { |
109 | 0 | write_num_bytes!(u32, 4, n, buf, to_be); |
110 | 0 | } |
111 | | |
112 | | #[inline] |
113 | 0 | fn write_u64(buf: &mut [u8], n: u64) { |
114 | 0 | write_num_bytes!(u64, 8, n, buf, to_be); |
115 | 0 | } |
116 | | |
117 | | serde_if_integer128! { |
118 | | #[inline] |
119 | 0 | fn write_u128(buf: &mut [u8], n: u128) { |
120 | 0 | write_num_bytes!(u128, 16, n, buf, to_be); |
121 | 0 | } |
122 | | |
123 | | #[inline] |
124 | 0 | fn read_u128(buf: &[u8]) -> u128 { |
125 | 0 | read_num_bytes!(u128, 16, buf, to_be) |
126 | 0 | } |
127 | | } |
128 | | } |
129 | | |
130 | | pub trait ByteOrder: Clone + Copy { |
131 | | fn read_u16(buf: &[u8]) -> u16; |
132 | | |
133 | | fn read_u32(buf: &[u8]) -> u32; |
134 | | |
135 | | fn read_u64(buf: &[u8]) -> u64; |
136 | | |
137 | | fn write_u16(buf: &mut [u8], n: u16); |
138 | | |
139 | | fn write_u32(buf: &mut [u8], n: u32); |
140 | | |
141 | | fn write_u64(buf: &mut [u8], n: u64); |
142 | | |
143 | | #[inline] |
144 | 0 | fn read_i16(buf: &[u8]) -> i16 { |
145 | 0 | Self::read_u16(buf) as i16 |
146 | 0 | } |
147 | | |
148 | | #[inline] |
149 | 0 | fn read_i32(buf: &[u8]) -> i32 { |
150 | 0 | Self::read_u32(buf) as i32 |
151 | 0 | } |
152 | | |
153 | | #[inline] |
154 | 0 | fn read_i64(buf: &[u8]) -> i64 { |
155 | 0 | Self::read_u64(buf) as i64 |
156 | 0 | } |
157 | | |
158 | | #[inline] |
159 | 0 | fn read_f32(buf: &[u8]) -> f32 { |
160 | 0 | unsafe { *(&Self::read_u32(buf) as *const u32 as *const f32) } |
161 | 0 | } |
162 | | |
163 | | #[inline] |
164 | 0 | fn read_f64(buf: &[u8]) -> f64 { |
165 | 0 | unsafe { *(&Self::read_u64(buf) as *const u64 as *const f64) } |
166 | 0 | } |
167 | | |
168 | | #[inline] |
169 | 0 | fn write_i16(buf: &mut [u8], n: i16) { |
170 | 0 | Self::write_u16(buf, n as u16) |
171 | 0 | } |
172 | | |
173 | | #[inline] |
174 | 0 | fn write_i32(buf: &mut [u8], n: i32) { |
175 | 0 | Self::write_u32(buf, n as u32) |
176 | 0 | } |
177 | | |
178 | | #[inline] |
179 | 0 | fn write_i64(buf: &mut [u8], n: i64) { |
180 | 0 | Self::write_u64(buf, n as u64) |
181 | 0 | } |
182 | | |
183 | | #[inline] |
184 | 0 | fn write_f32(buf: &mut [u8], n: f32) { |
185 | 0 | let n = unsafe { *(&n as *const f32 as *const u32) }; |
186 | 0 | Self::write_u32(buf, n) |
187 | 0 | } |
188 | | |
189 | | #[inline] |
190 | 0 | fn write_f64(buf: &mut [u8], n: f64) { |
191 | 0 | let n = unsafe { *(&n as *const f64 as *const u64) }; |
192 | 0 | Self::write_u64(buf, n) |
193 | 0 | } |
194 | | |
195 | | serde_if_integer128! { |
196 | | fn read_u128(buf: &[u8]) -> u128; |
197 | | fn write_u128(buf: &mut [u8], n: u128); |
198 | | |
199 | | #[inline] |
200 | 0 | fn read_i128(buf: &[u8]) -> i128 { |
201 | 0 | Self::read_u128(buf) as i128 |
202 | 0 | } |
203 | | |
204 | | #[inline] |
205 | 0 | fn write_i128(buf: &mut [u8], n: i128) { |
206 | 0 | Self::write_u128(buf, n as u128) |
207 | 0 | } |
208 | | } |
209 | | } |
210 | | |
211 | | pub trait ReadBytesExt: io::Read { |
212 | | #[inline] |
213 | 1.40M | fn read_u8(&mut self) -> Result<u8> { |
214 | 1.40M | let mut buf = [0; 1]; |
215 | 1.40M | try!(self.read_exact(&mut buf)); |
216 | 1.40M | Ok(buf[0]) |
217 | 1.40M | } <bincode::de::read::IoReader<&mut flate2::zlib::bufread::ZlibDecoder<&[u8]>> as bincode::byteorder::ReadBytesExt>::read_u8 Line | Count | Source | 213 | 126k | fn read_u8(&mut self) -> Result<u8> { | 214 | 126k | let mut buf = [0; 1]; | 215 | 126k | try!(self.read_exact(&mut buf)); | 216 | 126k | Ok(buf[0]) | 217 | 126k | } |
<bincode::de::read::IoReader<&[u8]> as bincode::byteorder::ReadBytesExt>::read_u8 Line | Count | Source | 213 | 1.27M | fn read_u8(&mut self) -> Result<u8> { | 214 | 1.27M | let mut buf = [0; 1]; | 215 | 1.27M | try!(self.read_exact(&mut buf)); | 216 | 1.27M | Ok(buf[0]) | 217 | 1.27M | } |
Unexecuted instantiation: <_ as bincode::byteorder::ReadBytesExt>::read_u8 |
218 | | |
219 | | #[inline] |
220 | 0 | fn read_i8(&mut self) -> Result<i8> { |
221 | 0 | let mut buf = [0; 1]; |
222 | 0 | try!(self.read_exact(&mut buf)); |
223 | 0 | Ok(buf[0] as i8) |
224 | 0 | } |
225 | | |
226 | | #[inline] |
227 | 0 | fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> { |
228 | 0 | let mut buf = [0; 2]; |
229 | 0 | try!(self.read_exact(&mut buf)); |
230 | 0 | Ok(T::read_u16(&buf)) |
231 | 0 | } |
232 | | |
233 | | #[inline] |
234 | 0 | fn read_i16<T: ByteOrder>(&mut self) -> Result<i16> { |
235 | 0 | let mut buf = [0; 2]; |
236 | 0 | try!(self.read_exact(&mut buf)); |
237 | 0 | Ok(T::read_i16(&buf)) |
238 | 0 | } |
239 | | |
240 | | #[inline] |
241 | 89.1k | fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> { |
242 | 89.1k | let mut buf = [0; 4]; |
243 | 89.1k | try!(self.read_exact(&mut buf)); |
244 | 89.1k | Ok(T::read_u32(&buf)) |
245 | 89.1k | } <bincode::de::read::IoReader<&mut flate2::zlib::bufread::ZlibDecoder<&[u8]>> as bincode::byteorder::ReadBytesExt>::read_u32::<bincode::byteorder::LittleEndian> Line | Count | Source | 241 | 89.1k | fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> { | 242 | 89.1k | let mut buf = [0; 4]; | 243 | 89.1k | try!(self.read_exact(&mut buf)); | 244 | 89.1k | Ok(T::read_u32(&buf)) | 245 | 89.1k | } |
Unexecuted instantiation: <bincode::de::read::IoReader<&[u8]> as bincode::byteorder::ReadBytesExt>::read_u32::<bincode::byteorder::LittleEndian> Unexecuted instantiation: <_ as bincode::byteorder::ReadBytesExt>::read_u32::<_> |
246 | | |
247 | | #[inline] |
248 | 0 | fn read_i32<T: ByteOrder>(&mut self) -> Result<i32> { |
249 | 0 | let mut buf = [0; 4]; |
250 | 0 | try!(self.read_exact(&mut buf)); |
251 | 0 | Ok(T::read_i32(&buf)) |
252 | 0 | } |
253 | | |
254 | | #[inline] |
255 | 271k | fn read_u64<T: ByteOrder>(&mut self) -> Result<u64> { |
256 | 271k | let mut buf = [0; 8]; |
257 | 271k | try!(self.read_exact(&mut buf)); |
258 | 271k | Ok(T::read_u64(&buf)) |
259 | 271k | } <bincode::de::read::IoReader<&mut flate2::zlib::bufread::ZlibDecoder<&[u8]>> as bincode::byteorder::ReadBytesExt>::read_u64::<bincode::byteorder::LittleEndian> Line | Count | Source | 255 | 266k | fn read_u64<T: ByteOrder>(&mut self) -> Result<u64> { | 256 | 266k | let mut buf = [0; 8]; | 257 | 266k | try!(self.read_exact(&mut buf)); | 258 | 266k | Ok(T::read_u64(&buf)) | 259 | 266k | } |
<bincode::de::read::IoReader<&[u8]> as bincode::byteorder::ReadBytesExt>::read_u64::<bincode::byteorder::LittleEndian> Line | Count | Source | 255 | 5.40k | fn read_u64<T: ByteOrder>(&mut self) -> Result<u64> { | 256 | 5.40k | let mut buf = [0; 8]; | 257 | 5.40k | try!(self.read_exact(&mut buf)); | 258 | 5.40k | Ok(T::read_u64(&buf)) | 259 | 5.40k | } |
Unexecuted instantiation: <_ as bincode::byteorder::ReadBytesExt>::read_u64::<_> |
260 | | |
261 | | #[inline] |
262 | 0 | fn read_i64<T: ByteOrder>(&mut self) -> Result<i64> { |
263 | 0 | let mut buf = [0; 8]; |
264 | 0 | try!(self.read_exact(&mut buf)); |
265 | 0 | Ok(T::read_i64(&buf)) |
266 | 0 | } |
267 | | |
268 | | #[inline] |
269 | 0 | fn read_f32<T: ByteOrder>(&mut self) -> Result<f32> { |
270 | 0 | let mut buf = [0; 4]; |
271 | 0 | try!(self.read_exact(&mut buf)); |
272 | 0 | Ok(T::read_f32(&buf)) |
273 | 0 | } |
274 | | |
275 | | #[inline] |
276 | 0 | fn read_f64<T: ByteOrder>(&mut self) -> Result<f64> { |
277 | 0 | let mut buf = [0; 8]; |
278 | 0 | try!(self.read_exact(&mut buf)); |
279 | 0 | Ok(T::read_f64(&buf)) |
280 | 0 | } |
281 | | |
282 | | serde_if_integer128! { |
283 | | #[inline] |
284 | 0 | fn read_u128<T: ByteOrder>(&mut self) -> Result<u128> { |
285 | 0 | let mut buf = [0; 16]; |
286 | 0 | try!(self.read_exact(&mut buf)); |
287 | 0 | Ok(T::read_u128(&buf)) |
288 | 0 | } |
289 | | |
290 | | #[inline] |
291 | 0 | fn read_i128<T: ByteOrder>(&mut self) -> Result<i128> { |
292 | 0 | let mut buf = [0; 16]; |
293 | 0 | try!(self.read_exact(&mut buf)); |
294 | 0 | Ok(T::read_i128(&buf)) |
295 | 0 | } |
296 | | } |
297 | | } |
298 | | |
299 | | impl<R: io::Read + ?Sized> ReadBytesExt for R {} |
300 | | |
301 | | pub trait WriteBytesExt: io::Write { |
302 | | #[inline] |
303 | 0 | fn write_u8(&mut self, n: u8) -> Result<()> { |
304 | 0 | self.write_all(&[n]) |
305 | 0 | } Unexecuted instantiation: <&mut alloc::vec::Vec<u8> as bincode::byteorder::WriteBytesExt>::write_u8 Unexecuted instantiation: <&mut flate2::zlib::write::ZlibEncoder<&mut alloc::vec::Vec<u8>> as bincode::byteorder::WriteBytesExt>::write_u8 Unexecuted instantiation: <_ as bincode::byteorder::WriteBytesExt>::write_u8 |
306 | | |
307 | | #[inline] |
308 | 0 | fn write_i8(&mut self, n: i8) -> Result<()> { |
309 | 0 | self.write_all(&[n as u8]) |
310 | 0 | } |
311 | | |
312 | | #[inline] |
313 | 0 | fn write_u16<T: ByteOrder>(&mut self, n: u16) -> Result<()> { |
314 | 0 | let mut buf = [0; 2]; |
315 | 0 | T::write_u16(&mut buf, n); |
316 | 0 | self.write_all(&buf) |
317 | 0 | } |
318 | | |
319 | | #[inline] |
320 | 0 | fn write_i16<T: ByteOrder>(&mut self, n: i16) -> Result<()> { |
321 | 0 | let mut buf = [0; 2]; |
322 | 0 | T::write_i16(&mut buf, n); |
323 | 0 | self.write_all(&buf) |
324 | 0 | } |
325 | | |
326 | | #[inline] |
327 | 0 | fn write_u32<T: ByteOrder>(&mut self, n: u32) -> Result<()> { |
328 | 0 | let mut buf = [0; 4]; |
329 | 0 | T::write_u32(&mut buf, n); |
330 | 0 | self.write_all(&buf) |
331 | 0 | } Unexecuted instantiation: <&mut alloc::vec::Vec<u8> as bincode::byteorder::WriteBytesExt>::write_u32::<bincode::byteorder::LittleEndian> Unexecuted instantiation: <&mut flate2::zlib::write::ZlibEncoder<&mut alloc::vec::Vec<u8>> as bincode::byteorder::WriteBytesExt>::write_u32::<bincode::byteorder::LittleEndian> Unexecuted instantiation: <_ as bincode::byteorder::WriteBytesExt>::write_u32::<_> |
332 | | |
333 | | #[inline] |
334 | 0 | fn write_i32<T: ByteOrder>(&mut self, n: i32) -> Result<()> { |
335 | 0 | let mut buf = [0; 4]; |
336 | 0 | T::write_i32(&mut buf, n); |
337 | 0 | self.write_all(&buf) |
338 | 0 | } |
339 | | |
340 | | #[inline] |
341 | 0 | fn write_u64<T: ByteOrder>(&mut self, n: u64) -> Result<()> { |
342 | 0 | let mut buf = [0; 8]; |
343 | 0 | T::write_u64(&mut buf, n); |
344 | 0 | self.write_all(&buf) |
345 | 0 | } Unexecuted instantiation: <&mut alloc::vec::Vec<u8> as bincode::byteorder::WriteBytesExt>::write_u64::<bincode::byteorder::LittleEndian> Unexecuted instantiation: <&mut flate2::zlib::write::ZlibEncoder<&mut alloc::vec::Vec<u8>> as bincode::byteorder::WriteBytesExt>::write_u64::<bincode::byteorder::LittleEndian> Unexecuted instantiation: <_ as bincode::byteorder::WriteBytesExt>::write_u64::<_> |
346 | | |
347 | | #[inline] |
348 | 0 | fn write_i64<T: ByteOrder>(&mut self, n: i64) -> Result<()> { |
349 | 0 | let mut buf = [0; 8]; |
350 | 0 | T::write_i64(&mut buf, n); |
351 | 0 | self.write_all(&buf) |
352 | 0 | } |
353 | | |
354 | | #[inline] |
355 | 0 | fn write_f32<T: ByteOrder>(&mut self, n: f32) -> Result<()> { |
356 | 0 | let mut buf = [0; 4]; |
357 | 0 | T::write_f32(&mut buf, n); |
358 | 0 | self.write_all(&buf) |
359 | 0 | } |
360 | | |
361 | | #[inline] |
362 | 0 | fn write_f64<T: ByteOrder>(&mut self, n: f64) -> Result<()> { |
363 | 0 | let mut buf = [0; 8]; |
364 | 0 | T::write_f64(&mut buf, n); |
365 | 0 | self.write_all(&buf) |
366 | 0 | } |
367 | | |
368 | | serde_if_integer128! { |
369 | | #[inline] |
370 | 0 | fn write_u128<T: ByteOrder>(&mut self, n: u128) -> Result<()> { |
371 | 0 | let mut buf = [0; 16]; |
372 | 0 | T::write_u128(&mut buf, n); |
373 | 0 | self.write_all(&buf) |
374 | 0 | } |
375 | | |
376 | | #[inline] |
377 | 0 | fn write_i128<T: ByteOrder>(&mut self, n: i128) -> Result<()> { |
378 | 0 | let mut buf = [0; 16]; |
379 | 0 | T::write_i128(&mut buf, n); |
380 | 0 | self.write_all(&buf) |
381 | 0 | } |
382 | | } |
383 | | } |
384 | | |
385 | | impl<W: io::Write + ?Sized> WriteBytesExt for W {} |