Coverage Report

Created: 2025-10-10 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/byteorder-lite-0.1.0/src/lib.rs
Line
Count
Source
1
/*!
2
3
This crate is a fork of the [`byteorder`] crate which sets
4
`#![forbid(unsafe_code)]`. It includes all traits and most methods from the
5
original crate, but the `ReadBytesExt::read_*_into` family of methods had to be
6
removed because they currently cannot be implemented without unsafe code.
7
8
The organization of the crate is pretty simple. A trait, [`ByteOrder`], specifies
9
byte conversion methods for each type of number in Rust (sans numbers that have
10
a platform dependent size like `usize` and `isize`). Two types, [`BigEndian`]
11
and [`LittleEndian`] implement these methods. Finally, [`ReadBytesExt`] and
12
[`WriteBytesExt`] provide convenience methods available to all types that
13
implement [`Read`] and [`Write`].
14
15
An alias, [`NetworkEndian`], for [`BigEndian`] is provided to help improve
16
code clarity.
17
18
An additional alias, [`NativeEndian`], is provided for the endianness of the
19
local platform. This is convenient when serializing data for use and
20
conversions are not desired.
21
22
# Examples
23
24
Read unsigned 16 bit big-endian integers from a [`Read`] type:
25
26
```rust
27
use std::io::Cursor;
28
use byteorder_lite::{BigEndian, ReadBytesExt};
29
30
let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
31
// Note that we use type parameters to indicate which kind of byte order
32
// we want!
33
assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
34
assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());
35
```
36
37
Write unsigned 16 bit little-endian integers to a [`Write`] type:
38
39
```rust
40
use byteorder_lite::{LittleEndian, WriteBytesExt};
41
42
let mut wtr = vec![];
43
wtr.write_u16::<LittleEndian>(517).unwrap();
44
wtr.write_u16::<LittleEndian>(768).unwrap();
45
assert_eq!(wtr, vec![5, 2, 0, 3]);
46
```
47
48
# Optional Features
49
50
This crate can also be used without the standard library.
51
52
# Alternatives
53
54
The standard numeric types provide built-in methods like `to_le_bytes` and
55
`from_le_bytes`, which support some of the same use cases.
56
57
[`byteorder`]: https://crates.io/crates/byteorder
58
[`ByteOrder`]: trait.ByteOrder.html
59
[`BigEndian`]: enum.BigEndian.html
60
[`LittleEndian`]: enum.LittleEndian.html
61
[`ReadBytesExt`]: trait.ReadBytesExt.html
62
[`WriteBytesExt`]: trait.WriteBytesExt.html
63
[`NetworkEndian`]: type.NetworkEndian.html
64
[`NativeEndian`]: type.NativeEndian.html
65
[`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
66
[`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
67
*/
68
69
#![forbid(unsafe_code)]
70
#![deny(missing_docs)]
71
#![cfg_attr(not(feature = "std"), no_std)]
72
// When testing under miri, we disable tests that take too long. But this
73
// provokes lots of dead code warnings. So we just squash them.
74
#![cfg_attr(miri, allow(dead_code, unused_macros))]
75
76
use core::{fmt::Debug, hash::Hash};
77
78
#[cfg(feature = "std")]
79
pub use crate::io::{ReadBytesExt, WriteBytesExt};
80
81
#[cfg(feature = "std")]
82
mod io;
83
84
#[inline]
85
0
fn extend_sign(val: u64, nbytes: usize) -> i64 {
86
0
    let shift = (8 - nbytes) * 8;
87
0
    (val << shift) as i64 >> shift
88
0
}
89
90
#[inline]
91
0
fn extend_sign128(val: u128, nbytes: usize) -> i128 {
92
0
    let shift = (16 - nbytes) * 8;
93
0
    (val << shift) as i128 >> shift
94
0
}
95
96
#[inline]
97
0
fn unextend_sign(val: i64, nbytes: usize) -> u64 {
98
0
    let shift = (8 - nbytes) * 8;
99
0
    (val << shift) as u64 >> shift
100
0
}
101
102
#[inline]
103
0
fn unextend_sign128(val: i128, nbytes: usize) -> u128 {
104
0
    let shift = (16 - nbytes) * 8;
105
0
    (val << shift) as u128 >> shift
106
0
}
107
108
#[inline]
109
0
fn pack_size(n: u64) -> usize {
110
0
    (8 - ((n | 1).leading_zeros() >> 3)) as usize
111
0
}
112
113
#[inline]
114
0
fn pack_size128(n: u128) -> usize {
115
0
    (16 - ((n | 1).leading_zeros() >> 3)) as usize
116
0
}
117
118
mod private {
119
    /// Sealed stops crates other than byteorder from implementing any traits
120
    /// that use it.
121
    pub trait Sealed {}
122
    impl Sealed for super::LittleEndian {}
123
    impl Sealed for super::BigEndian {}
124
}
125
126
/// `ByteOrder` describes types that can serialize integers as bytes.
127
///
128
/// Note that `Self` does not appear anywhere in this trait's definition!
129
/// Therefore, in order to use it, you'll need to use syntax like
130
/// `T::read_u16(&[0, 1])` where `T` implements `ByteOrder`.
131
///
132
/// This crate provides two types that implement `ByteOrder`: [`BigEndian`]
133
/// and [`LittleEndian`].
134
/// This trait is sealed and cannot be implemented for callers to avoid
135
/// breaking backwards compatibility when adding new derived traits.
136
///
137
/// # Examples
138
///
139
/// Write and read `u32` numbers in little endian order:
140
///
141
/// ```rust
142
/// use byteorder_lite::{ByteOrder, LittleEndian};
143
///
144
/// let mut buf = [0; 4];
145
/// LittleEndian::write_u32(&mut buf, 1_000_000);
146
/// assert_eq!(1_000_000, LittleEndian::read_u32(&buf));
147
/// ```
148
///
149
/// Write and read `i16` numbers in big endian order:
150
///
151
/// ```rust
152
/// use byteorder_lite::{ByteOrder, BigEndian};
153
///
154
/// let mut buf = [0; 2];
155
/// BigEndian::write_i16(&mut buf, -5_000);
156
/// assert_eq!(-5_000, BigEndian::read_i16(&buf));
157
/// ```
158
///
159
/// [`BigEndian`]: enum.BigEndian.html
160
/// [`LittleEndian`]: enum.LittleEndian.html
161
pub trait ByteOrder:
162
    Clone
163
    + Copy
164
    + Debug
165
    + Default
166
    + Eq
167
    + Hash
168
    + Ord
169
    + PartialEq
170
    + PartialOrd
171
    + private::Sealed
172
{
173
    /// Reads an unsigned 16 bit integer from `buf`.
174
    ///
175
    /// # Panics
176
    ///
177
    /// Panics when `buf.len() < 2`.
178
    fn read_u16(buf: &[u8]) -> u16;
179
180
    /// Reads an unsigned 24 bit integer from `buf`, stored in u32.
181
    ///
182
    /// # Panics
183
    ///
184
    /// Panics when `buf.len() < 3`.
185
    ///
186
    /// # Examples
187
    ///
188
    /// Write and read 24 bit `u32` numbers in little endian order:
189
    ///
190
    /// ```rust
191
    /// use byteorder_lite::{ByteOrder, LittleEndian};
192
    ///
193
    /// let mut buf = [0; 3];
194
    /// LittleEndian::write_u24(&mut buf, 1_000_000);
195
    /// assert_eq!(1_000_000, LittleEndian::read_u24(&buf));
196
    /// ```
197
1.39k
    fn read_u24(buf: &[u8]) -> u32 {
198
1.39k
        Self::read_uint(buf, 3) as u32
199
1.39k
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Line
Count
Source
197
1.39k
    fn read_u24(buf: &[u8]) -> u32 {
198
1.39k
        Self::read_uint(buf, 3) as u32
199
1.39k
    }
Unexecuted instantiation: <_ as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u24
200
201
    /// Reads an unsigned 32 bit integer from `buf`.
202
    ///
203
    /// # Panics
204
    ///
205
    /// Panics when `buf.len() < 4`.
206
    ///
207
    /// # Examples
208
    ///
209
    /// Write and read `u32` numbers in little endian order:
210
    ///
211
    /// ```rust
212
    /// use byteorder_lite::{ByteOrder, LittleEndian};
213
    ///
214
    /// let mut buf = [0; 4];
215
    /// LittleEndian::write_u32(&mut buf, 1_000_000);
216
    /// assert_eq!(1_000_000, LittleEndian::read_u32(&buf));
217
    /// ```
218
    fn read_u32(buf: &[u8]) -> u32;
219
220
    /// Reads an unsigned 48 bit integer from `buf`, stored in u64.
221
    ///
222
    /// # Panics
223
    ///
224
    /// Panics when `buf.len() < 6`.
225
    ///
226
    /// # Examples
227
    ///
228
    /// Write and read 48 bit `u64` numbers in little endian order:
229
    ///
230
    /// ```rust
231
    /// use byteorder_lite::{ByteOrder, LittleEndian};
232
    ///
233
    /// let mut buf = [0; 6];
234
    /// LittleEndian::write_u48(&mut buf, 1_000_000_000_000);
235
    /// assert_eq!(1_000_000_000_000, LittleEndian::read_u48(&buf));
236
    /// ```
237
0
    fn read_u48(buf: &[u8]) -> u64 {
238
0
        Self::read_uint(buf, 6) as u64
239
0
    }
240
241
    /// Reads an unsigned 64 bit integer from `buf`.
242
    ///
243
    /// # Panics
244
    ///
245
    /// Panics when `buf.len() < 8`.
246
    ///
247
    /// # Examples
248
    ///
249
    /// Write and read `u64` numbers in little endian order:
250
    ///
251
    /// ```rust
252
    /// use byteorder_lite::{ByteOrder, LittleEndian};
253
    ///
254
    /// let mut buf = [0; 8];
255
    /// LittleEndian::write_u64(&mut buf, 1_000_000);
256
    /// assert_eq!(1_000_000, LittleEndian::read_u64(&buf));
257
    /// ```
258
    fn read_u64(buf: &[u8]) -> u64;
259
260
    /// Reads an unsigned 128 bit integer from `buf`.
261
    ///
262
    /// # Panics
263
    ///
264
    /// Panics when `buf.len() < 16`.
265
    ///
266
    /// # Examples
267
    ///
268
    /// Write and read `u128` numbers in little endian order:
269
    ///
270
    /// ```rust
271
    /// use byteorder_lite::{ByteOrder, LittleEndian};
272
    ///
273
    /// let mut buf = [0; 16];
274
    /// LittleEndian::write_u128(&mut buf, 1_000_000);
275
    /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf));
276
    /// ```
277
    fn read_u128(buf: &[u8]) -> u128;
278
279
    /// Reads an unsigned n-bytes integer from `buf`.
280
    ///
281
    /// # Panics
282
    ///
283
    /// Panics when `nbytes < 1` or `nbytes > 8` or
284
    /// `buf.len() < nbytes`
285
    ///
286
    /// # Examples
287
    ///
288
    /// Write and read an n-byte number in little endian order:
289
    ///
290
    /// ```rust
291
    /// use byteorder_lite::{ByteOrder, LittleEndian};
292
    ///
293
    /// let mut buf = [0; 3];
294
    /// LittleEndian::write_uint(&mut buf, 1_000_000, 3);
295
    /// assert_eq!(1_000_000, LittleEndian::read_uint(&buf, 3));
296
    /// ```
297
    fn read_uint(buf: &[u8], nbytes: usize) -> u64;
298
299
    /// Reads an unsigned n-bytes integer from `buf`.
300
    ///
301
    /// # Panics
302
    ///
303
    /// Panics when `nbytes < 1` or `nbytes > 16` or
304
    /// `buf.len() < nbytes`
305
    ///
306
    /// # Examples
307
    ///
308
    /// Write and read an n-byte number in little endian order:
309
    ///
310
    /// ```rust
311
    /// use byteorder_lite::{ByteOrder, LittleEndian};
312
    ///
313
    /// let mut buf = [0; 3];
314
    /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3);
315
    /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3));
316
    /// ```
317
    fn read_uint128(buf: &[u8], nbytes: usize) -> u128;
318
319
    /// Writes an unsigned 16 bit integer `n` to `buf`.
320
    ///
321
    /// # Panics
322
    ///
323
    /// Panics when `buf.len() < 2`.
324
    ///
325
    /// # Examples
326
    ///
327
    /// Write and read `u16` numbers in little endian order:
328
    ///
329
    /// ```rust
330
    /// use byteorder_lite::{ByteOrder, LittleEndian};
331
    ///
332
    /// let mut buf = [0; 2];
333
    /// LittleEndian::write_u16(&mut buf, 1_000);
334
    /// assert_eq!(1_000, LittleEndian::read_u16(&buf));
335
    /// ```
336
    fn write_u16(buf: &mut [u8], n: u16);
337
338
    /// Writes an unsigned 24 bit integer `n` to `buf`, stored in u32.
339
    ///
340
    /// # Panics
341
    ///
342
    /// Panics when `buf.len() < 3`.
343
    ///
344
    /// # Examples
345
    ///
346
    /// Write and read 24 bit `u32` numbers in little endian order:
347
    ///
348
    /// ```rust
349
    /// use byteorder_lite::{ByteOrder, LittleEndian};
350
    ///
351
    /// let mut buf = [0; 3];
352
    /// LittleEndian::write_u24(&mut buf, 1_000_000);
353
    /// assert_eq!(1_000_000, LittleEndian::read_u24(&buf));
354
    /// ```
355
0
    fn write_u24(buf: &mut [u8], n: u32) {
356
0
        Self::write_uint(buf, n as u64, 3)
357
0
    }
358
359
    /// Writes an unsigned 32 bit integer `n` to `buf`.
360
    ///
361
    /// # Panics
362
    ///
363
    /// Panics when `buf.len() < 4`.
364
    ///
365
    /// # Examples
366
    ///
367
    /// Write and read `u32` numbers in little endian order:
368
    ///
369
    /// ```rust
370
    /// use byteorder_lite::{ByteOrder, LittleEndian};
371
    ///
372
    /// let mut buf = [0; 4];
373
    /// LittleEndian::write_u32(&mut buf, 1_000_000);
374
    /// assert_eq!(1_000_000, LittleEndian::read_u32(&buf));
375
    /// ```
376
    fn write_u32(buf: &mut [u8], n: u32);
377
378
    /// Writes an unsigned 48 bit integer `n` to `buf`, stored in u64.
379
    ///
380
    /// # Panics
381
    ///
382
    /// Panics when `buf.len() < 6`.
383
    ///
384
    /// # Examples
385
    ///
386
    /// Write and read 48 bit `u64` numbers in little endian order:
387
    ///
388
    /// ```rust
389
    /// use byteorder_lite::{ByteOrder, LittleEndian};
390
    ///
391
    /// let mut buf = [0; 6];
392
    /// LittleEndian::write_u48(&mut buf, 1_000_000_000_000);
393
    /// assert_eq!(1_000_000_000_000, LittleEndian::read_u48(&buf));
394
    /// ```
395
0
    fn write_u48(buf: &mut [u8], n: u64) {
396
0
        Self::write_uint(buf, n as u64, 6)
397
0
    }
398
399
    /// Writes an unsigned 64 bit integer `n` to `buf`.
400
    ///
401
    /// # Panics
402
    ///
403
    /// Panics when `buf.len() < 8`.
404
    ///
405
    /// # Examples
406
    ///
407
    /// Write and read `u64` numbers in little endian order:
408
    ///
409
    /// ```rust
410
    /// use byteorder_lite::{ByteOrder, LittleEndian};
411
    ///
412
    /// let mut buf = [0; 8];
413
    /// LittleEndian::write_u64(&mut buf, 1_000_000);
414
    /// assert_eq!(1_000_000, LittleEndian::read_u64(&buf));
415
    /// ```
416
    fn write_u64(buf: &mut [u8], n: u64);
417
418
    /// Writes an unsigned 128 bit integer `n` to `buf`.
419
    ///
420
    /// # Panics
421
    ///
422
    /// Panics when `buf.len() < 16`.
423
    ///
424
    /// # Examples
425
    ///
426
    /// Write and read `u128` numbers in little endian order:
427
    ///
428
    /// ```rust
429
    /// use byteorder_lite::{ByteOrder, LittleEndian};
430
    ///
431
    /// let mut buf = [0; 16];
432
    /// LittleEndian::write_u128(&mut buf, 1_000_000);
433
    /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf));
434
    /// ```
435
    fn write_u128(buf: &mut [u8], n: u128);
436
437
    /// Writes an unsigned integer `n` to `buf` using only `nbytes`.
438
    ///
439
    /// # Panics
440
    ///
441
    /// If `n` is not representable in `nbytes`, or if `nbytes` is `> 8`, then
442
    /// this method panics.
443
    ///
444
    /// # Examples
445
    ///
446
    /// Write and read an n-byte number in little endian order:
447
    ///
448
    /// ```rust
449
    /// use byteorder_lite::{ByteOrder, LittleEndian};
450
    ///
451
    /// let mut buf = [0; 3];
452
    /// LittleEndian::write_uint(&mut buf, 1_000_000, 3);
453
    /// assert_eq!(1_000_000, LittleEndian::read_uint(&buf, 3));
454
    /// ```
455
    fn write_uint(buf: &mut [u8], n: u64, nbytes: usize);
456
457
    /// Writes an unsigned integer `n` to `buf` using only `nbytes`.
458
    ///
459
    /// # Panics
460
    ///
461
    /// If `n` is not representable in `nbytes`, or if `nbytes` is `> 16`, then
462
    /// this method panics.
463
    ///
464
    /// # Examples
465
    ///
466
    /// Write and read an n-byte number in little endian order:
467
    ///
468
    /// ```rust
469
    /// use byteorder_lite::{ByteOrder, LittleEndian};
470
    ///
471
    /// let mut buf = [0; 3];
472
    /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3);
473
    /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3));
474
    /// ```
475
    fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize);
476
477
    /// Reads a signed 16 bit integer from `buf`.
478
    ///
479
    /// # Panics
480
    ///
481
    /// Panics when `buf.len() < 2`.
482
    ///
483
    /// # Examples
484
    ///
485
    /// Write and read `i16` numbers in little endian order:
486
    ///
487
    /// ```rust
488
    /// use byteorder_lite::{ByteOrder, LittleEndian};
489
    ///
490
    /// let mut buf = [0; 2];
