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
use std::io::{self, Write};

/// Trait to enable writing bytes to a writer.
pub trait WriteBytes {
    /// Write bytes to a writer as big endian.
    ///
    /// Returns the amount of bytes written.
    fn write_be_bytes<W: Write>(&self, writer: &mut W) -> io::Result<usize>;

    /// Write bytes to a writer as little endian.
    ///
    /// Returns the amount of bytes written.
    fn write_le_bytes<W: Write>(&self, writer: &mut W) -> io::Result<usize>;

    /// Write bytes to a writer using native endianness.
    ///
    /// Returns the amount of bytes written.
    fn write_ne_bytes<W: Write>(&self, writer: &mut W) -> io::Result<usize>;
}

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

macro_rules! write_bytes_impl {
    ($($SelfT:ty),* $(,)?) => { $(
        impl WriteBytes for $SelfT {
        doc_comment! {
            concat!("Write bytes to a writer as big endian.

# Examples

```
use std::io::Cursor;
use omnom::prelude::*;

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

let num = 12_", stringify!($SelfT), ";
buf.write_be(num).unwrap();
```"),
            fn write_be_bytes<W: Write>(&self, writer: &mut W) -> io::Result<usize> {
                let b = &self.to_be_bytes();
                let len = b.len();
                writer.write_all(b)?;
                Ok(len)
            }
        }

        doc_comment! {
            concat!("Write bytes to a writer as little endian.

# Examples

```
use std::io::Cursor;
use omnom::prelude::*;

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

let num = 12_", stringify!($SelfT), ";
buf.write_le(num).unwrap();
```"),
            fn write_le_bytes<W: Write>(&self, writer: &mut W) -> io::Result<usize> {
                let b = &self.to_le_bytes();
                let len = b.len();
                writer.write_all(b)?;
                Ok(len)
            }
        }

        doc_comment! {
            concat!("Write bytes to a writer using native endianness.

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

[`write_be_bytes`]: #method.write_be_bytes
[`write_le_bytes`]: #method.write_le_bytes

# Examples

```
use std::io::Cursor;
use omnom::prelude::*;

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

let num = 12_", stringify!($SelfT), ";
buf.write_ne(num).unwrap();
```"),
            fn write_ne_bytes<W: Write>(&self, writer: &mut W) -> io::Result<usize> {
                let b = &self.to_ne_bytes();
                let len = b.len();
                writer.write_all(b)?;
                Ok(len)
            }
        }
    }
)*}}

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