| 1 | //! Traits used in this library |
| 2 | use std::io; |
| 3 | |
| 4 | /// Writer extension to write little endian data |
| 5 | pub trait WriteBytesExt<T> { |
| 6 | /// Writes `T` to a bytes stream. Least significant byte first. |
| 7 | fn write_le(&mut self, n: T) -> io::Result<()>; |
| 8 | |
| 9 | /* |
| 10 | #[inline] |
| 11 | fn write_byte(&mut self, n: u8) -> io::Result<()> where Self: Write { |
| 12 | self.write_all(&[n]) |
| 13 | } |
| 14 | */ |
| 15 | } |
| 16 | |
| 17 | impl<W: io::Write + ?Sized> WriteBytesExt<u8> for W { |
| 18 | #[inline(always)] |
| 19 | fn write_le(&mut self, n: u8) -> io::Result<()> { |
| 20 | self.write_all(&[n]) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl<W: io::Write + ?Sized> WriteBytesExt<u16> for W { |
| 25 | #[inline] |
| 26 | fn write_le(&mut self, n: u16) -> io::Result<()> { |
| 27 | self.write_all(&n.to_le_bytes()) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<W: io::Write + ?Sized> WriteBytesExt<u32> for W { |
| 32 | #[inline] |
| 33 | fn write_le(&mut self, n: u32) -> io::Result<()> { |
| 34 | self.write_all(&n.to_le_bytes()) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl<W: io::Write + ?Sized> WriteBytesExt<u64> for W { |
| 39 | #[inline] |
| 40 | fn write_le(&mut self, n: u64) -> io::Result<()> { |
| 41 | self.write_all(&n.to_le_bytes()) |
| 42 | } |
| 43 | } |