491
    /// LittleEndian::write_i16(&mut buf, -1_000);
492
    /// assert_eq!(-1_000, LittleEndian::read_i16(&buf));
493
    /// ```
494
    #[inline]
495
0
    fn read_i16(buf: &[u8]) -> i16 {
496
0
        Self::read_u16(buf) as i16
497
0
    }
498
499
    /// Reads a signed 24 bit integer from `buf`, stored in i32.
500
    ///
501
    /// # Panics
502
    ///
503
    /// Panics when `buf.len() < 3`.
504
    ///
505
    /// # Examples
506
    ///
507
    /// Write and read 24 bit `i32` numbers in little endian order:
508
    ///
509
    /// ```rust
510
    /// use byteorder_lite::{ByteOrder, LittleEndian};
511
    ///
512
    /// let mut buf = [0; 3];
513
    /// LittleEndian::write_i24(&mut buf, -1_000_000);
514
    /// assert_eq!(-1_000_000, LittleEndian::read_i24(&buf));
515
    /// ```
516
    #[inline]
517
0
    fn read_i24(buf: &[u8]) -> i32 {
518
0
        Self::read_int(buf, 3) as i32
519
0
    }
520
521
    /// Reads a signed 32 bit integer from `buf`.
522
    ///
523
    /// # Panics
524
    ///
525
    /// Panics when `buf.len() < 4`.
526
    ///
527
    /// # Examples
528
    ///
529
    /// Write and read `i32` numbers in little endian order:
530
    ///
531
    /// ```rust
532
    /// use byteorder_lite::{ByteOrder, LittleEndian};
533
    ///
534
    /// let mut buf = [0; 4];
535
    /// LittleEndian::write_i32(&mut buf, -1_000_000);
536
    /// assert_eq!(-1_000_000, LittleEndian::read_i32(&buf));
537
    /// ```
538
    #[inline]
539
6.57k
    fn read_i32(buf: &[u8]) -> i32 {
540
6.57k
        Self::read_u32(buf) as i32
541
6.57k
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Unexecuted instantiation: <_ as byteorder_lite::ByteOrder>::read_i32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Line
Count
Source
539
2.87k
    fn read_i32(buf: &[u8]) -> i32 {
540
2.87k
        Self::read_u32(buf) as i32
541
2.87k
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Line
Count
Source
539
3.70k
    fn read_i32(buf: &[u8]) -> i32 {
540
3.70k
        Self::read_u32(buf) as i32
541
3.70k
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_i32
542
543
    /// Reads a signed 48 bit integer from `buf`, stored in i64.
544
    ///
545
    /// # Panics
546
    ///
547
    /// Panics when `buf.len() < 6`.
548
    ///
549
    /// # Examples
550
    ///
551
    /// Write and read 48 bit `i64` numbers in little endian order:
552
    ///
553
    /// ```rust
554
    /// use byteorder_lite::{ByteOrder, LittleEndian};
555
    ///
556
    /// let mut buf = [0; 6];
557
    /// LittleEndian::write_i48(&mut buf, -1_000_000_000_000);
558
    /// assert_eq!(-1_000_000_000_000, LittleEndian::read_i48(&buf));
559
    /// ```
560
    #[inline]
561
0
    fn read_i48(buf: &[u8]) -> i64 {
562
0
        Self::read_int(buf, 6) as i64
563
0
    }
564
565
    /// Reads a signed 64 bit integer from `buf`.
566
    ///
567
    /// # Panics
568
    ///
569
    /// Panics when `buf.len() < 8`.
570
    ///
571
    /// # Examples
572
    ///
573
    /// Write and read `i64` numbers in little endian order:
574
    ///
575
    /// ```rust
576
    /// use byteorder_lite::{ByteOrder, LittleEndian};
577
    ///
578
    /// let mut buf = [0; 8];
579
    /// LittleEndian::write_i64(&mut buf, -1_000_000_000);
580
    /// assert_eq!(-1_000_000_000, LittleEndian::read_i64(&buf));
581
    /// ```
582
    #[inline]
583
0
    fn read_i64(buf: &[u8]) -> i64 {
584
0
        Self::read_u64(buf) as i64
585
0
    }
586
587
    /// Reads a signed 128 bit integer from `buf`.
588
    ///
589
    /// # Panics
590
    ///
591
    /// Panics when `buf.len() < 16`.
592
    ///
593
    /// # Examples
594
    ///
595
    /// Write and read `i128` numbers in little endian order:
596
    ///
597
    /// ```rust
598
    /// use byteorder_lite::{ByteOrder, LittleEndian};
599
    ///
600
    /// let mut buf = [0; 16];
601
    /// LittleEndian::write_i128(&mut buf, -1_000_000_000);
602
    /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf));
603
    /// ```
604
    #[inline]
605
0
    fn read_i128(buf: &[u8]) -> i128 {
606
0
        Self::read_u128(buf) as i128
607
0
    }
608
609
    /// Reads a signed n-bytes integer from `buf`.
610
    ///
611
    /// # Panics
612
    ///
613
    /// Panics when `nbytes < 1` or `nbytes > 8` or
614
    /// `buf.len() < nbytes`
615
    ///
616
    /// # Examples
617
    ///
618
    /// Write and read n-length signed numbers in little endian order:
619
    ///
620
    /// ```rust
621
    /// use byteorder_lite::{ByteOrder, LittleEndian};
622
    ///
623
    /// let mut buf = [0; 3];
624
    /// LittleEndian::write_int(&mut buf, -1_000, 3);
625
    /// assert_eq!(-1_000, LittleEndian::read_int(&buf, 3));
626
    /// ```
627
    #[inline]
628
0
    fn read_int(buf: &[u8], nbytes: usize) -> i64 {
629
0
        extend_sign(Self::read_uint(buf, nbytes), nbytes)
630
0
    }
631
632
    /// Reads a signed n-bytes integer from `buf`.
633
    ///
634
    /// # Panics
635
    ///
636
    /// Panics when `nbytes < 1` or `nbytes > 16` or
637
    /// `buf.len() < nbytes`
638
    ///
639
    /// # Examples
640
    ///
641
    /// Write and read n-length signed numbers in little endian order:
642
    ///
643
    /// ```rust
644
    /// use byteorder_lite::{ByteOrder, LittleEndian};
645
    ///
646
    /// let mut buf = [0; 3];
647
    /// LittleEndian::write_int128(&mut buf, -1_000, 3);
648
    /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3));
649
    /// ```
650
    #[inline]
651
0
    fn read_int128(buf: &[u8], nbytes: usize) -> i128 {
652
0
        extend_sign128(Self::read_uint128(buf, nbytes), nbytes)
653
0
    }
654
655
    /// Reads a IEEE754 single-precision (4 bytes) floating point number.
656
    ///
657
    /// # Panics
658
    ///
659
    /// Panics when `buf.len() < 4`.
660
    ///
661
    /// # Examples
662
    ///
663
    /// Write and read `f32` numbers in little endian order:
664
    ///
665
    /// ```rust
666
    /// use byteorder_lite::{ByteOrder, LittleEndian};
667
    ///
668
    /// let e = 2.71828;
669
    /// let mut buf = [0; 4];
670
    /// LittleEndian::write_f32(&mut buf, e);
671
    /// assert_eq!(e, LittleEndian::read_f32(&buf));
672
    /// ```
673
    #[inline]
674
0
    fn read_f32(buf: &[u8]) -> f32 {
675
0
        f32::from_bits(Self::read_u32(buf))
676
0
    }
677
678
    /// Reads a IEEE754 double-precision (8 bytes) floating point number.
679
    ///
680
    /// # Panics
681
    ///
682
    /// Panics when `buf.len() < 8`.
683
    ///
684
    /// # Examples
685
    ///
686
    /// Write and read `f64` numbers in little endian order:
687
    ///
688
    /// ```rust
689
    /// use byteorder_lite::{ByteOrder, LittleEndian};
690
    ///
691
    /// let phi = 1.6180339887;
692
    /// let mut buf = [0; 8];
693
    /// LittleEndian::write_f64(&mut buf, phi);
694
    /// assert_eq!(phi, LittleEndian::read_f64(&buf));
695
    /// ```
696
    #[inline]
697
0
    fn read_f64(buf: &[u8]) -> f64 {
698
0
        f64::from_bits(Self::read_u64(buf))
699
0
    }
700
701
    /// Writes a signed 16 bit integer `n` to `buf`.
702
    ///
703
    /// # Panics
704
    ///
705
    /// Panics when `buf.len() < 2`.
706
    ///
707
    /// # Examples
708
    ///
709
    /// Write and read `i16` numbers in little endian order:
710
    ///
711
    /// ```rust
712
    /// use byteorder_lite::{ByteOrder, LittleEndian};
713
    ///
714
    /// let mut buf = [0; 2];
715
    /// LittleEndian::write_i16(&mut buf, -1_000);
716
    /// assert_eq!(-1_000, LittleEndian::read_i16(&buf));
717
    /// ```
718
    #[inline]
719
0
    fn write_i16(buf: &mut [u8], n: i16) {
720
0
        Self::write_u16(buf, n as u16)
721
0
    }
722
723
    /// Writes a signed 24 bit integer `n` to `buf`, stored in i32.
724
    ///
725
    /// # Panics
726
    ///
727
    /// Panics when `buf.len() < 3`.
728
    ///
729
    /// # Examples
730
    ///
731
    /// Write and read 24 bit `i32` numbers in little endian order:
732
    ///
733
    /// ```rust
734
    /// use byteorder_lite::{ByteOrder, LittleEndian};
735
    ///
736
    /// let mut buf = [0; 3];
737
    /// LittleEndian::write_i24(&mut buf, -1_000_000);
738
    /// assert_eq!(-1_000_000, LittleEndian::read_i24(&buf));
739
    /// ```
740
    #[inline]
741
0
    fn write_i24(buf: &mut [u8], n: i32) {
742
0
        Self::write_int(buf, n as i64, 3)
743
0
    }
744
745
    /// Writes a signed 32 bit integer `n` to `buf`.
746
    ///
747
    /// # Panics
748
    ///
749
    /// Panics when `buf.len() < 4`.
750
    ///
751
    /// # Examples
752
    ///
753
    /// Write and read `i32` numbers in little endian order:
754
    ///
755
    /// ```rust
756
    /// use byteorder_lite::{ByteOrder, LittleEndian};
757
    ///
758
    /// let mut buf = [0; 4];
759
    /// LittleEndian::write_i32(&mut buf, -1_000_000);
760
    /// assert_eq!(-1_000_000, LittleEndian::read_i32(&buf));
761
    /// ```
762
    #[inline]
763
0
    fn write_i32(buf: &mut [u8], n: i32) {
764
0
        Self::write_u32(buf, n as u32)
765
0
    }
Unexecuted instantiation: <_ as byteorder_lite::ByteOrder>::write_i32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_i32
766
767
    /// Writes a signed 48 bit integer `n` to `buf`, stored in i64.
768
    ///
769
    /// # Panics
770
    ///
771
    /// Panics when `buf.len() < 6`.
772
    ///
773
    /// # Examples
774
    ///
775
    /// Write and read 48 bit `i64` numbers in little endian order:
776
    ///
777
    /// ```rust
778
    /// use byteorder_lite::{ByteOrder, LittleEndian};
779
    ///
780
    /// let mut buf = [0; 6];
781
    /// LittleEndian::write_i48(&mut buf, -1_000_000_000_000);
782
    /// assert_eq!(-1_000_000_000_000, LittleEndian::read_i48(&buf));
783
    /// ```
784
    #[inline]
785
0
    fn write_i48(buf: &mut [u8], n: i64) {
786
0
        Self::write_int(buf, n as i64, 6)
787
0
    }
788
789
    /// Writes a signed 64 bit integer `n` to `buf`.
790
    ///
791
    /// # Panics
792
    ///
793
    /// Panics when `buf.len() < 8`.
794
    ///
795
    /// # Examples
796
    ///
797
    /// Write and read `i64` numbers in little endian order:
798
    ///
799
    /// ```rust
800
    /// use byteorder_lite::{ByteOrder, LittleEndian};
801
    ///
802
    /// let mut buf = [0; 8];
803
    /// LittleEndian::write_i64(&mut buf, -1_000_000_000);
804
    /// assert_eq!(-1_000_000_000, LittleEndian::read_i64(&buf));
805
    /// ```
806
    #[inline]
807
0
    fn write_i64(buf: &mut [u8], n: i64) {
808
0
        Self::write_u64(buf, n as u64)
809
0
    }
810
811
    /// Writes a signed 128 bit integer `n` to `buf`.
812
    ///
813
    /// # Panics
814
    ///
815
    /// Panics when `buf.len() < 16`.
816
    ///
817
    /// # Examples
818
    ///
819
    /// Write and read n-byte `i128` numbers in little endian order:
820
    ///
821
    /// ```rust
822
    /// use byteorder_lite::{ByteOrder, LittleEndian};
823
    ///
824
    /// let mut buf = [0; 16];
825
    /// LittleEndian::write_i128(&mut buf, -1_000_000_000);
826
    /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf));
827
    /// ```
828
    #[inline]
829
0
    fn write_i128(buf: &mut [u8], n: i128) {
830
0
        Self::write_u128(buf, n as u128)
831
0
    }
832
833
    /// Writes a signed integer `n` to `buf` using only `nbytes`.
834
    ///
835
    /// # Panics
836
    ///
837
    /// If `n` is not representable in `nbytes`, or if `nbytes` is `> 8`, then
838
    /// this method panics.
839
    ///
840
    /// # Examples
841
    ///
842
    /// Write and read an n-byte number in little endian order:
843
    ///
844
    /// ```rust
845
    /// use byteorder_lite::{ByteOrder, LittleEndian};
846
    ///
847
    /// let mut buf = [0; 3];
848
    /// LittleEndian::write_int(&mut buf, -1_000, 3);
849
    /// assert_eq!(-1_000, LittleEndian::read_int(&buf, 3));
850
    /// ```
851
    #[inline]
852
0
    fn write_int(buf: &mut [u8], n: i64, nbytes: usize) {
853
0
        Self::write_uint(buf, unextend_sign(n, nbytes), nbytes)
854
0
    }
855
856
    /// Writes a signed integer `n` to `buf` using only `nbytes`.
857
    ///
858
    /// # Panics
859
    ///
860
    /// If `n` is not representable in `nbytes`, or if `nbytes` is `> 16`, then
861
    /// this method panics.
862
    ///
863
    /// # Examples
864
    ///
865
    /// Write and read n-length signed numbers in little endian order:
866
    ///
867
    /// ```rust
868
    /// use byteorder_lite::{ByteOrder, LittleEndian};
869
    ///
870
    /// let mut buf = [0; 3];
871
    /// LittleEndian::write_int128(&mut buf, -1_000, 3);
872
    /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3));
873
    /// ```
874
    #[inline]
875
0
    fn write_int128(buf: &mut [u8], n: i128, nbytes: usize) {
876
0
        Self::write_uint128(buf, unextend_sign128(n, nbytes), nbytes)
877
0
    }
878
879
    /// Writes a IEEE754 single-precision (4 bytes) floating point number.
880
    ///
881
    /// # Panics
882
    ///
883
    /// Panics when `buf.len() < 4`.
884
    ///
885
    /// # Examples
886
    ///
887
    /// Write and read `f32` numbers in little endian order:
888
    ///
889
    /// ```rust
890
    /// use byteorder_lite::{ByteOrder, LittleEndian};
891
    ///
892
    /// let e = 2.71828;
893
    /// let mut buf = [0; 4];
894
    /// LittleEndian::write_f32(&mut buf, e);
895
    /// assert_eq!(e, LittleEndian::read_f32(&buf));
896
    /// ```
897
    #[inline]
898
0
    fn write_f32(buf: &mut [u8], n: f32) {
899
0
        Self::write_u32(buf, n.to_bits())
900
0
    }
901
902
    /// Writes a IEEE754 double-precision (8 bytes) floating point number.
903
    ///
904
    /// # Panics
905
    ///
906
    /// Panics when `buf.len() < 8`.
907
    ///
908
    /// # Examples
909
    ///
910
    /// Write and read `f64` numbers in little endian order:
911
    ///
912
    /// ```rust
913
    /// use byteorder_lite::{ByteOrder, LittleEndian};
914
    ///
915
    /// let phi = 1.6180339887;
916
    /// let mut buf = [0; 8];
917
    /// LittleEndian::write_f64(&mut buf, phi);
918
    /// assert_eq!(phi, LittleEndian::read_f64(&buf));
919
    /// ```
920
    #[inline]
921
0
    fn write_f64(buf: &mut [u8], n: f64) {
922
0
        Self::write_u64(buf, n.to_bits())
923
0
    }
924
925
    /// Reads unsigned 16 bit integers from `src` into `dst`.
926
    ///
927
    /// # Panics
928
    ///
929
    /// Panics when `src.len() != 2*dst.len()`.
930
    ///
931
    /// # Examples
932
    ///
933
    /// Write and read `u16` numbers in little endian order:
934
    ///
935
    /// ```rust
936
    /// use byteorder_lite::{ByteOrder, LittleEndian};
937
    ///
938
    /// let mut bytes = [0; 8];
939
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
940
    /// LittleEndian::write_u16_into(&numbers_given, &mut bytes);
941
    ///
942
    /// let mut numbers_got = [0; 4];
943
    /// LittleEndian::read_u16_into(&bytes, &mut numbers_got);
944
    /// assert_eq!(numbers_given, numbers_got);
945
    /// ```
946
    fn read_u16_into(src: &[u8], dst: &mut [u16]);
947
948
    /// Reads unsigned 32 bit integers from `src` into `dst`.
949
    ///
950
    /// # Panics
951
    ///
952
    /// Panics when `src.len() != 4*dst.len()`.
953
    ///
954
    /// # Examples
955
    ///
956
    /// Write and read `u32` numbers in little endian order:
957
    ///
958
    /// ```rust
959
    /// use byteorder_lite::{ByteOrder, LittleEndian};
960
    ///
961
    /// let mut bytes = [0; 16];
962
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
963
    /// LittleEndian::write_u32_into(&numbers_given, &mut bytes);
964
    ///
965
    /// let mut numbers_got = [0; 4];
966
    /// LittleEndian::read_u32_into(&bytes, &mut numbers_got);
967
    /// assert_eq!(numbers_given, numbers_got);
968
    /// ```
969
    fn read_u32_into(src: &[u8], dst: &mut [u32]);
