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
use super::*;

/// Internal macro for quickly implementing binwrite for types supported by byteorder
macro_rules! binwrite_impl {
    ($(($type_name:ty, $write_func:ident)),*$(,)?) => {
        $(
            impl BinWrite for $type_name {
                fn write_options<W: Write>(&self, writer: &mut W, options: &WriterOption) -> Result<()> {
                    match options.endian {
                        Endian::Big => {
                            writer.$write_func::<BE>(*self)
                        }
                        Endian::Little => {
                            writer.$write_func::<LE>(*self)
                        }
                        Endian::Native => {
                            writer.$write_func::<NativeEndian>(*self)
                        }
                    }
                }
            }
        )*
    }
}

impl BinWrite for char {
    fn write_options<W: Write>(&self, writer: &mut W, _options: &WriterOption) -> Result<()> {
        writer.write_all(&[*self as u8])
    }
}

impl BinWrite for u8 {
    fn write_options<W: Write>(&self, writer: &mut W, _options: &WriterOption) -> Result<()> {
        writer.write_all(&[*self])
    }
}

impl BinWrite for i8 {
    fn write_options<W: Write>(&self, writer: &mut W, _options: &WriterOption) -> Result<()> {
        writer.write_all(&[*self as u8])
    }
}

binwrite_impl!(
    (u16, write_u16),
    (u32, write_u32),
    (u64, write_u64),
    (i16, write_i16),
    (i32, write_i32),
    (i64, write_i64),
    (f32, write_f32),
    (f64, write_f64),
);

impl<B: BinWrite> BinWrite for Vec<B> {
    fn write_options<W: Write>(&self, writer: &mut W, options: &WriterOption) -> Result<()> {
        for item in self {
            BinWrite::write_options(item, writer, options)?;
        }
        Ok(())
    }
}

impl<B: BinWrite> BinWrite for [B] {
    fn write_options<W: Write>(&self, writer: &mut W, options: &WriterOption) -> Result<()> {
        for item in &self[..] {
            BinWrite::write_options(item, writer, options)?;
        }
        Ok(())
    }
}

macro_rules! binwrite_array_impl {
    ($($size:literal),*$(,)?) => {
        $(
            impl<B: BinWrite> BinWrite for [B; $size] {
                fn write_options<W: Write>(&self, writer: &mut W, options: &WriterOption) -> Result<()> {
                    for item in &self[..] {
                        BinWrite::write_options(item, writer, options)?;
                    }
                    Ok(())
                }
            }
        )*
    }
}

binwrite_array_impl!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);

impl BinWrite for &[u8] {
    fn write_options<W: Write>(&self, writer: &mut W, _options: &WriterOption) -> Result<()> {
        writer.write_all(self)
    }
}

impl BinWrite for str {
    fn write_options<W: Write>(&self, writer: &mut W, _options: &WriterOption) -> Result<()> {
        writer.write_all(self.as_bytes())
    }
}

impl BinWrite for &str {
    fn write_options<W: Write>(&self, writer: &mut W, _options: &WriterOption) -> Result<()> {
        writer.write_all((*self).as_bytes())
    }
}

impl<B: BinWrite> BinWrite for &B {
    fn write_options<W: Write>(&self, writer: &mut W, options: &WriterOption) -> Result<()> {
        (*self).write_options(writer, options)
    }
}

impl BinWrite for String {
    fn write_options<W: Write>(&self, writer: &mut W, options: &WriterOption) -> Result<()> {
        BinWrite::write_options(&self[..], writer, options)
    }
}

/// Internal macro to recursively implement BinWrite for every size tuple 0 to 20
macro_rules! binwrite_tuple_impl {
    ($type1:ident $(, $types:ident)*) => {
        #[allow(non_camel_case_types)]
        impl<$type1: BinWrite, $($types: BinWrite),*> BinWrite for ($type1, $($types),*) {
            paste::item!{
                fn write_options<W: Write>(&self, writer: &mut W, options: &WriterOption) -> Result<()> {
                        let (_, $([<item_ $types>]),*) = self;
                        BinWrite::write_options(&self.0, writer, options)?;
                        $(
                            BinWrite::write_options([<item_ $types>], writer, options)?;
                        )*
                    Ok(())
                }
            }
        }

        binwrite_tuple_impl!($($types),*);
    };

    () => {
        impl BinWrite for () {
            fn write_options<W: Write>(&self, _: &mut W, _: &WriterOption) -> Result<()> {
                Ok(())
            }
        }
    };
}

binwrite_tuple_impl!(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20);