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