970
971
    /// Reads unsigned 64 bit integers from `src` into `dst`.
972
    ///
973
    /// # Panics
974
    ///
975
    /// Panics when `src.len() != 8*dst.len()`.
976
    ///
977
    /// # Examples
978
    ///
979
    /// Write and read `u64` numbers in little endian order:
980
    ///
981
    /// ```rust
982
    /// use byteorder_lite::{ByteOrder, LittleEndian};
983
    ///
984
    /// let mut bytes = [0; 32];
985
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
986
    /// LittleEndian::write_u64_into(&numbers_given, &mut bytes);
987
    ///
988
    /// let mut numbers_got = [0; 4];
989
    /// LittleEndian::read_u64_into(&bytes, &mut numbers_got);
990
    /// assert_eq!(numbers_given, numbers_got);
991
    /// ```
992
    fn read_u64_into(src: &[u8], dst: &mut [u64]);
993
994
    /// Reads unsigned 128 bit integers from `src` into `dst`.
995
    ///
996
    /// # Panics
997
    ///
998
    /// Panics when `src.len() != 16*dst.len()`.
999
    ///
1000
    /// # Examples
1001
    ///
1002
    /// Write and read `u128` numbers in little endian order:
1003
    ///
1004
    /// ```rust
1005
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1006
    ///
1007
    /// let mut bytes = [0; 64];
1008
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1009
    /// LittleEndian::write_u128_into(&numbers_given, &mut bytes);
1010
    ///
1011
    /// let mut numbers_got = [0; 4];
1012
    /// LittleEndian::read_u128_into(&bytes, &mut numbers_got);
1013
    /// assert_eq!(numbers_given, numbers_got);
1014
    /// ```
1015
    fn read_u128_into(src: &[u8], dst: &mut [u128]);
1016
1017
    /// Reads signed 16 bit integers from `src` to `dst`.
1018
    ///
1019
    /// # Panics
1020
    ///
1021
    /// Panics when `buf.len() != 2*dst.len()`.
1022
    ///
1023
    /// # Examples
1024
    ///
1025
    /// Write and read `i16` numbers in little endian order:
1026
    ///
1027
    /// ```rust
1028
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1029
    ///
1030
    /// let mut bytes = [0; 8];
1031
    /// let numbers_given = [1, 2, 0x0f, 0xee];
1032
    /// LittleEndian::write_i16_into(&numbers_given, &mut bytes);
1033
    ///
1034
    /// let mut numbers_got = [0; 4];
1035
    /// LittleEndian::read_i16_into(&bytes, &mut numbers_got);
1036
    /// assert_eq!(numbers_given, numbers_got);
1037
    /// ```
1038
    fn read_i16_into(src: &[u8], dst: &mut [i16]);
1039
1040
    /// Reads signed 32 bit integers from `src` into `dst`.
1041
    ///
1042
    /// # Panics
1043
    ///
1044
    /// Panics when `src.len() != 4*dst.len()`.
1045
    ///
1046
    /// # Examples
1047
    ///
1048
    /// Write and read `i32` numbers in little endian order:
1049
    ///
1050
    /// ```rust
1051
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1052
    ///
1053
    /// let mut bytes = [0; 16];
1054
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1055
    /// LittleEndian::write_i32_into(&numbers_given, &mut bytes);
1056
    ///
1057
    /// let mut numbers_got = [0; 4];
1058
    /// LittleEndian::read_i32_into(&bytes, &mut numbers_got);
1059
    /// assert_eq!(numbers_given, numbers_got);
1060
    /// ```
1061
    fn read_i32_into(src: &[u8], dst: &mut [i32]);
1062
1063
    /// Reads signed 64 bit integers from `src` into `dst`.
1064
    ///
1065
    /// # Panics
1066
    ///
1067
    /// Panics when `src.len() != 8*dst.len()`.
1068
    ///
1069
    /// # Examples
1070
    ///
1071
    /// Write and read `i64` numbers in little endian order:
1072
    ///
1073
    /// ```rust
1074
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1075
    ///
1076
    /// let mut bytes = [0; 32];
1077
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1078
    /// LittleEndian::write_i64_into(&numbers_given, &mut bytes);
1079
    ///
1080
    /// let mut numbers_got = [0; 4];
1081
    /// LittleEndian::read_i64_into(&bytes, &mut numbers_got);
1082
    /// assert_eq!(numbers_given, numbers_got);
1083
    /// ```
1084
    fn read_i64_into(src: &[u8], dst: &mut [i64]);
1085
1086
    /// Reads signed 128 bit integers from `src` into `dst`.
1087
    ///
1088
    /// # Panics
1089
    ///
1090
    /// Panics when `src.len() != 16*dst.len()`.
1091
    ///
1092
    /// # Examples
1093
    ///
1094
    /// Write and read `i128` numbers in little endian order:
1095
    ///
1096
    /// ```rust
1097
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1098
    ///
1099
    /// let mut bytes = [0; 64];
1100
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1101
    /// LittleEndian::write_i128_into(&numbers_given, &mut bytes);
1102
    ///
1103
    /// let mut numbers_got = [0; 4];
1104
    /// LittleEndian::read_i128_into(&bytes, &mut numbers_got);
1105
    /// assert_eq!(numbers_given, numbers_got);
1106
    /// ```
1107
    fn read_i128_into(src: &[u8], dst: &mut [i128]);
1108
1109
    /// Reads IEEE754 single-precision (4 bytes) floating point numbers from
1110
    /// `src` into `dst`.
1111
    ///
1112
    /// # Panics
1113
    ///
1114
    /// Panics when `src.len() != 4*dst.len()`.
1115
    ///
1116
    /// # Examples
1117
    ///
1118
    /// Write and read `f32` numbers in little endian order:
1119
    ///
1120
    /// ```rust
1121
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1122
    ///
1123
    /// let mut bytes = [0; 16];
1124
    /// let numbers_given = [1.0, 2.0, 31.312e31, -11.32e19];
1125
    /// LittleEndian::write_f32_into(&numbers_given, &mut bytes);
1126
    ///
1127
    /// let mut numbers_got = [0.0; 4];
1128
    /// LittleEndian::read_f32_into(&bytes, &mut numbers_got);
1129
    /// assert_eq!(numbers_given, numbers_got);
1130
    /// ```
1131
    fn read_f32_into(src: &[u8], dst: &mut [f32]);
1132
1133
    /// **DEPRECATED**.
1134
    ///
1135
    /// This method is deprecated. Use `read_f32_into` instead.
1136
    /// Reads IEEE754 single-precision (4 bytes) floating point numbers from
1137
    /// `src` into `dst`.
1138
    ///
1139
    /// # Panics
1140
    ///
1141
    /// Panics when `src.len() != 4*dst.len()`.
1142
    ///
1143
    /// # Examples
1144
    ///
1145
    /// Write and read `f32` numbers in little endian order:
1146
    ///
1147
    /// ```rust
1148
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1149
    ///
1150
    /// let mut bytes = [0; 16];
1151
    /// let numbers_given = [1.0, 2.0, 31.312e31, -11.32e19];
1152
    /// LittleEndian::write_f32_into(&numbers_given, &mut bytes);
1153
    ///
1154
    /// let mut numbers_got = [0.0; 4];
1155
    /// LittleEndian::read_f32_into_unchecked(&bytes, &mut numbers_got);
1156
    /// assert_eq!(numbers_given, numbers_got);
1157
    /// ```
1158
    #[inline]
1159
    #[deprecated(since = "1.3.0", note = "please use `read_f32_into` instead")]
1160
0
    fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) {
1161
0
        Self::read_f32_into(src, dst);
1162
0
    }
1163
1164
    /// Reads IEEE754 single-precision (4 bytes) floating point numbers from
1165
    /// `src` into `dst`.
1166
    ///
1167
    /// # Panics
1168
    ///
1169
    /// Panics when `src.len() != 8*dst.len()`.
1170
    ///
1171
    /// # Examples
1172
    ///
1173
    /// Write and read `f64` numbers in little endian order:
1174
    ///
1175
    /// ```rust
1176
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1177
    ///
1178
    /// let mut bytes = [0; 32];
1179
    /// let numbers_given = [1.0, 2.0, 31.312e211, -11.32e91];
1180
    /// LittleEndian::write_f64_into(&numbers_given, &mut bytes);
1181
    ///
1182
    /// let mut numbers_got = [0.0; 4];
1183
    /// LittleEndian::read_f64_into(&bytes, &mut numbers_got);
1184
    /// assert_eq!(numbers_given, numbers_got);
1185
    /// ```
1186
    fn read_f64_into(src: &[u8], dst: &mut [f64]);
1187
1188
    /// **DEPRECATED**.
1189
    ///
1190
    /// This method is deprecated. Use `read_f64_into` instead.
1191
    ///
1192
    /// Reads IEEE754 single-precision (4 bytes) floating point numbers from
1193
    /// `src` into `dst`.
1194
    ///
1195
    /// # Panics
1196
    ///
1197
    /// Panics when `src.len() != 8*dst.len()`.
1198
    ///
1199
    /// # Examples
1200
    ///
1201
    /// Write and read `f64` numbers in little endian order:
1202
    ///
1203
    /// ```rust
1204
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1205
    ///
1206
    /// let mut bytes = [0; 32];
1207
    /// let numbers_given = [1.0, 2.0, 31.312e211, -11.32e91];
1208
    /// LittleEndian::write_f64_into(&numbers_given, &mut bytes);
1209
    ///
1210
    /// let mut numbers_got = [0.0; 4];
1211
    /// LittleEndian::read_f64_into_unchecked(&bytes, &mut numbers_got);
1212
    /// assert_eq!(numbers_given, numbers_got);
1213
    /// ```
1214
    #[inline]
1215
    #[deprecated(since = "1.3.0", note = "please use `read_f64_into` instead")]
1216
0
    fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) {
1217
0
        Self::read_f64_into(src, dst);
1218
0
    }
1219
1220
    /// Writes unsigned 16 bit integers from `src` into `dst`.
1221
    ///
1222
    /// # Panics
1223
    ///
1224
    /// Panics when `dst.len() != 2*src.len()`.
1225
    ///
1226
    /// # Examples
1227
    ///
1228
    /// Write and read `u16` numbers in little endian order:
1229
    ///
1230
    /// ```rust
1231
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1232
    ///
1233
    /// let mut bytes = [0; 8];
1234
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1235
    /// LittleEndian::write_u16_into(&numbers_given, &mut bytes);
1236
    ///
1237
    /// let mut numbers_got = [0; 4];
1238
    /// LittleEndian::read_u16_into(&bytes, &mut numbers_got);
1239
    /// assert_eq!(numbers_given, numbers_got);
1240
    /// ```
1241
    fn write_u16_into(src: &[u16], dst: &mut [u8]);
1242
1243
    /// Writes unsigned 32 bit integers from `src` into `dst`.
1244
    ///
1245
    /// # Panics
1246
    ///
1247
    /// Panics when `dst.len() != 4*src.len()`.
1248
    ///
1249
    /// # Examples
1250
    ///
1251
    /// Write and read `u32` numbers in little endian order:
1252
    ///
1253
    /// ```rust
1254
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1255
    ///
1256
    /// let mut bytes = [0; 16];
1257
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1258
    /// LittleEndian::write_u32_into(&numbers_given, &mut bytes);
1259
    ///
1260
    /// let mut numbers_got = [0; 4];
1261
    /// LittleEndian::read_u32_into(&bytes, &mut numbers_got);
1262
    /// assert_eq!(numbers_given, numbers_got);
1263
    /// ```
1264
    fn write_u32_into(src: &[u32], dst: &mut [u8]);
1265
1266
    /// Writes unsigned 64 bit integers from `src` into `dst`.
1267
    ///
1268
    /// # Panics
1269
    ///
1270
    /// Panics when `dst.len() != 8*src.len()`.
1271
    ///
1272
    /// # Examples
1273
    ///
1274
    /// Write and read `u64` numbers in little endian order:
1275
    ///
1276
    /// ```rust
1277
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1278
    ///
1279
    /// let mut bytes = [0; 32];
1280
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1281
    /// LittleEndian::write_u64_into(&numbers_given, &mut bytes);
1282
    ///
1283
    /// let mut numbers_got = [0; 4];
1284
    /// LittleEndian::read_u64_into(&bytes, &mut numbers_got);
1285
    /// assert_eq!(numbers_given, numbers_got);
1286
    /// ```
1287
    fn write_u64_into(src: &[u64], dst: &mut [u8]);
1288
1289
    /// Writes unsigned 128 bit integers from `src` into `dst`.
1290
    ///
1291
    /// # Panics
1292
    ///
1293
    /// Panics when `dst.len() != 16*src.len()`.
1294
    ///
1295
    /// # Examples
1296
    ///
1297
    /// Write and read `u128` numbers in little endian order:
1298
    ///
1299
    /// ```rust
1300
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1301
    ///
1302
    /// let mut bytes = [0; 64];
1303
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1304
    /// LittleEndian::write_u128_into(&numbers_given, &mut bytes);
1305
    ///
1306
    /// let mut numbers_got = [0; 4];
1307
    /// LittleEndian::read_u128_into(&bytes, &mut numbers_got);
1308
    /// assert_eq!(numbers_given, numbers_got);
1309
    /// ```
1310
    fn write_u128_into(src: &[u128], dst: &mut [u8]);
1311
1312
    /// Writes signed 8 bit integers from `src` into `dst`.
1313
    ///
1314
    /// Note that since each `i8` is a single byte, no byte order conversions
1315
    /// are used. This method is included because it provides a safe, simple
1316
    /// way for the caller to write from a `&[i8]` buffer. (Without this
1317
    /// method, the caller would have to either use `unsafe` code or convert
1318
    /// each byte to `u8` individually.)
1319
    ///
1320
    /// # Panics
1321
    ///
1322
    /// Panics when `buf.len() != src.len()`.
1323
    ///
1324
    /// # Examples
1325
    ///
1326
    /// Write and read `i8` numbers in little endian order:
1327
    ///
1328
    /// ```rust
1329
    /// use byteorder_lite::{ByteOrder, LittleEndian, ReadBytesExt};
1330
    ///
1331
    /// let mut bytes = [0; 4];
1332
    /// let numbers_given = [1, 2, 0xf, 0xe];
1333
    /// LittleEndian::write_i8_into(&numbers_given, &mut bytes);
1334
    /// ```
1335
0
    fn write_i8_into(src: &[i8], dst: &mut [u8]) {
1336
0
        assert_eq!(src.len(), dst.len());
1337
0
        for (d, s) in dst.iter_mut().zip(src.iter()) {
1338
0
            *d = *s as u8;
1339
0
        }
1340
0
    }
1341
1342
    /// Writes signed 16 bit integers from `src` into `dst`.
1343
    ///
1344
    /// # Panics
1345
    ///
1346
    /// Panics when `buf.len() != 2*src.len()`.
1347
    ///
1348
    /// # Examples
1349
    ///
1350
    /// Write and read `i16` numbers in little endian order:
1351
    ///
1352
    /// ```rust
1353
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1354
    ///
1355
    /// let mut bytes = [0; 8];
1356
    /// let numbers_given = [1, 2, 0x0f, 0xee];
1357
    /// LittleEndian::write_i16_into(&numbers_given, &mut bytes);
1358
    ///
1359
    /// let mut numbers_got = [0; 4];
1360
    /// LittleEndian::read_i16_into(&bytes, &mut numbers_got);
1361
    /// assert_eq!(numbers_given, numbers_got);
1362
    /// ```
1363
    fn write_i16_into(src: &[i16], dst: &mut [u8]);
1364
1365
    /// Writes signed 32 bit integers from `src` into `dst`.
1366
    ///
1367
    /// # Panics
1368
    ///
1369
    /// Panics when `dst.len() != 4*src.len()`.
1370
    ///
1371
    /// # Examples
1372
    ///
1373
    /// Write and read `i32` numbers in little endian order:
1374
    ///
1375
    /// ```rust
1376
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1377
    ///
1378
    /// let mut bytes = [0; 16];
1379
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1380
    /// LittleEndian::write_i32_into(&numbers_given, &mut bytes);
1381
    ///
1382
    /// let mut numbers_got = [0; 4];
1383
    /// LittleEndian::read_i32_into(&bytes, &mut numbers_got);
1384
    /// assert_eq!(numbers_given, numbers_got);
1385
    /// ```
1386
    fn write_i32_into(src: &[i32], dst: &mut [u8]);
1387
1388
    /// Writes signed 64 bit integers from `src` into `dst`.
1389
    ///
1390
    /// # Panics
1391
    ///
1392
    /// Panics when `dst.len() != 8*src.len()`.
1393
    ///
1394
    /// # Examples
1395
    ///
1396
    /// Write and read `i64` numbers in little endian order:
1397
    ///
1398
    /// ```rust
1399
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1400
    ///
1401
    /// let mut bytes = [0; 32];
1402
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1403
    /// LittleEndian::write_i64_into(&numbers_given, &mut bytes);
1404
    ///
1405
    /// let mut numbers_got = [0; 4];
1406
    /// LittleEndian::read_i64_into(&bytes, &mut numbers_got);
1407
    /// assert_eq!(numbers_given, numbers_got);
1408
    /// ```
1409
    fn write_i64_into(src: &[i64], dst: &mut [u8]);
1410
1411
    /// Writes signed 128 bit integers from `src` into `dst`.
1412
    ///
1413
    /// # Panics
1414
    ///
1415
    /// Panics when `dst.len() != 16*src.len()`.
1416
    ///
1417
    /// # Examples
1418
    ///
1419
    /// Write and read `i128` numbers in little endian order:
1420
    ///
1421
    /// ```rust
1422
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1423
    ///
1424
    /// let mut bytes = [0; 64];
1425
    /// let numbers_given = [1, 2, 0xf00f, 0xffee];
1426
    /// LittleEndian::write_i128_into(&numbers_given, &mut bytes);
1427
    ///
1428
    /// let mut numbers_got = [0; 4];
1429
    /// LittleEndian::read_i128_into(&bytes, &mut numbers_got);
1430
    /// assert_eq!(numbers_given, numbers_got);
1431
    /// ```
1432
    fn write_i128_into(src: &[i128], dst: &mut [u8]);
1433
1434
    /// Writes IEEE754 single-precision (4 bytes) floating point numbers from
1435
    /// `src` into `dst`.
1436
    ///
1437
    /// # Panics
1438
    ///
1439
    /// Panics when `src.len() != 4*dst.len()`.
