1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::io::{self, BufRead, Read};
use std::mem;

use crate::BufReadExt;

/// 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>;

    /// Fill bytes from a reader as big endian.
    ///
    /// Returns the amount of bytes read.
    fn fill_be_bytes<R: BufRead>(reader: &mut R) -> io::Result<Self>;

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

    /// Fill bytes from a reader using native endianness.
    ///
    /// Returns the amount of bytes read.
    fn fill_ne_bytes<R: BufRead>(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))
            }
        }

        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 fill_be_bytes<R: BufRead>(reader: &mut R) -> io::Result<Self> {
                let mut buf = [0; mem::size_of::<$SelfT>()];
                reader.fill_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 fill_le_bytes<R: BufRead>(reader: &mut R) -> io::Result<Self> {
                let mut buf = [0; mem::size_of::<$SelfT>()];
                reader.fill_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 fill_ne_bytes<R: BufRead>(reader: &mut R) -> io::Result<Self> {
                let mut buf = [0; mem::size_of::<$SelfT>()];
                reader.fill_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);