rw-types 0.2.0

Library for reading and writing types.
Documentation
use std::io;

use crate::{Endian, write::Writable};

/// An extension to the [`io::Write`] trait.
pub trait WriteExt: io::Write {
    /// Writes `W` with the specified endian.
    fn write_ty<W: Writable>(&mut self, value: &W, endian: Endian) -> io::Result<()>;
    /// Writes `W` with big endian.
    fn write_ty_be<W: Writable>(&mut self, value: &W) -> io::Result<()>;
    /// Writes `W` with little endian.
    fn write_ty_le<W: Writable>(&mut self, value: &W) -> io::Result<()>;
    /// Writes `W` with native endian.
    fn write_ty_ne<W: Writable>(&mut self, value: &W) -> io::Result<()>;
}

impl<T: io::Write> WriteExt for T {
    #[inline(always)]
    fn write_ty<W: Writable>(&mut self, value: &W, endian: Endian) -> io::Result<()> {
        value.write(self, endian)
    }
    #[inline(always)]
    fn write_ty_be<W: Writable>(&mut self, value: &W) -> io::Result<()> {
        value.write(self, Endian::Big)
    }
    #[inline(always)]
    fn write_ty_le<W: Writable>(&mut self, value: &W) -> io::Result<()> {
        value.write(self, Endian::Little)
    }
    #[inline(always)]
    fn write_ty_ne<W: Writable>(&mut self, value: &W) -> io::Result<()> {
        value.write(self, Endian::Native)
    }
}