1440
    ///
1441
    /// # Examples
1442
    ///
1443
    /// Write and read `f32` numbers in little endian order:
1444
    ///
1445
    /// ```rust
1446
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1447
    ///
1448
    /// let mut bytes = [0; 16];
1449
    /// let numbers_given = [1.0, 2.0, 31.312e31, -11.32e19];
1450
    /// LittleEndian::write_f32_into(&numbers_given, &mut bytes);
1451
    ///
1452
    /// let mut numbers_got = [0.0; 4];
1453
    /// LittleEndian::read_f32_into(&bytes, &mut numbers_got);
1454
    /// assert_eq!(numbers_given, numbers_got);
1455
    /// ```
1456
    fn write_f32_into(src: &[f32], dst: &mut [u8]);
1457
1458
    /// Writes IEEE754 double-precision (8 bytes) floating point numbers from
1459
    /// `src` into `dst`.
1460
    ///
1461
    /// # Panics
1462
    ///
1463
    /// Panics when `src.len() != 8*dst.len()`.
1464
    ///
1465
    /// # Examples
1466
    ///
1467
    /// Write and read `f64` numbers in little endian order:
1468
    ///
1469
    /// ```rust
1470
    /// use byteorder_lite::{ByteOrder, LittleEndian};
1471
    ///
1472
    /// let mut bytes = [0; 32];
1473
    /// let numbers_given = [1.0, 2.0, 31.312e211, -11.32e91];
1474
    /// LittleEndian::write_f64_into(&numbers_given, &mut bytes);
1475
    ///
1476
    /// let mut numbers_got = [0.0; 4];
1477
    /// LittleEndian::read_f64_into(&bytes, &mut numbers_got);
1478
    /// assert_eq!(numbers_given, numbers_got);
1479
    /// ```
1480
    fn write_f64_into(src: &[f64], dst: &mut [u8]);
1481
1482
    /// Converts the given slice of unsigned 16 bit integers to a particular
1483
    /// endianness.
1484
    ///
1485
    /// If the endianness matches the endianness of the host platform, then
1486
    /// this is a no-op.
1487
    ///
1488
    /// # Examples
1489
    ///
1490
    /// Convert the host platform's endianness to big-endian:
1491
    ///
1492
    /// ```rust
1493
    /// use byteorder_lite::{ByteOrder, BigEndian};
1494
    ///
1495
    /// let mut numbers = [5, 65000];
1496
    /// BigEndian::from_slice_u16(&mut numbers);
1497
    /// assert_eq!(numbers, [5u16.to_be(), 65000u16.to_be()]);
1498
    /// ```
1499
    fn from_slice_u16(numbers: &mut [u16]);
1500
1501
    /// Converts the given slice of unsigned 32 bit integers to a particular
1502
    /// endianness.
1503
    ///
1504
    /// If the endianness matches the endianness of the host platform, then
1505
    /// this is a no-op.
1506
    ///
1507
    /// # Examples
1508
    ///
1509
    /// Convert the host platform's endianness to big-endian:
1510
    ///
1511
    /// ```rust
1512
    /// use byteorder_lite::{ByteOrder, BigEndian};
1513
    ///
1514
    /// let mut numbers = [5, 65000];
1515
    /// BigEndian::from_slice_u32(&mut numbers);
1516
    /// assert_eq!(numbers, [5u32.to_be(), 65000u32.to_be()]);
1517
    /// ```
1518
    fn from_slice_u32(numbers: &mut [u32]);
1519
1520
    /// Converts the given slice of unsigned 64 bit integers to a particular
1521
    /// endianness.
1522
    ///
1523
    /// If the endianness matches the endianness of the host platform, then
1524
    /// this is a no-op.
1525
    ///
1526
    /// # Examples
1527
    ///
1528
    /// Convert the host platform's endianness to big-endian:
1529
    ///
1530
    /// ```rust
1531
    /// use byteorder_lite::{ByteOrder, BigEndian};
1532
    ///
1533
    /// let mut numbers = [5, 65000];
1534
    /// BigEndian::from_slice_u64(&mut numbers);
1535
    /// assert_eq!(numbers, [5u64.to_be(), 65000u64.to_be()]);
1536
    /// ```
1537
    fn from_slice_u64(numbers: &mut [u64]);
1538
1539
    /// Converts the given slice of unsigned 128 bit integers to a particular
1540
    /// endianness.
1541
    ///
1542
    /// If the endianness matches the endianness of the host platform, then
1543
    /// this is a no-op.
1544
    ///
1545
    /// # Examples
1546
    ///
1547
    /// Convert the host platform's endianness to big-endian:
1548
    ///
1549
    /// ```rust
1550
    /// use byteorder_lite::{ByteOrder, BigEndian};
1551
    ///
1552
    /// let mut numbers = [5, 65000];
1553
    /// BigEndian::from_slice_u128(&mut numbers);
1554
    /// assert_eq!(numbers, [5u128.to_be(), 65000u128.to_be()]);
1555
    /// ```
1556
    fn from_slice_u128(numbers: &mut [u128]);
1557
1558
    /// Converts the given slice of signed 16 bit integers to a particular
1559
    /// endianness.
1560
    ///
1561
    /// If the endianness matches the endianness of the host platform, then
1562
    /// this is a no-op.
1563
    ///
1564
    /// # Examples
1565
    ///
1566
    /// Convert the host platform's endianness to big-endian:
1567
    ///
1568
    /// ```rust
1569
    /// use byteorder_lite::{ByteOrder, BigEndian};
1570
    ///
1571
    /// let mut numbers = [5, 6500];
1572
    /// BigEndian::from_slice_i16(&mut numbers);
1573
    /// assert_eq!(numbers, [5i16.to_be(), 6500i16.to_be()]);
1574
    /// ```
1575
    fn from_slice_i16(src: &mut [i16]);
1576
1577
    /// Converts the given slice of signed 32 bit integers to a particular
1578
    /// endianness.
1579
    ///
1580
    /// If the endianness matches the endianness of the host platform, then
1581
    /// this is a no-op.
1582
    ///
1583
    /// # Examples
1584
    ///
1585
    /// Convert the host platform's endianness to big-endian:
1586
    ///
1587
    /// ```rust
1588
    /// use byteorder_lite::{ByteOrder, BigEndian};
1589
    ///
1590
    /// let mut numbers = [5, 65000];
1591
    /// BigEndian::from_slice_i32(&mut numbers);
1592
    /// assert_eq!(numbers, [5i32.to_be(), 65000i32.to_be()]);
1593
    /// ```
1594
    fn from_slice_i32(src: &mut [i32]);
1595
1596
    /// Converts the given slice of signed 64 bit integers to a particular
1597
    /// endianness.
1598
    ///
1599
    /// If the endianness matches the endianness of the host platform, then
1600
    /// this is a no-op.
1601
    ///
1602
    /// # Examples
1603
    ///
1604
    /// Convert the host platform's endianness to big-endian:
1605
    ///
1606
    /// ```rust
1607
    /// use byteorder_lite::{ByteOrder, BigEndian};
1608
    ///
1609
    /// let mut numbers = [5, 65000];
1610
    /// BigEndian::from_slice_i64(&mut numbers);
1611
    /// assert_eq!(numbers, [5i64.to_be(), 65000i64.to_be()]);
1612
    /// ```
1613
    fn from_slice_i64(src: &mut [i64]);
1614
1615
    /// Converts the given slice of signed 128 bit integers to a particular
1616
    /// endianness.
1617
    ///
1618
    /// If the endianness matches the endianness of the host platform, then
1619
    /// this is a no-op.
1620
    ///
1621
    /// # Examples
1622
    ///
1623
    /// Convert the host platform's endianness to big-endian:
1624
    ///
1625
    /// ```rust
1626
    /// use byteorder_lite::{ByteOrder, BigEndian};
1627
    ///
1628
    /// let mut numbers = [5, 65000];
1629
    /// BigEndian::from_slice_i128(&mut numbers);
1630
    /// assert_eq!(numbers, [5i128.to_be(), 65000i128.to_be()]);
1631
    /// ```
1632
    fn from_slice_i128(src: &mut [i128]);
1633
1634
    /// Converts the given slice of IEEE754 single-precision (4 bytes) floating
1635
    /// point numbers to a particular endianness.
1636
    ///
1637
    /// If the endianness matches the endianness of the host platform, then
1638
    /// this is a no-op.
1639
    fn from_slice_f32(numbers: &mut [f32]);
1640
1641
    /// Converts the given slice of IEEE754 double-precision (8 bytes) floating
1642
    /// point numbers to a particular endianness.
1643
    ///
1644
    /// If the endianness matches the endianness of the host platform, then
1645
    /// this is a no-op.
1646
    fn from_slice_f64(numbers: &mut [f64]);
1647
}
1648
1649
/// Defines big-endian serialization.
1650
///
1651
/// Note that this type has no value constructor. It is used purely at the
1652
/// type level.
1653
///
1654
/// # Examples
1655
///
1656
/// Write and read `u32` numbers in big endian order:
1657
///
1658
/// ```rust
1659
/// use byteorder_lite::{ByteOrder, BigEndian};
1660
///
1661
/// let mut buf = [0; 4];
1662
/// BigEndian::write_u32(&mut buf, 1_000_000);
1663
/// assert_eq!(1_000_000, BigEndian::read_u32(&buf));
1664
/// ```
1665
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1666
pub enum BigEndian {}
1667
1668
impl Default for BigEndian {
1669
0
    fn default() -> BigEndian {
1670
0
        panic!("BigEndian default")
1671
    }
1672
}
1673
1674
/// A type alias for [`BigEndian`].
1675
///
1676
/// [`BigEndian`]: enum.BigEndian.html
1677
pub type BE = BigEndian;
1678
1679
/// Defines little-endian serialization.
1680
///
1681
/// Note that this type has no value constructor. It is used purely at the
1682
/// type level.
1683
///
1684
/// # Examples
1685
///
1686
/// Write and read `u32` numbers in little endian order:
1687
///
1688
/// ```rust
1689
/// use byteorder_lite::{ByteOrder, LittleEndian};
1690
///
1691
/// let mut buf = [0; 4];
1692
/// LittleEndian::write_u32(&mut buf, 1_000_000);
1693
/// assert_eq!(1_000_000, LittleEndian::read_u32(&buf));
1694
/// ```
1695
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1696
pub enum LittleEndian {}
1697
1698
impl Default for LittleEndian {
1699
0
    fn default() -> LittleEndian {
1700
0
        panic!("LittleEndian default")
1701
    }
1702
}
1703
1704
/// A type alias for [`LittleEndian`].
1705
///
1706
/// [`LittleEndian`]: enum.LittleEndian.html
1707
pub type LE = LittleEndian;
1708
1709
/// Defines network byte order serialization.
1710
///
1711
/// Network byte order is defined by [RFC 1700][1] to be big-endian, and is
1712
/// referred to in several protocol specifications.  This type is an alias of
1713
/// [`BigEndian`].
1714
///
1715
/// [1]: https://tools.ietf.org/html/rfc1700
1716
///
1717
/// Note that this type has no value constructor. It is used purely at the
1718
/// type level.
1719
///
1720
/// # Examples
1721
///
1722
/// Write and read `i16` numbers in big endian order:
1723
///
1724
/// ```rust
1725
/// use byteorder_lite::{ByteOrder, NetworkEndian, BigEndian};
1726
///
1727
/// let mut buf = [0; 2];
1728
/// BigEndian::write_i16(&mut buf, -5_000);
1729
/// assert_eq!(-5_000, NetworkEndian::read_i16(&buf));
1730
/// ```
1731
///
1732
/// [`BigEndian`]: enum.BigEndian.html
1733
pub type NetworkEndian = BigEndian;
1734
1735
/// Defines system native-endian serialization.
1736
///
1737
/// Note that this type has no value constructor. It is used purely at the
1738
/// type level.
1739
///
1740
/// On this platform, this is an alias for [`LittleEndian`].
1741
///
1742
/// [`LittleEndian`]: enum.LittleEndian.html
1743
#[cfg(target_endian = "little")]
1744
pub type NativeEndian = LittleEndian;
1745
1746
/// Defines system native-endian serialization.
1747
///
1748
/// Note that this type has no value constructor. It is used purely at the
1749
/// type level.
1750
///
1751
/// On this platform, this is an alias for [`BigEndian`].
1752
///
1753
/// [`BigEndian`]: enum.BigEndian.html
1754
#[cfg(target_endian = "big")]
1755
pub type NativeEndian = BigEndian;
1756
1757
/// Copies a &[u8] $src into a &mut [$ty] $dst for the endianness given by
1758
/// $from_bytes (must be either from_be_bytes or from_le_bytes).
1759
///
1760
/// Panics if $src.len() != $dst.len() * size_of::<$ty>().
1761
macro_rules! read_slice {
1762
    ($src:expr, $dst:expr, $ty:ty, $from_bytes:ident) => {{
1763
        const SIZE: usize = core::mem::size_of::<$ty>();
1764
        // Check types:
1765
        let src: &[u8] = $src;
1766
        let dst: &mut [$ty] = $dst;
1767
        assert_eq!(src.len(), dst.len() * SIZE);
1768
        for (src, dst) in src.chunks_exact(SIZE).zip(dst.iter_mut()) {
1769
            *dst = <$ty>::$from_bytes(src.try_into().unwrap());
1770
        }
1771
    }};
1772
}
1773
1774
/// Copies a &[$ty] $src into a &mut [u8] $dst for the endianness given by
1775
/// $from_bytes (must be either from_be_bytes or from_le_bytes).
1776
///
1777
/// Panics if $src.len() * size_of::<$ty>() != $dst.len().
1778
macro_rules! write_slice {
1779
    ($src:expr, $dst:expr, $ty:ty, $to_bytes:ident) => {{
1780
        const SIZE: usize = core::mem::size_of::<$ty>();
1781
        // Check types:
1782
        let src: &[$ty] = $src;
1783
        let dst: &mut [u8] = $dst;
1784
        assert_eq!(src.len() * SIZE, dst.len());
1785
        for (src, dst) in src.iter().zip(dst.chunks_exact_mut(SIZE)) {
1786
            dst.copy_from_slice(&src.$to_bytes());
1787
        }
1788
    }};
1789
}
1790
1791
impl ByteOrder for BigEndian {
1792
    #[inline]
1793
602M
    fn read_u16(buf: &[u8]) -> u16 {
1794
602M
        u16::from_be_bytes(buf[..2].try_into().unwrap())
1795
602M
    }
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
<byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Line
Count
Source
1793
1.37M
    fn read_u16(buf: &[u8]) -> u16 {
1794
1.37M
        u16::from_be_bytes(buf[..2].try_into().unwrap())
1795
1.37M
    }
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
<byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
Line
Count
Source
1793
601M
    fn read_u16(buf: &[u8]) -> u16 {
1794
601M
        u16::from_be_bytes(buf[..2].try_into().unwrap())
1795
601M
    }
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u16
1796
1797
    #[inline]
1798
0
    fn read_u32(buf: &[u8]) -> u32 {
1799
0
        u32::from_be_bytes(buf[..4].try_into().unwrap())
1800
0
    }
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u32
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::read_u32
1801
1802
    #[inline]
1803
0
    fn read_u64(buf: &[u8]) -> u64 {
1804
0
        u64::from_be_bytes(buf[..8].try_into().unwrap())
1805
0
    }
1806
1807
    #[inline]
1808
0
    fn read_u128(buf: &[u8]) -> u128 {
1809
0
        u128::from_be_bytes(buf[..16].try_into().unwrap())
1810
0
    }
1811
1812
    #[inline]
1813
0
    fn read_uint(buf: &[u8], nbytes: usize) -> u64 {
1814
0
        let mut out = [0; 8];
1815
0
        assert!(1 <= nbytes && nbytes <= out.len() && nbytes <= buf.len());
1816
0
        let start = out.len() - nbytes;
1817
0
        out[start..].copy_from_slice(&buf[..nbytes]);
1818
0
        u64::from_be_bytes(out)
1819
0
    }
1820
1821
    #[inline]
1822
0
    fn read_uint128(buf: &[u8], nbytes: usize) -> u128 {
1823
0
        let mut out = [0; 16];
1824
0
        assert!(1 <= nbytes && nbytes <= out.len() && nbytes <= buf.len());
1825
0
        let start = out.len() - nbytes;
1826
0
        out[start..].copy_from_slice(&buf[..nbytes]);
1827
0
        u128::from_be_bytes(out)
1828
0
    }
1829
1830
    #[inline]
1831
0
    fn write_u16(buf: &mut [u8], n: u16) {
1832
0
        buf[..2].copy_from_slice(&n.to_be_bytes());
1833
0
    }
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::BigEndian as byteorder_lite::ByteOrder>::write_u16
1834
1835
    #[inline]
1836
0
    fn write_u32(buf: &mut [u8], n: u32) {
1837
0
        buf[..4].copy_from_slice(&n.to_be_bytes());
1838
0
    }
1839
1840
    #[inline]
1841
0
    fn write_u64(buf: &mut [u8], n: u64) {
1842
0
        buf[..8].copy_from_slice(&n.to_be_bytes());
1843
0
    }
1844
1845
    #[inline]
1846
0
    fn write_u128(buf: &mut [u8], n: u128) {
1847
0
        buf[..16].copy_from_slice(&n.to_be_bytes());
1848
0
    }
1849
1850
    #[inline]
1851
0
    fn write_uint(buf: &mut [u8], n: u64, nbytes: usize) {
1852
0
        assert!(pack_size(n) <= nbytes && nbytes <= 8);
1853
0
        assert!(nbytes <= buf.len());
1854
1855
0
        buf[..nbytes].copy_from_slice(&n.to_be_bytes()[(8 - nbytes)..]);
1856
0
    }
1857
1858
    #[inline]
1859
0
    fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) {
1860
0
        assert!(pack_size128(n) <= nbytes && nbytes <= 16);
1861
0
        assert!(nbytes <= buf.len());
1862
1863
0
        buf[..nbytes].copy_from_slice(&n.to_be_bytes()[(16 - nbytes)..]);
1864
0
    }
1865
1866
    #[inline]
1867
0
    fn read_u16_into(src: &[u8], dst: &mut [u16]) {
1868
0
        read_slice!(src, dst, u16, from_be_bytes);
1869
0
    }
1870
1871
    #[inline]
1872
0
    fn read_u32_into(src: &[u8], dst: &mut [u32]) {
1873
0
        read_slice!(src, dst, u32, from_be_bytes);
1874
0
    }
