| 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 as u8, (n >> 8) as u8]) |
| 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_le(n as u16)?; |
| 35 | self.write_le((n >> 16) as u16) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<W: io::Write + ?Sized> WriteBytesExt<u64> for W { |
| 40 | #[inline ] |
| 41 | fn write_le(&mut self, n: u64) -> io::Result<()> { |
| 42 | self.write_le(n as u32)?; |
| 43 | self.write_le((n >> 32) as u32) |
| 44 | } |
| 45 | } |
| 46 | |