1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::io::{self, Read};
use std::mem;

/// Trait to enable writing bytes to a reader.
pub trait ReadBytes: Sized {
    /// Read bytes from a reader as big endian.
    ///
    /// Returns the amount of bytes read.
    fn read_be_bytes<R: Read>(reader: &mut R) -> io::Result<Self>;

    /// Read bytes from a reader as little endian.
    ///
    /// Returns the amount of bytes read.
    fn read_le_bytes<R: Read>(reader: &mut R) -> io::Result<Self>;

    /// Read bytes from a reader using native endianness.
    ///
    /// Returns the amount of bytes read.
    fn read_ne_bytes<R: Read>(reader: &mut R) -> io::Result<Self>;
}

macro_rules! doc_comment {
    ($x:expr, $($tt:tt)*) => {
        #[doc = $x]
        $($tt)*
    };
}

macro_rules! read_bytes_impl {
    ($($SelfT:ty),* $(,)?) => { $(
        impl ReadBytes for $SelfT {
        doc_comment! {
            concat!("Read bytes from a reader as big endian.

# Examples

```
use std::io::{Cursor, Seek, SeekFrom};
use omnom::prelude::*;

let mut buf = Cursor::new(vec![0; 15]);

let num = 12_", stringify!($SelfT), ";
buf.write_be(num).unwrap();

buf.seek(SeekFrom::Start(0)).unwrap();
let num: ", stringify!($SelfT), " = buf.read_be().unwrap();
assert_eq!(num, 12);
```"),
            fn read_be_bytes<R: Read>(reader: &mut R) -> io::Result<Self> {
                let mut buf = [0; mem::size_of::<$SelfT>()];
                reader.read_exact(&mut buf)?;
                Ok(<$SelfT>::from_be_bytes(buf))
            }
        }

        doc_comment! {
            concat!("Read bytes from a reader as little endian.

# Examples

```
use std::io::{Cursor, Seek, SeekFrom};
use omnom::prelude::*;

let mut buf = Cursor::new(vec![0; 15]);

let num = 12_", stringify!($SelfT), ";
buf.write_le(num).unwrap();

buf.seek(SeekFrom::Start(0)).unwrap();
let num: ", stringify!($SelfT), " = buf.read_le().unwrap();
assert_eq!(num, 12);
```"),
            fn read_le_bytes<R: Read>(reader: &mut R) -> io::Result<Self> {
                let mut buf = [0; mem::size_of::<$SelfT>()];
                reader.read_exact(&mut buf)?;
                Ok(<$SelfT>::from_le_bytes(buf))
            }
        }

        doc_comment! {
            concat!("Read bytes from a reader using native endianness.

As the target platform's native endianness is used, portable code
likely wants to use [`read_be_bytes`] or [`read_le_bytes`], as
appropriate instead.

[`read_be_bytes`]: #method.read_be_bytes
[`read_le_bytes`]: #method.read_le_bytes

# Examples

```
use std::io::{Cursor, Seek, SeekFrom};
use omnom::prelude::*;

let mut buf = Cursor::new(vec![0; 15]);

let num = 12_", stringify!($SelfT), ";
buf.write_ne(num).unwrap();

buf.seek(SeekFrom::Start(0)).unwrap();
let num: ", stringify!($SelfT), " = buf.read_ne().unwrap();
assert_eq!(num, 12);
```"),
            fn read_ne_bytes<R: Read>(reader: &mut R) -> io::Result<Self> {
                let mut buf = [0; mem::size_of::<$SelfT>()];
                reader.read_exact(&mut buf)?;
                Ok(<$SelfT>::from_ne_bytes(buf))
            }
        }
    }
)*}}

read_bytes_impl!(u8, u16, u32, u64, u128, usize);
read_bytes_impl!(i8, i16, i32, i64, i128, isize);