1875
1876
    #[inline]
1877
0
    fn read_u64_into(src: &[u8], dst: &mut [u64]) {
1878
0
        read_slice!(src, dst, u64, from_be_bytes);
1879
0
    }
1880
1881
    #[inline]
1882
0
    fn read_u128_into(src: &[u8], dst: &mut [u128]) {
1883
0
        read_slice!(src, dst, u128, from_be_bytes);
1884
0
    }
1885
1886
    #[inline]
1887
0
    fn read_i16_into(src: &[u8], dst: &mut [i16]) {
1888
0
        read_slice!(src, dst, i16, from_be_bytes);
1889
0
    }
1890
1891
    #[inline]
1892
0
    fn read_i32_into(src: &[u8], dst: &mut [i32]) {
1893
0
        read_slice!(src, dst, i32, from_be_bytes);
1894
0
    }
1895
1896
    #[inline]
1897
0
    fn read_i64_into(src: &[u8], dst: &mut [i64]) {
1898
0
        read_slice!(src, dst, i64, from_be_bytes);
1899
0
    }
1900
1901
    #[inline]
1902
0
    fn read_i128_into(src: &[u8], dst: &mut [i128]) {
1903
0
        read_slice!(src, dst, i128, from_be_bytes);
1904
0
    }
1905
1906
    #[inline]
1907
0
    fn read_f32_into(src: &[u8], dst: &mut [f32]) {
1908
0
        read_slice!(src, dst, f32, from_be_bytes);
1909
0
    }
1910
1911
    #[inline]
1912
0
    fn read_f64_into(src: &[u8], dst: &mut [f64]) {
1913
0
        read_slice!(src, dst, f64, from_be_bytes);
1914
0
    }
1915
1916
    #[inline]
1917
0
    fn write_u16_into(src: &[u16], dst: &mut [u8]) {
1918
0
        write_slice!(src, dst, u16, to_be_bytes);
1919
0
    }
1920
1921
    #[inline]
1922
0
    fn write_u32_into(src: &[u32], dst: &mut [u8]) {
1923
0
        write_slice!(src, dst, u32, to_be_bytes);
1924
0
    }
1925
1926
    #[inline]
1927
0
    fn write_u64_into(src: &[u64], dst: &mut [u8]) {
1928
0
        write_slice!(src, dst, u64, to_be_bytes);
1929
0
    }
1930
1931
    #[inline]
1932
0
    fn write_u128_into(src: &[u128], dst: &mut [u8]) {
1933
0
        write_slice!(src, dst, u128, to_be_bytes);
1934
0
    }
1935
1936
    #[inline]
1937
0
    fn write_i16_into(src: &[i16], dst: &mut [u8]) {
1938
0
        write_slice!(src, dst, i16, to_be_bytes);
1939
0
    }
1940
1941
    #[inline]
1942
0
    fn write_i32_into(src: &[i32], dst: &mut [u8]) {
1943
0
        write_slice!(src, dst, i32, to_be_bytes);
1944
0
    }
1945
1946
    #[inline]
1947
0
    fn write_i64_into(src: &[i64], dst: &mut [u8]) {
1948
0
        write_slice!(src, dst, i64, to_be_bytes);
1949
0
    }
1950
1951
    #[inline]
1952
0
    fn write_i128_into(src: &[i128], dst: &mut [u8]) {
1953
0
        write_slice!(src, dst, i128, to_be_bytes);
1954
0
    }
1955
1956
    #[inline]
1957
0
    fn write_f32_into(src: &[f32], dst: &mut [u8]) {
1958
0
        write_slice!(src, dst, f32, to_be_bytes);
1959
0
    }
1960
1961
    #[inline]
1962
0
    fn write_f64_into(src: &[f64], dst: &mut [u8]) {
1963
0
        write_slice!(src, dst, f64, to_be_bytes);
1964
0
    }
1965
1966
    #[inline]
1967
0
    fn from_slice_u16(numbers: &mut [u16]) {
1968
0
        if cfg!(target_endian = "little") {
1969
0
            for n in numbers {
1970
0
                *n = n.to_be();
1971
0
            }
1972
0
        }
1973
0
    }
1974
1975
    #[inline]
1976
0
    fn from_slice_u32(numbers: &mut [u32]) {
1977
0
        if cfg!(target_endian = "little") {
1978
0
            for n in numbers {
1979
0
                *n = n.to_be();
1980
0
            }
1981
0
        }
1982
0
    }
1983
1984
    #[inline]
1985
0
    fn from_slice_u64(numbers: &mut [u64]) {
1986
0
        if cfg!(target_endian = "little") {
1987
0
            for n in numbers {
1988
0
                *n = n.to_be();
1989
0
            }
1990
0
        }
1991
0
    }
1992
1993
    #[inline]
1994
0
    fn from_slice_u128(numbers: &mut [u128]) {
1995
0
        if cfg!(target_endian = "little") {
1996
0
            for n in numbers {
1997
0
                *n = n.to_be();
1998
0
            }
1999
0
        }
2000
0
    }
2001
2002
    #[inline]
2003
0
    fn from_slice_i16(numbers: &mut [i16]) {
2004
0
        if cfg!(target_endian = "little") {
2005
0
            for n in numbers {
2006
0
                *n = n.to_be();
2007
0
            }
2008
0
        }
2009
0
    }
2010
2011
    #[inline]
2012
0
    fn from_slice_i32(numbers: &mut [i32]) {
2013
0
        if cfg!(target_endian = "little") {
2014
0
            for n in numbers {
2015
0
                *n = n.to_be();
2016
0
            }
2017
0
        }
2018
0
    }
2019
2020
    #[inline]
2021
0
    fn from_slice_i64(numbers: &mut [i64]) {
2022
0
        if cfg!(target_endian = "little") {
2023
0
            for n in numbers {
2024
0
                *n = n.to_be();
2025
0
            }
2026
0
        }
2027
0
    }
2028
2029
    #[inline]
2030
0
    fn from_slice_i128(numbers: &mut [i128]) {
2031
0
        if cfg!(target_endian = "little") {
2032
0
            for n in numbers {
2033
0
                *n = n.to_be();
2034
0
            }
2035
0
        }
2036
0
    }
2037
2038
    #[inline]
2039
0
    fn from_slice_f32(numbers: &mut [f32]) {
2040
0
        if cfg!(target_endian = "little") {
2041
0
            for n in numbers {
2042
0
                *n = f32::from_bits(n.to_bits().to_be());
2043
0
            }
2044
0
        }
2045
0
    }
2046
2047
    #[inline]
2048
0
    fn from_slice_f64(numbers: &mut [f64]) {
2049
0
        if cfg!(target_endian = "little") {
2050
0
            for n in numbers {
2051
0
                *n = f64::from_bits(n.to_bits().to_be());
2052
0
            }
2053
0
        }
2054
0
    }
2055
}
2056
2057
impl ByteOrder for LittleEndian {
2058
    #[inline]
2059
3.63M
    fn read_u16(buf: &[u8]) -> u16 {
2060
3.63M
        u16::from_le_bytes(buf[..2].try_into().unwrap())
2061
3.63M
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Line
Count
Source
2059
2.05k
    fn read_u16(buf: &[u8]) -> u16 {
2060
2.05k
        u16::from_le_bytes(buf[..2].try_into().unwrap())
2061
2.05k
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Line
Count
Source
2059
2.25M
    fn read_u16(buf: &[u8]) -> u16 {
2060
2.25M
        u16::from_le_bytes(buf[..2].try_into().unwrap())
2061
2.25M
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Line
Count
Source
2059
1.38M
    fn read_u16(buf: &[u8]) -> u16 {
2060
1.38M
        u16::from_le_bytes(buf[..2].try_into().unwrap())
2061
1.38M
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u16
2062
2063
    #[inline]
2064
1.83M
    fn read_u32(buf: &[u8]) -> u32 {
2065
1.83M
        u32::from_le_bytes(buf[..4].try_into().unwrap())
2066
1.83M
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Line
Count
Source
2064
12.5k
    fn read_u32(buf: &[u8]) -> u32 {
2065
12.5k
        u32::from_le_bytes(buf[..4].try_into().unwrap())
2066
12.5k
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Line
Count
Source
2064
1.80M
    fn read_u32(buf: &[u8]) -> u32 {
2065
1.80M
        u32::from_le_bytes(buf[..4].try_into().unwrap())
2066
1.80M
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Line
Count
Source
2064
2.87k
    fn read_u32(buf: &[u8]) -> u32 {
2065
2.87k
        u32::from_le_bytes(buf[..4].try_into().unwrap())
2066
2.87k
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Line
Count
Source
2064
3.69k
    fn read_u32(buf: &[u8]) -> u32 {
2065
3.69k
        u32::from_le_bytes(buf[..4].try_into().unwrap())
2066
3.69k
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Line
Count
Source
2064
3.70k
    fn read_u32(buf: &[u8]) -> u32 {
2065
3.70k
        u32::from_le_bytes(buf[..4].try_into().unwrap())
2066
3.70k
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_u32
2067
2068
    #[inline]
2069
0
    fn read_u64(buf: &[u8]) -> u64 {
2070
0
        u64::from_le_bytes(buf[..8].try_into().unwrap())
2071
0
    }
2072
2073
    #[inline]
2074
0
    fn read_u128(buf: &[u8]) -> u128 {
2075
0
        u128::from_le_bytes(buf[..16].try_into().unwrap())
2076
0
    }
2077
2078
    #[inline]
2079
1.39k
    fn read_uint(buf: &[u8], nbytes: usize) -> u64 {
2080
1.39k
        let mut out = [0; 8];
2081
1.39k
        assert!(1 <= nbytes && nbytes <= out.len() && nbytes <= buf.len());
2082
1.39k
        out[..nbytes].copy_from_slice(&buf[..nbytes]);
2083
1.39k
        u64::from_le_bytes(out)
2084
1.39k
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Line
Count
Source
2079
1.39k
    fn read_uint(buf: &[u8], nbytes: usize) -> u64 {
2080
1.39k
        let mut out = [0; 8];
2081
1.39k
        assert!(1 <= nbytes && nbytes <= out.len() && nbytes <= buf.len());
2082
1.39k
        out[..nbytes].copy_from_slice(&buf[..nbytes]);
2083
1.39k
        u64::from_le_bytes(out)
2084
1.39k
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::read_uint
2085
2086
    #[inline]
2087
0
    fn read_uint128(buf: &[u8], nbytes: usize) -> u128 {
2088
0
        let mut out = [0; 16];
2089
0
        assert!(1 <= nbytes && nbytes <= out.len() && nbytes <= buf.len());
2090
0
        out[..nbytes].copy_from_slice(&buf[..nbytes]);
2091
0
        u128::from_le_bytes(out)
2092
0
    }
2093
2094
    #[inline]
2095
604M
    fn write_u16(buf: &mut [u8], n: u16) {
2096
604M
        buf[..2].copy_from_slice(&n.to_le_bytes());
2097
604M
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Line
Count
Source
2095
1.38M
    fn write_u16(buf: &mut [u8], n: u16) {
2096
1.38M
        buf[..2].copy_from_slice(&n.to_le_bytes());
2097
1.38M
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Line
Count
Source
2095
1.38M
    fn write_u16(buf: &mut [u8], n: u16) {
2096
1.38M
        buf[..2].copy_from_slice(&n.to_le_bytes());
2097
1.38M
    }
<byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
Line
Count
Source
2095
601M
    fn write_u16(buf: &mut [u8], n: u16) {
2096
601M
        buf[..2].copy_from_slice(&n.to_le_bytes());
2097
601M
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u16
2098
2099
    #[inline]
2100
0
    fn write_u32(buf: &mut [u8], n: u32) {
2101
0
        buf[..4].copy_from_slice(&n.to_le_bytes());
2102
0
    }
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u32
Unexecuted instantiation: <byteorder_lite::LittleEndian as byteorder_lite::ByteOrder>::write_u32
2103
2104
    #[inline]
2105
0
    fn write_u64(buf: &mut [u8], n: u64) {
2106
0
        buf[..8].copy_from_slice(&n.to_le_bytes());
2107
0
    }
2108
2109
    #[inline]
2110
0
    fn write_u128(buf: &mut [u8], n: u128) {
2111
0
        buf[..16].copy_from_slice(&n.to_le_bytes());
2112
0
    }
2113
2114
    #[inline]
2115
0
    fn write_uint(buf: &mut [u8], n: u64, nbytes: usize) {
2116
0
        assert!(pack_size(n) <= nbytes && nbytes <= 8);
2117
0
        assert!(nbytes <= buf.len());
2118
2119
0
        buf[..nbytes].copy_from_slice(&n.to_le_bytes()[..nbytes]);
2120
0
    }
2121
2122
    #[inline]
2123
0
    fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) {
2124
0
        assert!(pack_size128(n) <= nbytes && nbytes <= 16);
2125
0
        assert!(nbytes <= buf.len());
2126
2127
0
        buf[..nbytes].copy_from_slice(&n.to_le_bytes()[..nbytes]);
2128
0
    }
2129
2130
    #[inline]
2131
0
    fn read_u16_into(src: &[u8], dst: &mut [u16]) {
2132
0
        read_slice!(src, dst, u16, from_le_bytes);
2133
0
    }
2134
2135
    #[inline]
2136
0
    fn read_u32_into(src: &[u8], dst: &mut [u32]) {
2137
0
        read_slice!(src, dst, u32, from_le_bytes);
2138
0
    }
2139
2140
    #[inline]
2141
0
    fn read_u64_into(src: &[u8], dst: &mut [u64]) {
2142
0
        read_slice!(src, dst, u64, from_le_bytes);
2143
0
    }
2144
2145
    #[inline]
2146
0
    fn read_u128_into(src: &[u8], dst: &mut [u128]) {
2147
0
        read_slice!(src, dst, u128, from_le_bytes);
2148
0
    }
2149
2150
    #[inline]
2151
0
    fn read_i16_into(src: &[u8], dst: &mut [i16]) {
2152
0
        read_slice!(src, dst, i16, from_le_bytes);
2153
0
    }
2154
2155
    #[inline]
2156
0
    fn read_i32_into(src: &[u8], dst: &mut [i32]) {
2157
0
        read_slice!(src, dst, i32, from_le_bytes);
2158
0
    }
2159
2160
    #[inline]
2161
0
    fn read_i64_into(src: &[u8], dst: &mut [i64]) {
2162
0
        read_slice!(src, dst, i64, from_le_bytes);
2163
0
    }
2164
2165
    #[inline]
2166
0
    fn read_i128_into(src: &[u8], dst: &mut [i128]) {
2167
0
        read_slice!(src, dst, i128, from_le_bytes);
2168
0
    }
2169
2170
    #[inline]
2171
0
    fn read_f32_into(src: &[u8], dst: &mut [f32]) {
2172
0
        read_slice!(src, dst, f32, from_le_bytes);
2173
0
    }
2174
2175
    #[inline]
2176
0
    fn read_f64_into(src: &[u8], dst: &mut [f64]) {
2177
0
        read_slice!(src, dst, f64, from_le_bytes);
2178
0
    }
2179
2180
    #[inline]
2181
0
    fn write_u16_into(src: &[u16], dst: &mut [u8]) {
2182
0
        write_slice!(src, dst, u16, to_le_bytes);
2183
0
    }
2184
2185
    #[inline]
2186
0
    fn write_u32_into(src: &[u32], dst: &mut [u8]) {
2187
0
        write_slice!(src, dst, u32, to_le_bytes);
2188
0
    }
2189
2190
    #[inline]
2191
0
    fn write_u64_into(src: &[u64], dst: &mut [u8]) {
2192
0
        write_slice!(src, dst, u64, to_le_bytes);
2193
0
    }
2194
2195
    #[inline]
2196
0
    fn write_u128_into(src: &[u128], dst: &mut [u8]) {
2197
0
        write_slice!(src, dst, u128, to_le_bytes);
2198
0
    }
2199
2200
    #[inline]
2201
0
    fn write_i16_into(src: &[i16], dst: &mut [u8]) {
2202
0
        write_slice!(src, dst, i16, to_le_bytes);
2203
0
    }
2204
2205
    #[inline]
2206
0
    fn write_i32_into(src: &[i32], dst: &mut [u8]) {
2207
0
        write_slice!(src, dst, i32, to_le_bytes);
2208
0
    }
2209
2210
    #[inline]
2211
0
    fn write_i64_into(src: &[i64], dst: &mut [u8]) {
2212
0
        write_slice!(src, dst, i64, to_le_bytes);
2213
0
    }
2214
2215
    #[inline]
2216
0
    fn write_i128_into(src: &[i128], dst: &mut [u8]) {
2217
0
        write_slice!(src, dst, i128, to_le_bytes);
2218
0
    }
2219
2220
    #[inline]
2221
0
    fn write_f32_into(src: &[f32], dst: &mut [u8]) {
2222
0
        write_slice!(src, dst, f32, to_le_bytes);
2223
0
    }
2224
2225
    #[inline]
2226
0
    fn write_f64_into(src: &[f64], dst: &mut [u8]) {
2227
0
        write_slice!(src, dst, f64, to_le_bytes);
2228
0
    }
2229
2230
    #[inline]
2231
0
    fn from_slice_u16(numbers: &mut [u16]) {
2232
0
        if cfg!(target_endian = "big") {
2233
0
            for n in numbers {
2234
0
                *n = n.to_le();
2235
0
            }
2236
0
        }
2237
0
    }
2238
2239
    #[inline]
2240
0
    fn from_slice_u32(numbers: &mut [u32]) {
2241
0
        if cfg!(target_endian = "big") {
2242
0
            for n in numbers {
2243
0
                *n = n.to_le();
2244
0
            }
2245
0
        }
2246
0
    }
2247
2248
    #[inline]
2249
0
    fn from_slice_u64(numbers: &mut [u64]) {
2250
0
        if cfg!(target_endian = "big") {
2251
0
            for n in numbers {
2252
0
                *n = n.to_le();
2253
0
            }
2254
0
        }
2255
0
    }
2256
2257
    #[inline]
2258
0
    fn from_slice_u128(numbers: &mut [u128]) {
2259
0
        if cfg!(target_endian = "big") {
2260
0
            for n in numbers {
2261
0
                *n = n.to_le();
2262
0
            }
2263
0
        }
2264
0
    }
2265
2266
    #[inline]
2267
0
    fn from_slice_i16(numbers: &mut [i16]) {
2268
0
        if cfg!(target_endian = "big") {
2269
0
            for n in numbers {
2270
0
                *n = n.to_le();
2271
0
            }
2272
0
        }
2273
0
    }
2274
2275
    #[inline]
2276
0
    fn from_slice_i32(numbers: &mut [i32]) {
2277
0
        if cfg!(target_endian = "big") {
2278
0
            for n in numbers {
2279
0
                *n = n.to_le();
2280
0
            }
2281
0
        }
2282
0
    }
2283
2284
    #[inline]
2285
0
    fn from_slice_i64(numbers: &mut [i64]) {
2286
0
        if cfg!(target_endian = "big") {
2287
0
            for n in numbers {
2288
0
                *n = n.to_le();
2289
0
            }
2290
0
        }
2291
0
    }
2292
2293
    #[inline]
2294
0
    fn from_slice_i128(numbers: &mut [i128]) {
2295
0
        if cfg!(target_endian = "big") {
2296
0
            for n in numbers {
2297
0
                *n = n.to_le();
2298
0
            }
2299
0
        }
2300
0
    }
2301
2302
    #[inline]
2303
0
    fn from_slice_f32(numbers: &mut [f32]) {
2304
0
        if cfg!(target_endian = "big") {
2305
0
            for n in numbers {
2306
0
                *n = f32::from_bits(n.to_bits().to_le());
2307
0
            }
2308
0
        }
2309
0
    }
2310
2311
    #[inline]
2312
0
    fn from_slice_f64(numbers: &mut [f64]) {
2313
0
        if cfg!(target_endian = "big") {
2314
0
            for n in numbers {
2315
0
                *n = f64::from_bits(n.to_bits().to_le());
2316
0
            }
2317
0
        }
2318
0
    }
2319
}
2320
2321
#[cfg(test)]
2322
mod test {
2323
    use quickcheck::{Arbitrary, Gen, QuickCheck, StdGen, Testable};
2324
    use rand::{thread_rng, Rng};
2325
2326
    pub const U24_MAX: u32 = 16_777_215;
2327
    pub const I24_MAX: i32 = 8_388_607;
2328
    pub const U48_MAX: u64 = 281_474_976_710_655;
2329
    pub const I48_MAX: i64 = 140_737_488_355_327;
2330
2331
    pub const U64_MAX: u64 = ::core::u64::MAX;
2332
    pub const I64_MAX: u64 = ::core::i64::MAX as u64;
2333
2334
    macro_rules! calc_max {
2335
        ($max:expr, $bytes:expr) => {
2336
            calc_max!($max, $bytes, 8)
2337
        };
2338
        ($max:expr, $bytes:expr, $maxbytes:expr) => {
2339
            ($max - 1) >> (8 * ($maxbytes - $bytes))
2340
        };
2341
    }
2342
2343
    #[derive(Clone, Debug)]
2344
    pub struct Wi128<T>(pub T);
2345
2346
    impl<T: Clone> Wi128<T> {
2347
        pub fn clone(&self) -> T {
2348
            self.0.clone()
2349
        }
2350
    }
2351
2352
    impl<T: PartialEq> PartialEq<T> for Wi128<T> {
2353
        fn eq(&self, other: &T) -> bool {
2354
            self.0.eq(other)
2355
        }
2356
    }
2357
2358
    impl Arbitrary for Wi128<u128> {
2359
        fn arbitrary<G: Gen>(gen: &mut G) -> Wi128<u128> {
2360
            let max = calc_max!(::core::u128::MAX, gen.size(), 16);
2361
            let output = (gen.gen::<u64>() as u128)
2362
                | ((gen.gen::<u64>() as u128) << 64);
2363
            Wi128(output & (max - 1))
2364
        }
2365
    }
2366
2367
    impl Arbitrary for Wi128<i128> {
2368
        fn arbitrary<G: Gen>(gen: &mut G) -> Wi128<i128> {
2369
            let max = calc_max!(::core::i128::MAX, gen.size(), 16);
2370
            let output = (gen.gen::<i64>() as i128)
2371
                | ((gen.gen::<i64>() as i128) << 64);
2372
            Wi128(output & (max - 1))
2373
        }
2374
    }
2375
2376
    pub fn qc_sized<A: Testable>(f: A, size: u64) {
2377
        QuickCheck::new()
2378
            .gen(StdGen::new(thread_rng(), size as usize))
2379
            .tests(1_00)
2380
            .max_tests(10_000)
2381
            .quickcheck(f);
2382
    }
2383
2384
    macro_rules! qc_byte_order {
2385
        ($name:ident, $ty_int:ty, $max:expr,
2386
         $bytes:expr, $read:ident, $write:ident) => {
2387
            #[cfg(not(miri))]
2388
            mod $name {
2389
                #[allow(unused_imports)]
2390
                use super::{qc_sized, Wi128};
2391
                use crate::{
2392
                    BigEndian, ByteOrder, LittleEndian, NativeEndian,
2393
                };
2394
2395
                #[test]
2396
                fn big_endian() {
2397
                    fn prop(n: $ty_int) -> bool {
2398
                        let mut buf = [0; 16];
2399
                        BigEndian::$write(&mut buf, n.clone(), $bytes);
2400
                        n == BigEndian::$read(&buf[..$bytes], $bytes)
2401
                    }
2402
                    qc_sized(prop as fn($ty_int) -> bool, $max);
2403
                }
2404
2405
                #[test]
2406
                fn little_endian() {
2407
                    fn prop(n: $ty_int) -> bool {
2408
                        let mut buf = [0; 16];
2409
                        LittleEndian::$write(&mut buf, n.clone(), $bytes);
2410
                        n == LittleEndian::$read(&buf[..$bytes], $bytes)
2411
                    }
2412
                    qc_sized(prop as fn($ty_int) -> bool, $max);
2413
                }
2414
2415
                #[test]
2416
                fn native_endian() {
2417
                    fn prop(n: $ty_int) -> bool {
2418
                        let mut buf = [0; 16];
2419
                        NativeEndian::$write(&mut buf, n.clone(), $bytes);
2420
                        n == NativeEndian::$read(&buf[..$bytes], $bytes)
2421
                    }
2422
                    qc_sized(prop as fn($ty_int) -> bool, $max);
2423
                }
2424
            }
2425
        };
2426
        ($name:ident, $ty_int:ty, $max:expr,
2427
         $read:ident, $write:ident) => {
2428
            #[cfg(not(miri))]
2429
            mod $name {
2430
                #[allow(unused_imports)]
2431
                use super::{qc_sized, Wi128};
2432
                use crate::{
2433
                    BigEndian, ByteOrder, LittleEndian, NativeEndian,
2434
                };
2435
                use core::mem::size_of;
2436
2437
                #[test]
2438
                fn big_endian() {
2439
                    fn prop(n: $ty_int) -> bool {
2440
                        let bytes = size_of::<$ty_int>();
2441
                        let mut buf = [0; 16];
2442
                        BigEndian::$write(&mut buf[16 - bytes..], n.clone());
2443
                        n == BigEndian::$read(&buf[16 - bytes..])
2444
                    }
2445
                    qc_sized(prop as fn($ty_int) -> bool, $max - 1);
2446
                }
2447
2448
                #[test]
2449
                fn little_endian() {
2450
                    fn prop(n: $ty_int) -> bool {
2451
                        let bytes = size_of::<$ty_int>();
2452
                        let mut buf = [0; 16];
2453
                        LittleEndian::$write(&mut buf[..bytes], n.clone());
2454
                        n == LittleEndian::$read(&buf[..bytes])
2455
                    }
2456
                    qc_sized(prop as fn($ty_int) -> bool, $max - 1);
2457
                }
2458
2459
                #[test]
2460
                fn native_endian() {
2461
                    fn prop(n: $ty_int) -> bool {
2462
                        let bytes = size_of::<$ty_int>();
2463
                        let mut buf = [0; 16];
2464
                        NativeEndian::$write(&mut buf[..bytes], n.clone());
2465
                        n == NativeEndian::$read(&buf[..bytes])
2466
                    }
2467
                    qc_sized(prop as fn($ty_int) -> bool, $max - 1);
2468
                }
2469
            }
2470
        };
2471
    }
2472
2473
    qc_byte_order!(
2474
        prop_u16,
2475
        u16,
2476
        ::core::u16::MAX as u64,
2477
        read_u16,
2478
        write_u16
2479
    );
2480
    qc_byte_order!(
2481
        prop_i16,
2482
        i16,
2483
        ::core::i16::MAX as u64,
2484
        read_i16,
2485
        write_i16
2486
    );
2487
    qc_byte_order!(
2488
        prop_u24,
2489
        u32,
2490
        crate::test::U24_MAX as u64,
2491
        read_u24,
2492
        write_u24
2493
    );
2494
    qc_byte_order!(
2495
        prop_i24,
2496
        i32,
2497
        crate::test::I24_MAX as u64,
2498
        read_i24,
2499
        write_i24
2500
    );
2501
    qc_byte_order!(
2502
        prop_u32,
2503
        u32,
2504
        ::core::u32::MAX as u64,
2505
        read_u32,
2506
        write_u32
2507
    );
2508
    qc_byte_order!(
2509
        prop_i32,
2510
        i32,
2511
        ::core::i32::MAX as u64,
2512
        read_i32,
2513
        write_i32
2514
    );
2515
    qc_byte_order!(
2516
        prop_u48,
2517
        u64,
2518
        crate::test::U48_MAX as u64,
2519
        read_u48,
2520
        write_u48
2521
    );
2522
    qc_byte_order!(
2523
        prop_i48,
2524
        i64,
2525
        crate::test::I48_MAX as u64,
2526
        read_i48,
2527
        write_i48
2528
    );
2529
    qc_byte_order!(
2530
        prop_u64,
2531
        u64,
2532
        ::core::u64::MAX as u64,
2533
        read_u64,
2534
        write_u64
2535
    );
2536
    qc_byte_order!(
2537
        prop_i64,
2538
        i64,
2539
        ::core::i64::MAX as u64,
2540
        read_i64,
2541
        write_i64
2542
    );
2543
    qc_byte_order!(
2544
        prop_f32,
2545
        f32,
2546
        ::core::u64::MAX as u64,
2547
        read_f32,
2548
        write_f32
2549
    );
2550
    qc_byte_order!(
2551
        prop_f64,
2552
        f64,
2553
        ::core::i64::MAX as u64,
2554
        read_f64,
2555
        write_f64
2556
    );
2557
2558
    qc_byte_order!(prop_u128, Wi128<u128>, 16 + 1, read_u128, write_u128);
2559
    qc_byte_order!(prop_i128, Wi128<i128>, 16 + 1, read_i128, write_i128);
2560
2561
    qc_byte_order!(
2562
        prop_uint_1,
2563
        u64,
2564
        calc_max!(super::U64_MAX, 1),
2565
        1,
2566
        read_uint,
2567
        write_uint
2568
    );
2569
    qc_byte_order!(
2570
        prop_uint_2,
2571
        u64,
2572
        calc_max!(super::U64_MAX, 2),
2573
        2,
2574
        read_uint,
2575
        write_uint
2576
    );
2577
    qc_byte_order!(
2578
        prop_uint_3,
2579
        u64,
2580
        calc_max!(super::U64_MAX, 3),
2581
        3,
2582
        read_uint,
2583
        write_uint
2584
    );
2585
    qc_byte_order!(
2586
        prop_uint_4,
2587
        u64,
2588
        calc_max!(super::U64_MAX, 4),
2589
        4,
2590
        read_uint,
2591
        write_uint
2592
    );
2593
    qc_byte_order!(
2594
        prop_uint_5,
2595
        u64,
2596
        calc_max!(super::U64_MAX, 5),
2597
        5,
2598
        read_uint,
2599
        write_uint
2600
    );
2601
    qc_byte_order!(
2602
        prop_uint_6,
2603
        u64,
2604
        calc_max!(super::U64_MAX, 6),
2605
        6,
2606
        read_uint,
2607
        write_uint
2608
    );
2609
    qc_byte_order!(
2610
        prop_uint_7,
2611
        u64,
2612
        calc_max!(super::U64_MAX, 7),
2613
        7,
2614
        read_uint,
2615
        write_uint
2616
    );
2617
    qc_byte_order!(
2618
        prop_uint_8,
2619
        u64,
2620
        calc_max!(super::U64_MAX, 8),
2621
        8,
2622
        read_uint,
2623
        write_uint
2624
    );
2625
2626
    qc_byte_order!(
2627
        prop_uint128_1,
2628
        Wi128<u128>,
2629
        1,
2630
        1,
2631
        read_uint128,
2632
        write_uint128
2633
    );
2634
    qc_byte_order!(
2635
        prop_uint128_2,
2636
        Wi128<u128>,
2637
        2,
2638
        2,
2639
        read_uint128,
2640
        write_uint128
2641
    );
2642
    qc_byte_order!(
2643
        prop_uint128_3,
2644
        Wi128<u128>,
2645
        3,
2646
        3,
2647
        read_uint128,
2648
        write_uint128
2649
    );
2650
    qc_byte_order!(
2651
        prop_uint128_4,
2652
        Wi128<u128>,
2653
        4,
2654
        4,
2655
        read_uint128,
2656
        write_uint128
2657
    );
2658
    qc_byte_order!(
2659
        prop_uint128_5,
2660
        Wi128<u128>,
2661
        5,
2662
        5,
2663
        read_uint128,
2664
        write_uint128
2665
    );
2666
    qc_byte_order!(
2667
        prop_uint128_6,
2668
        Wi128<u128>,
2669
        6,
2670
        6,
2671
        read_uint128,
2672
        write_uint128
2673
    );
2674
    qc_byte_order!(
2675
        prop_uint128_7,
2676
        Wi128<u128>,
2677
        7,
2678
        7,
2679
        read_uint128,
2680
        write_uint128
2681
    );
2682
    qc_byte_order!(
2683
        prop_uint128_8,
2684
        Wi128<u128>,
2685
        8,
2686
        8,
2687
        read_uint128,
2688
        write_uint128
2689
    );
2690
    qc_byte_order!(
2691
        prop_uint128_9,
2692
        Wi128<u128>,
2693
        9,
2694
        9,
2695
        read_uint128,
2696
        write_uint128
2697
    );
2698
    qc_byte_order!(
2699
        prop_uint128_10,
2700
        Wi128<u128>,
2701
        10,
2702
        10,
2703
        read_uint128,
2704
        write_uint128
2705
    );
2706
    qc_byte_order!(
2707
        prop_uint128_11,
2708
        Wi128<u128>,
2709
        11,
2710
        11,
2711
        read_uint128,
2712
        write_uint128
2713
    );
2714
    qc_byte_order!(
2715
        prop_uint128_12,
2716
        Wi128<u128>,
2717
        12,
2718
        12,
2719
        read_uint128,
2720
        write_uint128
2721
    );
2722
    qc_byte_order!(
2723
        prop_uint128_13,
2724
        Wi128<u128>,
2725
        13,
2726
        13,
2727
        read_uint128,
2728
        write_uint128
2729
    );
2730
    qc_byte_order!(
2731
        prop_uint128_14,
2732
        Wi128<u128>,
2733
        14,
2734
        14,
2735
        read_uint128,
2736
        write_uint128
2737
    );
2738
    qc_byte_order!(
2739
        prop_uint128_15,
2740
        Wi128<u128>,
2741
        15,
2742
        15,
2743
        read_uint128,
2744
        write_uint128
2745
    );
2746
    qc_byte_order!(
2747
        prop_uint128_16,
2748
        Wi128<u128>,
2749
        16,
2750
        16,
2751
        read_uint128,
2752
        write_uint128
2753
    );
2754
2755
    qc_byte_order!(
2756
        prop_int_1,
2757
        i64,
2758
        calc_max!(super::I64_MAX, 1),
2759
        1,
2760
        read_int,
2761
        write_int
2762
    );
2763
    qc_byte_order!(
2764
        prop_int_2,
2765
        i64,
2766
        calc_max!(super::I64_MAX, 2),
2767
        2,
2768
        read_int,
2769
        write_int
2770
    );
2771
    qc_byte_order!(
2772
        prop_int_3,
2773
        i64,
2774
        calc_max!(super::I64_MAX, 3),
2775
        3,
2776
        read_int,
2777
        write_int
2778
    );
2779
    qc_byte_order!(
2780
        prop_int_4,
2781
        i64,
2782
        calc_max!(super::I64_MAX, 4),
2783
        4,
2784
        read_int,
2785
        write_int
2786
    );
2787
    qc_byte_order!(
2788
        prop_int_5,
2789
        i64,
2790
        calc_max!(super::I64_MAX, 5),
2791
        5,
2792
        read_int,
2793
        write_int
2794
    );
2795
    qc_byte_order!(
2796
        prop_int_6,
2797
        i64,
2798
        calc_max!(super::I64_MAX, 6),
2799
        6,
2800
        read_int,
2801
        write_int
2802
    );
2803
    qc_byte_order!(
2804
        prop_int_7,
2805
        i64,
2806
        calc_max!(super::I64_MAX, 7),
2807
        7,
2808
        read_int,
2809
        write_int
2810
    );
2811
    qc_byte_order!(
2812
        prop_int_8,
2813
        i64,
2814
        calc_max!(super::I64_MAX, 8),
2815
        8,
2816
        read_int,
2817
        write_int
2818
    );
2819
2820
    qc_byte_order!(
2821
        prop_int128_1,
2822
        Wi128<i128>,
2823
        1,
2824
        1,
2825
        read_int128,
2826
        write_int128
2827
    );
2828
    qc_byte_order!(
2829
        prop_int128_2,
2830
        Wi128<i128>,
2831
        2,
2832
        2,
2833
        read_int128,
2834
        write_int128
2835
    );
2836
    qc_byte_order!(
2837
        prop_int128_3,
2838
        Wi128<i128>,
2839
        3,
2840
        3,
2841
        read_int128,
2842
        write_int128
2843
    );
2844
    qc_byte_order!(
2845
        prop_int128_4,
2846
        Wi128<i128>,
2847
        4,
2848
        4,
2849
        read_int128,
2850
        write_int128
2851
    );
2852
    qc_byte_order!(
2853
        prop_int128_5,
2854
        Wi128<i128>,
2855
        5,
2856
        5,
2857
        read_int128,
2858
        write_int128
2859
    );
2860
    qc_byte_order!(
2861
        prop_int128_6,
2862
        Wi128<i128>,
2863
        6,
2864
        6,
2865
        read_int128,
2866
        write_int128
2867
    );
2868
    qc_byte_order!(
2869
        prop_int128_7,
2870
        Wi128<i128>,
2871
        7,
2872
        7,
2873
        read_int128,
2874
        write_int128
2875
    );
2876
    qc_byte_order!(
2877
        prop_int128_8,
2878
        Wi128<i128>,
2879
        8,
2880
        8,
2881
        read_int128,
2882
        write_int128
2883
    );
2884
    qc_byte_order!(
2885
        prop_int128_9,
2886
        Wi128<i128>,
2887
        9,
2888
        9,
2889
        read_int128,
2890
        write_int128
2891
    );
2892
    qc_byte_order!(
2893
        prop_int128_10,
2894
        Wi128<i128>,
2895
        10,
2896
        10,
2897
        read_int128,
2898
        write_int128
2899
    );
2900
    qc_byte_order!(
2901
        prop_int128_11,
2902
        Wi128<i128>,
2903
        11,
2904
        11,
2905
        read_int128,
2906
        write_int128
2907
    );
2908
    qc_byte_order!(
2909
        prop_int128_12,
2910
        Wi128<i128>,
2911
        12,
2912
        12,
2913
        read_int128,
2914
        write_int128
2915
    );
2916
    qc_byte_order!(
2917
        prop_int128_13,
2918
        Wi128<i128>,
2919
        13,
2920
        13,
2921
        read_int128,
2922
        write_int128
2923
    );
2924
    qc_byte_order!(
2925
        prop_int128_14,
2926
        Wi128<i128>,
2927
        14,
2928
        14,
2929
        read_int128,
2930
        write_int128
2931
    );
2932
    qc_byte_order!(
2933
        prop_int128_15,
2934
        Wi128<i128>,
2935
        15,
2936
        15,
2937
        read_int128,
2938
        write_int128
2939
    );
2940
    qc_byte_order!(
2941
        prop_int128_16,
2942
        Wi128<i128>,
2943
        16,
2944
        16,
2945
        read_int128,
2946
        write_int128
2947
    );
2948
2949
    // Test that all of the byte conversion functions panic when given a
2950
    // buffer that is too small.
2951
    //
2952
    // These tests are critical to ensure safety, otherwise we might end up
2953
    // with a buffer overflow.
2954
    macro_rules! too_small {
2955
        ($name:ident, $maximally_small:expr, $zero:expr,
2956
         $read:ident, $write:ident) => {
2957
            mod $name {
2958
                use crate::{
2959
                    BigEndian, ByteOrder, LittleEndian, NativeEndian,
2960
                };
2961
2962
                #[test]
2963
                #[should_panic]
2964
                fn read_big_endian() {
2965
                    let buf = [0; $maximally_small];
2966
                    BigEndian::$read(&buf);
2967
                }
2968
2969
                #[test]
2970
                #[should_panic]
2971
                fn read_little_endian() {
2972
                    let buf = [0; $maximally_small];
2973
                    LittleEndian::$read(&buf);
2974
                }
2975
2976
                #[test]
2977
                #[should_panic]
2978
                fn read_native_endian() {
2979
                    let buf = [0; $maximally_small];
2980
                    NativeEndian::$read(&buf);
2981
                }
2982
2983
                #[test]
2984
                #[should_panic]
2985
                fn write_big_endian() {
2986
                    let mut buf = [0; $maximally_small];
2987
                    BigEndian::$write(&mut buf, $zero);
2988
                }
2989
2990
                #[test]
2991
                #[should_panic]
2992
                fn write_little_endian() {
2993
                    let mut buf = [0; $maximally_small];
2994
                    LittleEndian::$write(&mut buf, $zero);
2995
                }
2996
2997
                #[test]
2998
                #[should_panic]
2999
                fn write_native_endian() {
3000
                    let mut buf = [0; $maximally_small];
3001
                    NativeEndian::$write(&mut buf, $zero);
3002
                }
3003
            }
3004
        };
3005
        ($name:ident, $maximally_small:expr, $read:ident) => {
3006
            mod $name {
3007
                use crate::{
3008
                    BigEndian, ByteOrder, LittleEndian, NativeEndian,
3009
                };
3010
3011
                #[test]
3012
                #[should_panic]
3013
                fn read_big_endian() {
3014
                    let buf = [0; $maximally_small];
3015
                    BigEndian::$read(&buf, $maximally_small + 1);
3016
                }
3017
3018
                #[test]
3019
                #[should_panic]
3020
                fn read_little_endian() {
3021
                    let buf = [0; $maximally_small];
3022
                    LittleEndian::$read(&buf, $maximally_small + 1);
3023
                }
3024
3025
                #[test]
3026
                #[should_panic]
3027
                fn read_native_endian() {
3028
                    let buf = [0; $maximally_small];
3029
                    NativeEndian::$read(&buf, $maximally_small + 1);
3030
                }
3031
            }
3032
        };
3033
    }
3034
3035
    too_small!(small_u16, 1, 0, read_u16, write_u16);
3036
    too_small!(small_i16, 1, 0, read_i16, write_i16);
3037
    too_small!(small_u32, 3, 0, read_u32, write_u32);
3038
    too_small!(small_i32, 3, 0, read_i32, write_i32);
3039
    too_small!(small_u64, 7, 0, read_u64, write_u64);
3040
    too_small!(small_i64, 7, 0, read_i64, write_i64);
3041
    too_small!(small_f32, 3, 0.0, read_f32, write_f32);
3042
    too_small!(small_f64, 7, 0.0, read_f64, write_f64);
3043
    too_small!(small_u128, 15, 0, read_u128, write_u128);
3044
    too_small!(small_i128, 15, 0, read_i128, write_i128);
3045
3046
    too_small!(small_uint_1, 1, read_uint);
3047
    too_small!(small_uint_2, 2, read_uint);
3048
    too_small!(small_uint_3, 3, read_uint);
3049
    too_small!(small_uint_4, 4, read_uint);
3050
    too_small!(small_uint_5, 5, read_uint);
3051
    too_small!(small_uint_6, 6, read_uint);
3052
    too_small!(small_uint_7, 7, read_uint);
3053
3054
    too_small!(small_uint128_1, 1, read_uint128);
3055
    too_small!(small_uint128_2, 2, read_uint128);
3056
    too_small!(small_uint128_3, 3, read_uint128);
3057
    too_small!(small_uint128_4, 4, read_uint128);
3058
    too_small!(small_uint128_5, 5, read_uint128);
3059
    too_small!(small_uint128_6, 6, read_uint128);
3060
    too_small!(small_uint128_7, 7, read_uint128);
3061
    too_small!(small_uint128_8, 8, read_uint128);
3062
    too_small!(small_uint128_9, 9, read_uint128);
3063
    too_small!(small_uint128_10, 10, read_uint128);
3064
    too_small!(small_uint128_11, 11, read_uint128);
3065
    too_small!(small_uint128_12, 12, read_uint128);
3066
    too_small!(small_uint128_13, 13, read_uint128);
3067
    too_small!(small_uint128_14, 14, read_uint128);
3068
    too_small!(small_uint128_15, 15, read_uint128);
3069
3070
    too_small!(small_int_1, 1, read_int);
3071
    too_small!(small_int_2, 2, read_int);
3072
    too_small!(small_int_3, 3, read_int);
3073
    too_small!(small_int_4, 4, read_int);
3074
    too_small!(small_int_5, 5, read_int);
3075
    too_small!(small_int_6, 6, read_int);
3076
    too_small!(small_int_7, 7, read_int);
3077
3078
    too_small!(small_int128_1, 1, read_int128);
3079
    too_small!(small_int128_2, 2, read_int128);
3080
    too_small!(small_int128_3, 3, read_int128);
3081
    too_small!(small_int128_4, 4, read_int128);
3082
    too_small!(small_int128_5, 5, read_int128);
3083
    too_small!(small_int128_6, 6, read_int128);
3084
    too_small!(small_int128_7, 7, read_int128);
3085
    too_small!(small_int128_8, 8, read_int128);
3086
    too_small!(small_int128_9, 9, read_int128);
3087
    too_small!(small_int128_10, 10, read_int128);
3088
    too_small!(small_int128_11, 11, read_int128);
3089
    too_small!(small_int128_12, 12, read_int128);
3090
    too_small!(small_int128_13, 13, read_int128);
3091
    too_small!(small_int128_14, 14, read_int128);
3092
    too_small!(small_int128_15, 15, read_int128);
3093
3094
    // Test that reading/writing slices enforces the correct lengths.
3095
    macro_rules! slice_lengths {
3096
        ($name:ident, $read:ident, $write:ident,
3097
         $num_bytes:expr, $numbers:expr) => {
3098
            mod $name {
3099
                use crate::{
3100
                    BigEndian, ByteOrder, LittleEndian, NativeEndian,
3101
                };
3102
3103
                #[test]
3104
                #[should_panic]
3105
                fn read_big_endian() {
3106
                    let bytes = [0; $num_bytes];
3107
                    let mut numbers = $numbers;
3108
                    BigEndian::$read(&bytes, &mut numbers);
3109
                }
3110
3111
                #[test]
3112
                #[should_panic]
3113
                fn read_little_endian() {
3114
                    let bytes = [0; $num_bytes];
3115
                    let mut numbers = $numbers;
3116
                    LittleEndian::$read(&bytes, &mut numbers);
3117
                }
3118
3119
                #[test]
3120
                #[should_panic]
3121
                fn read_native_endian() {
3122
                    let bytes = [0; $num_bytes];
3123
                    let mut numbers = $numbers;
3124
                    NativeEndian::$read(&bytes, &mut numbers);
3125
                }
3126
3127
                #[test]
3128
                #[should_panic]
3129
                fn write_big_endian() {
3130
                    let mut bytes = [0; $num_bytes];
3131
                    let numbers = $numbers;
3132
                    BigEndian::$write(&numbers, &mut bytes);
3133
                }
3134
3135
                #[test]
3136
                #[should_panic]
3137
                fn write_little_endian() {
3138
                    let mut bytes = [0; $num_bytes];
3139
                    let numbers = $numbers;
3140
                    LittleEndian::$write(&numbers, &mut bytes);
3141
                }
3142
3143
                #[test]
3144
                #[should_panic]
3145
                fn write_native_endian() {
3146
                    let mut bytes = [0; $num_bytes];
3147
                    let numbers = $numbers;
3148
                    NativeEndian::$write(&numbers, &mut bytes);
3149
                }
3150
            }
3151
        };
3152
    }
3153
3154
    slice_lengths!(
3155
        slice_len_too_small_u16,
3156
        read_u16_into,
3157
        write_u16_into,
3158
        3,
3159
        [0, 0]
3160
    );
3161
    slice_lengths!(
3162
        slice_len_too_big_u16,
3163
        read_u16_into,
3164
        write_u16_into,
3165
        5,
3166
        [0, 0]
3167
    );
3168
    slice_lengths!(
3169
        slice_len_too_small_i16,
3170
        read_i16_into,
3171
        write_i16_into,
3172
        3,
3173
        [0, 0]
3174
    );
3175
    slice_lengths!(
3176
        slice_len_too_big_i16,
3177
        read_i16_into,
3178
        write_i16_into,
3179
        5,
3180
        [0, 0]
3181
    );
3182
3183
    slice_lengths!(
3184
        slice_len_too_small_u32,
3185
        read_u32_into,
3186
        write_u32_into,
3187
        7,
3188
        [0, 0]
3189
    );
3190
    slice_lengths!(
3191
        slice_len_too_big_u32,
3192
        read_u32_into,
3193
        write_u32_into,
3194
        9,
3195
        [0, 0]
3196
    );
3197
    slice_lengths!(
3198
        slice_len_too_small_i32,
3199
        read_i32_into,
3200
        write_i32_into,
3201
        7,
3202
        [0, 0]
3203
    );
3204
    slice_lengths!(
3205
        slice_len_too_big_i32,
3206
        read_i32_into,
3207
        write_i32_into,
3208
        9,
3209
        [0, 0]
3210
    );
3211
3212
    slice_lengths!(
3213
        slice_len_too_small_u64,
3214
        read_u64_into,
3215
        write_u64_into,
3216
        15,
3217
        [0, 0]
3218
    );
3219
    slice_lengths!(
3220
        slice_len_too_big_u64,
3221
        read_u64_into,
3222
        write_u64_into,
3223
        17,
3224
        [0, 0]
3225
    );
3226
    slice_lengths!(
3227
        slice_len_too_small_i64,
3228
        read_i64_into,
3229
        write_i64_into,
3230
        15,
3231
        [0, 0]
3232
    );
3233
    slice_lengths!(
3234
        slice_len_too_big_i64,
3235
        read_i64_into,
3236
        write_i64_into,
3237
        17,
3238
        [0, 0]
3239
    );
3240
3241
    slice_lengths!(
3242
        slice_len_too_small_u128,
3243
        read_u128_into,
3244
        write_u128_into,
3245
        31,
3246
        [0, 0]
3247
    );
3248
    slice_lengths!(
3249
        slice_len_too_big_u128,
3250
        read_u128_into,
3251
        write_u128_into,
3252
        33,
3253
        [0, 0]
3254
    );
3255
    slice_lengths!(
3256
        slice_len_too_small_i128,
3257
        read_i128_into,
3258
        write_i128_into,
3259
        31,
3260
        [0, 0]
3261
    );
3262
    slice_lengths!(
3263
        slice_len_too_big_i128,
3264
        read_i128_into,
3265
        write_i128_into,
3266
        33,
3267
        [0, 0]
3268
    );
3269
3270
    #[test]
3271
    fn uint_bigger_buffer() {
3272
        use crate::{ByteOrder, LittleEndian};
3273
        let n = LittleEndian::read_uint(&[1, 2, 3, 4, 5, 6, 7, 8], 5);
3274
        assert_eq!(n, 0x05_0403_0201);
3275
    }
3276
3277
    #[test]
3278
    fn regression173_array_impl() {
3279
        use crate::{BigEndian, ByteOrder, LittleEndian};
3280
3281
        let xs = [0; 100];
3282
3283
        let x = BigEndian::read_u16(&xs);
3284
        assert_eq!(x, 0);
3285
        let x = BigEndian::read_u32(&xs);
3286
        assert_eq!(x, 0);
3287
        let x = BigEndian::read_u64(&xs);
3288
        assert_eq!(x, 0);
3289
        let x = BigEndian::read_u128(&xs);
3290
        assert_eq!(x, 0);
3291
        let x = BigEndian::read_i16(&xs);
3292
        assert_eq!(x, 0);
3293
        let x = BigEndian::read_i32(&xs);
3294
        assert_eq!(x, 0);
3295
        let x = BigEndian::read_i64(&xs);
3296
        assert_eq!(x, 0);
3297
        let x = BigEndian::read_i128(&xs);
3298
        assert_eq!(x, 0);
3299
3300
        let x = LittleEndian::read_u16(&xs);
3301
        assert_eq!(x, 0);
3302
        let x = LittleEndian::read_u32(&xs);
3303
        assert_eq!(x, 0);
3304
        let x = LittleEndian::read_u64(&xs);
3305
        assert_eq!(x, 0);
3306
        let x = LittleEndian::read_u128(&xs);
3307
        assert_eq!(x, 0);
3308
        let x = LittleEndian::read_i16(&xs);
3309
        assert_eq!(x, 0);
3310
        let x = LittleEndian::read_i32(&xs);
3311
        assert_eq!(x, 0);
3312
        let x = LittleEndian::read_i64(&xs);
3313
        assert_eq!(x, 0);
3314
        let x = LittleEndian::read_i128(&xs);
3315
        assert_eq!(x, 0);
3316
    }
3317
}
3318
3319
#[cfg(test)]
3320
#[cfg(feature = "std")]
3321
mod stdtests {
3322
    extern crate quickcheck;
3323
    extern crate rand;
3324
3325
    use self::quickcheck::{QuickCheck, StdGen, Testable};
3326
    use self::rand::thread_rng;
3327
3328
    fn qc_unsized<A: Testable>(f: A) {
3329
        QuickCheck::new()
3330
            .gen(StdGen::new(thread_rng(), 16))
3331
            .tests(1_00)
3332
            .max_tests(10_000)
3333
            .quickcheck(f);
3334
    }
3335
3336
    macro_rules! calc_max {
3337
        ($max:expr, $bytes:expr) => {
3338
            ($max - 1) >> (8 * (8 - $bytes))
3339
        };
3340
    }
3341
3342
    macro_rules! qc_bytes_ext {
3343
        ($name:ident, $ty_int:ty, $max:expr,
3344
         $bytes:expr, $read:ident, $write:ident) => {
3345
            #[cfg(not(miri))]
3346
            mod $name {
3347
                #[allow(unused_imports)]
3348
                use crate::test::{qc_sized, Wi128};
3349
                use crate::{
3350
                    BigEndian, LittleEndian, NativeEndian, ReadBytesExt,
3351
                    WriteBytesExt,
3352
                };
3353
                use std::io::Cursor;
3354
3355
                #[test]
3356
                fn big_endian() {
3357
                    fn prop(n: $ty_int) -> bool {
3358
                        let mut wtr = vec![];
3359
                        wtr.$write::<BigEndian>(n.clone()).unwrap();
3360
                        let offset = wtr.len() - $bytes;
3361
                        let mut rdr = Cursor::new(&mut wtr[offset..]);
3362
                        n == rdr.$read::<BigEndian>($bytes).unwrap()
3363
                    }
3364
                    qc_sized(prop as fn($ty_int) -> bool, $max);
3365
                }
3366
3367
                #[test]
3368
                fn little_endian() {
3369
                    fn prop(n: $ty_int) -> bool {
3370
                        let mut wtr = vec![];
3371
                        wtr.$write::<LittleEndian>(n.clone()).unwrap();
3372
                        let mut rdr = Cursor::new(wtr);
3373
                        n == rdr.$read::<LittleEndian>($bytes).unwrap()
3374
                    }
3375
                    qc_sized(prop as fn($ty_int) -> bool, $max);
3376
                }
3377
3378
                #[test]
3379
                fn native_endian() {
3380
                    fn prop(n: $ty_int) -> bool {
3381
                        let mut wtr = vec![];
3382
                        wtr.$write::<NativeEndian>(n.clone()).unwrap();
3383
                        let offset = if cfg!(target_endian = "big") {
3384
                            wtr.len() - $bytes
3385
                        } else {
3386
                            0
3387
                        };
3388
                        let mut rdr = Cursor::new(&mut wtr[offset..]);
3389
                        n == rdr.$read::<NativeEndian>($bytes).unwrap()
3390
                    }
3391
                    qc_sized(prop as fn($ty_int) -> bool, $max);
3392
                }
3393
            }
3394
        };
3395
        ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => {
3396
            #[cfg(not(miri))]
3397
            mod $name {
3398
                #[allow(unused_imports)]
3399
                use crate::test::{qc_sized, Wi128};
3400
                use crate::{
3401
                    BigEndian, LittleEndian, NativeEndian, ReadBytesExt,
3402
                    WriteBytesExt,
3403
                };
3404
                use std::io::Cursor;
3405
3406
                #[test]
3407
                fn big_endian() {
3408
                    fn prop(n: $ty_int) -> bool {
3409
                        let mut wtr = vec![];
3410
                        wtr.$write::<BigEndian>(n.clone()).unwrap();
3411
                        let mut rdr = Cursor::new(wtr);
3412
                        n == rdr.$read::<BigEndian>().unwrap()
3413
                    }
3414
                    qc_sized(prop as fn($ty_int) -> bool, $max - 1);
3415
                }
3416
3417
                #[test]
3418
                fn little_endian() {
3419
                    fn prop(n: $ty_int) -> bool {
3420
                        let mut wtr = vec![];
3421
                        wtr.$write::<LittleEndian>(n.clone()).unwrap();
3422
                        let mut rdr = Cursor::new(wtr);
3423
                        n == rdr.$read::<LittleEndian>().unwrap()
3424
                    }
3425
                    qc_sized(prop as fn($ty_int) -> bool, $max - 1);
3426
                }
3427
3428
                #[test]
3429
                fn native_endian() {
3430
                    fn prop(n: $ty_int) -> bool {
3431
                        let mut wtr = vec![];
3432
                        wtr.$write::<NativeEndian>(n.clone()).unwrap();
3433
                        let mut rdr = Cursor::new(wtr);
3434
                        n == rdr.$read::<NativeEndian>().unwrap()
3435
                    }
3436
                    qc_sized(prop as fn($ty_int) -> bool, $max - 1);
3437
                }
3438
            }
3439
        };
3440
    }
3441
3442
    qc_bytes_ext!(
3443
        prop_ext_u16,
3444
        u16,
3445
        ::std::u16::MAX as u64,
3446
        read_u16,
3447
        write_u16
3448
    );
3449
    qc_bytes_ext!(
3450
        prop_ext_i16,
3451
        i16,
3452
        ::std::i16::MAX as u64,
3453
        read_i16,
3454
        write_i16
3455
    );
3456
    qc_bytes_ext!(
3457
        prop_ext_u32,
3458
        u32,
3459
        ::std::u32::MAX as u64,
3460
        read_u32,
3461
        write_u32
3462
    );
3463
    qc_bytes_ext!(
3464
        prop_ext_i32,
3465
        i32,
3466
        ::std::i32::MAX as u64,
3467
        read_i32,
3468
        write_i32
3469
    );
3470
    qc_bytes_ext!(
3471
        prop_ext_u64,
3472
        u64,
3473
        ::std::u64::MAX as u64,
3474
        read_u64,
3475
        write_u64
3476
    );
3477
    qc_bytes_ext!(
3478
        prop_ext_i64,
3479
        i64,
3480
        ::std::i64::MAX as u64,
3481
        read_i64,
3482
        write_i64
3483
    );
3484
    qc_bytes_ext!(
3485
        prop_ext_f32,
3486
        f32,
3487
        ::std::u64::MAX as u64,
3488
        read_f32,
3489
        write_f32
3490
    );
3491
    qc_bytes_ext!(
3492
        prop_ext_f64,
3493
        f64,
3494
        ::std::i64::MAX as u64,
3495
        read_f64,
3496
        write_f64
3497
    );
3498
3499
    qc_bytes_ext!(prop_ext_u128, Wi128<u128>, 16 + 1, read_u128, write_u128);
3500
    qc_bytes_ext!(prop_ext_i128, Wi128<i128>, 16 + 1, read_i128, write_i128);
3501
3502
    qc_bytes_ext!(
3503
        prop_ext_uint_1,
3504
        u64,
3505
        calc_max!(crate::test::U64_MAX, 1),
3506
        1,
3507
        read_uint,
3508
        write_u64
3509
    );
3510
    qc_bytes_ext!(
3511
        prop_ext_uint_2,
3512
        u64,
3513
        calc_max!(crate::test::U64_MAX, 2),
3514
        2,
3515
        read_uint,
3516
        write_u64
3517
    );
3518
    qc_bytes_ext!(
3519
        prop_ext_uint_3,
3520
        u64,
3521
        calc_max!(crate::test::U64_MAX, 3),
3522
        3,
3523
        read_uint,
3524
        write_u64
3525
    );
3526
    qc_bytes_ext!(
3527
        prop_ext_uint_4,
3528
        u64,
3529
        calc_max!(crate::test::U64_MAX, 4),
3530
        4,
3531
        read_uint,
3532
        write_u64
3533
    );
3534
    qc_bytes_ext!(
3535
        prop_ext_uint_5,
3536
        u64,
3537
        calc_max!(crate::test::U64_MAX, 5),
3538
        5,
3539
        read_uint,
3540
        write_u64
3541
    );
3542
    qc_bytes_ext!(
3543
        prop_ext_uint_6,
3544
        u64,
3545
        calc_max!(crate::test::U64_MAX, 6),
3546
        6,
3547
        read_uint,
3548
        write_u64
3549
    );
3550
    qc_bytes_ext!(
3551
        prop_ext_uint_7,
3552
        u64,
3553
        calc_max!(crate::test::U64_MAX, 7),
3554
        7,
3555
        read_uint,
3556
        write_u64
3557
    );
3558
    qc_bytes_ext!(
3559
        prop_ext_uint_8,
3560
        u64,
3561
        calc_max!(crate::test::U64_MAX, 8),
3562
        8,
3563
        read_uint,
3564
        write_u64
3565
    );
3566
3567
    qc_bytes_ext!(
3568
        prop_ext_uint128_1,
3569
        Wi128<u128>,
3570
        1,
3571
        1,
3572
        read_uint128,
3573
        write_u128
3574
    );
3575
    qc_bytes_ext!(
3576
        prop_ext_uint128_2,
3577
        Wi128<u128>,
3578
        2,
3579
        2,
3580
        read_uint128,
3581
        write_u128
3582
    );
3583
    qc_bytes_ext!(
3584
        prop_ext_uint128_3,
3585
        Wi128<u128>,
3586
        3,
3587
        3,
3588
        read_uint128,
3589
        write_u128
3590
    );
3591
    qc_bytes_ext!(
3592
        prop_ext_uint128_4,
3593
        Wi128<u128>,
3594
        4,
3595
        4,
3596
        read_uint128,
3597
        write_u128
3598
    );
3599
    qc_bytes_ext!(
3600
        prop_ext_uint128_5,
3601
        Wi128<u128>,
3602
        5,
3603
        5,
3604
        read_uint128,
3605
        write_u128
3606
    );
3607
    qc_bytes_ext!(
3608
        prop_ext_uint128_6,
3609
        Wi128<u128>,
3610
        6,
3611
        6,
3612
        read_uint128,
3613
        write_u128
3614
    );
3615
    qc_bytes_ext!(
3616
        prop_ext_uint128_7,
3617
        Wi128<u128>,
3618
        7,
3619
        7,
3620
        read_uint128,
3621
        write_u128
3622
    );
3623
    qc_bytes_ext!(
3624
        prop_ext_uint128_8,
3625
        Wi128<u128>,
3626
        8,
3627
        8,
3628
        read_uint128,
3629
        write_u128
3630
    );
3631
    qc_bytes_ext!(
3632
        prop_ext_uint128_9,
3633
        Wi128<u128>,
3634
        9,
3635
        9,
3636
        read_uint128,
3637
        write_u128
3638
    );
3639
    qc_bytes_ext!(
3640
        prop_ext_uint128_10,
3641
        Wi128<u128>,
3642
        10,
3643
        10,
3644
        read_uint128,
3645
        write_u128
3646
    );
3647
    qc_bytes_ext!(
3648
        prop_ext_uint128_11,
3649
        Wi128<u128>,
3650
        11,
3651
        11,
3652
        read_uint128,
3653
        write_u128
3654
    );
3655
    qc_bytes_ext!(
3656
        prop_ext_uint128_12,
3657
        Wi128<u128>,
3658
        12,
3659
        12,
3660
        read_uint128,
3661
        write_u128
3662
    );
3663
    qc_bytes_ext!(
3664
        prop_ext_uint128_13,
3665
        Wi128<u128>,
3666
        13,
3667
        13,
3668
        read_uint128,
3669
        write_u128
3670
    );
3671
    qc_bytes_ext!(
3672
        prop_ext_uint128_14,
3673
        Wi128<u128>,
3674
        14,
3675
        14,
3676
        read_uint128,
3677
        write_u128
3678
    );
3679
    qc_bytes_ext!(
3680
        prop_ext_uint128_15,
3681
        Wi128<u128>,
3682
        15,
3683
        15,
3684
        read_uint128,
3685
        write_u128
3686
    );
3687
    qc_bytes_ext!(
3688
        prop_ext_uint128_16,
3689
        Wi128<u128>,
3690
        16,
3691
        16,
3692
        read_uint128,
3693
        write_u128
3694
    );
3695
3696
    qc_bytes_ext!(
3697
        prop_ext_int_1,
3698
        i64,
3699
        calc_max!(crate::test::I64_MAX, 1),
3700
        1,
3701
        read_int,
3702
        write_i64
3703
    );
3704
    qc_bytes_ext!(
3705
        prop_ext_int_2,
3706
        i64,
3707
        calc_max!(crate::test::I64_MAX, 2),
3708
        2,
3709
        read_int,
3710
        write_i64
3711
    );
3712
    qc_bytes_ext!(
3713
        prop_ext_int_3,
3714
        i64,
3715
        calc_max!(crate::test::I64_MAX, 3),
3716
        3,
3717
        read_int,
3718
        write_i64
3719
    );
3720
    qc_bytes_ext!(
3721
        prop_ext_int_4,
3722
        i64,
3723
        calc_max!(crate::test::I64_MAX, 4),
3724
        4,
3725
        read_int,
3726
        write_i64
3727
    );
3728
    qc_bytes_ext!(
3729
        prop_ext_int_5,
3730
        i64,
3731
        calc_max!(crate::test::I64_MAX, 5),
3732
        5,
3733
        read_int,
3734
        write_i64
3735
    );
3736
    qc_bytes_ext!(
3737
        prop_ext_int_6,
3738
        i64,
3739
        calc_max!(crate::test::I64_MAX, 6),
3740
        6,
3741
        read_int,
3742
        write_i64
3743
    );
3744
    qc_bytes_ext!(
3745
        prop_ext_int_7,
3746
        i64,
3747
        calc_max!(crate::test::I64_MAX, 1),
3748
        7,
3749
        read_int,
3750
        write_i64
3751
    );
3752
    qc_bytes_ext!(
3753
        prop_ext_int_8,
3754
        i64,
3755
        calc_max!(crate::test::I64_MAX, 8),
3756
        8,
3757
        read_int,
3758
        write_i64
3759
    );
3760
3761
    qc_bytes_ext!(
3762
        prop_ext_int128_1,
3763
        Wi128<i128>,
3764
        1,
3765
        1,
3766
        read_int128,
3767
        write_i128
3768
    );
3769
    qc_bytes_ext!(
3770
        prop_ext_int128_2,
3771
        Wi128<i128>,
3772
        2,
3773
        2,
3774
        read_int128,
3775
        write_i128
3776
    );
3777
    qc_bytes_ext!(
3778
        prop_ext_int128_3,
3779
        Wi128<i128>,
3780
        3,
3781
        3,
3782
        read_int128,
3783
        write_i128
3784
    );
3785
    qc_bytes_ext!(
3786
        prop_ext_int128_4,
3787
        Wi128<i128>,
3788
        4,
3789
        4,
3790
        read_int128,
3791
        write_i128
3792
    );
3793
    qc_bytes_ext!(
3794
        prop_ext_int128_5,
3795
        Wi128<i128>,
3796
        5,
3797
        5,
3798
        read_int128,
3799
        write_i128
3800
    );
3801
    qc_bytes_ext!(
3802
        prop_ext_int128_6,
3803
        Wi128<i128>,
3804
        6,
3805
        6,
3806
        read_int128,
3807
        write_i128
3808
    );
3809
    qc_bytes_ext!(
3810
        prop_ext_int128_7,
3811
        Wi128<i128>,
3812
        7,
3813
        7,
3814
        read_int128,
3815
        write_i128
3816
    );
3817
    qc_bytes_ext!(
3818
        prop_ext_int128_8,
3819
        Wi128<i128>,
3820
        8,
3821
        8,
3822
        read_int128,
3823
        write_i128
3824
    );
3825
    qc_bytes_ext!(
3826
        prop_ext_int128_9,
3827
        Wi128<i128>,
3828
        9,
3829
        9,
3830
        read_int128,
3831
        write_i128
3832
    );
3833
    qc_bytes_ext!(
3834
        prop_ext_int128_10,
3835
        Wi128<i128>,
3836
        10,
3837
        10,
3838
        read_int128,
3839
        write_i128
3840
    );
3841
    qc_bytes_ext!(
3842
        prop_ext_int128_11,
3843
        Wi128<i128>,
3844
        11,
3845
        11,
3846
        read_int128,
3847
        write_i128
3848
    );
3849
    qc_bytes_ext!(
3850
        prop_ext_int128_12,
3851
        Wi128<i128>,
3852
        12,
3853
        12,
3854
        read_int128,
3855
        write_i128
3856
    );
3857
    qc_bytes_ext!(
3858
        prop_ext_int128_13,
3859
        Wi128<i128>,
3860
        13,
3861
        13,
3862
        read_int128,
3863
        write_i128
3864
    );
3865
    qc_bytes_ext!(
3866
        prop_ext_int128_14,
3867
        Wi128<i128>,
3868
        14,
3869
        14,
3870
        read_int128,
3871
        write_i128
3872
    );
3873
    qc_bytes_ext!(
3874
        prop_ext_int128_15,
3875
        Wi128<i128>,
3876
        15,
3877
        15,
3878
        read_int128,
3879
        write_i128
3880
    );
3881
    qc_bytes_ext!(
3882
        prop_ext_int128_16,
3883
        Wi128<i128>,
3884
        16,
3885
        16,
3886
        read_int128,
3887
        write_i128
3888
    );
3889
3890
    // Test slice serialization/deserialization.
3891
    macro_rules! qc_slice {
3892
        ($name:ident, $ty_int:ty, $read:ident, $write:ident, $zero:expr) => {
3893
            #[cfg(not(miri))]
3894
            mod $name {
3895
                use super::qc_unsized;
3896
                #[allow(unused_imports)]
3897
                use crate::test::Wi128;
3898
                use crate::{
3899
                    BigEndian, ByteOrder, LittleEndian, NativeEndian,
3900
                };
3901
                use core::mem::size_of;
3902
3903
                #[test]
3904
                fn big_endian() {
3905
                    fn prop(numbers: Vec<$ty_int>) -> bool {
3906
                        let numbers: Vec<_> =
3907
                            numbers.into_iter().map(|x| x.clone()).collect();
3908
                        let num_bytes = size_of::<$ty_int>() * numbers.len();
3909
                        let mut bytes = vec![0; num_bytes];
3910
3911
                        BigEndian::$write(&numbers, &mut bytes);
3912
3913
                        let mut got = vec![$zero; numbers.len()];
3914
                        BigEndian::$read(&bytes, &mut got);
3915
3916
                        numbers == got
3917
                    }
3918
                    qc_unsized(prop as fn(_) -> bool);
3919
                }
3920
3921
                #[test]
3922
                fn little_endian() {
3923
                    fn prop(numbers: Vec<$ty_int>) -> bool {
3924
                        let numbers: Vec<_> =
3925
                            numbers.into_iter().map(|x| x.clone()).collect();
3926
                        let num_bytes = size_of::<$ty_int>() * numbers.len();
3927
                        let mut bytes = vec![0; num_bytes];
3928
3929
                        LittleEndian::$write(&numbers, &mut bytes);
3930
3931
                        let mut got = vec![$zero; numbers.len()];
3932
                        LittleEndian::$read(&bytes, &mut got);
3933
3934
                        numbers == got
3935
                    }
3936
                    qc_unsized(prop as fn(_) -> bool);
3937
                }
3938
3939
                #[test]
3940
                fn native_endian() {
3941
                    fn prop(numbers: Vec<$ty_int>) -> bool {
3942
                        let numbers: Vec<_> =
3943
                            numbers.into_iter().map(|x| x.clone()).collect();
3944
                        let num_bytes = size_of::<$ty_int>() * numbers.len();
3945
                        let mut bytes = vec![0; num_bytes];
3946
3947
                        NativeEndian::$write(&numbers, &mut bytes);
3948
3949
                        let mut got = vec![$zero; numbers.len()];
3950
                        NativeEndian::$read(&bytes, &mut got);
3951
3952
                        numbers == got
3953
                    }
3954
                    qc_unsized(prop as fn(_) -> bool);
3955
                }
3956
            }
3957
        };
3958
    }
3959
3960
    qc_slice!(prop_slice_u16, u16, read_u16_into, write_u16_into, 0);
3961
    qc_slice!(prop_slice_i16, i16, read_i16_into, write_i16_into, 0);
3962
    qc_slice!(prop_slice_u32, u32, read_u32_into, write_u32_into, 0);
3963
    qc_slice!(prop_slice_i32, i32, read_i32_into, write_i32_into, 0);
3964
    qc_slice!(prop_slice_u64, u64, read_u64_into, write_u64_into, 0);
3965
    qc_slice!(prop_slice_i64, i64, read_i64_into, write_i64_into, 0);
3966
    qc_slice!(
3967
        prop_slice_u128,
3968
        Wi128<u128>,
3969
        read_u128_into,
3970
        write_u128_into,
3971
        0
3972
    );
3973
    qc_slice!(
3974
        prop_slice_i128,
3975
        Wi128<i128>,
3976
        read_i128_into,
3977
        write_i128_into,
3978
        0
3979
    );
3980
3981
    qc_slice!(prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0);
3982
    qc_slice!(prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0);
3983
}