byteable 0.39.0

A Rust crate for convenient serialization and deserialization of byte-oriented data.
Documentation
use crate::{PlainOldData, RawRepr, TryFromRawRepr, byteable_trait::DecodeError};
use core::{error, fmt};
use std::io::{self, Read, Write};

struct CountingReader<'a, R: Read + ?Sized> {
    inner: &'a mut R,
    count: usize,
}

impl<'a, R: Read + ?Sized> CountingReader<'a, R> {
    fn new(inner: &'a mut R) -> Self {
        Self { inner, count: 0 }
    }

    fn count(&self) -> usize {
        self.count
    }
}

impl<R: Read + ?Sized> Read for CountingReader<'_, R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.inner.read(buf)?;
        self.count += n;
        Ok(n)
    }

    fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
        let n = self.inner.read_vectored(bufs)?;
        self.count += n;
        Ok(n)
    }

    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
        let n = self.inner.read_to_end(buf)?;
        self.count += n;
        Ok(n)
    }

    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
        let n = self.inner.read_to_string(buf)?;
        self.count += n;
        Ok(n)
    }

    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
        self.inner.read_exact(buf)?;
        self.count += buf.len();
        Ok(())
    }
}

struct CountingWriter<'a, W: Write + ?Sized> {
    inner: &'a mut W,
    count: usize,
}

impl<'a, W: Write + ?Sized> CountingWriter<'a, W> {
    fn new(inner: &'a mut W) -> Self {
        Self { inner, count: 0 }
    }

    fn count(&self) -> usize {
        self.count
    }
}

impl<W: Write + ?Sized> Write for CountingWriter<'_, W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let n = self.inner.write(buf)?;
        self.count += n;
        Ok(n)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }

    fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
        let n = self.inner.write_vectored(bufs)?;
        self.count += n;
        Ok(n)
    }

    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        self.inner.write_all(buf)?;
        self.count += buf.len();
        Ok(())
    }
}

/// Error returned when reading a value from a [`Read`] source fails.
///
/// Wraps either an I/O error from the underlying reader or a [`DecodeError`] produced
/// when the bytes are valid I/O but do not represent a valid value of the target type.
#[derive(Debug)]
pub enum ReadableError {
    /// An I/O error occurred while reading bytes from the underlying reader.
    Io(std::io::Error),
    /// The bytes were read successfully but could not be decoded into the target type.
    DecodeError(DecodeError),
}

impl fmt::Display for ReadableError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ReadableError::Io(error) => error.fmt(f),
            ReadableError::DecodeError(decode_error) => decode_error.fmt(f),
        }
    }
}

impl error::Error for ReadableError {}

impl From<std::io::Error> for ReadableError {
    #[inline]
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}

impl From<DecodeError> for ReadableError {
    #[inline]
    fn from(value: DecodeError) -> Self {
        Self::DecodeError(value)
    }
}

/// Deserialize a fixed-size value from a [`Read`] source.
///
/// A blanket impl covers all types that implement [`TryFromRawRepr`]: it allocates a
/// zeroed raw buffer, fills it with `read_exact`, then calls `try_from_raw` to validate
/// and convert.
///
/// Prefer the extension method [`ReadFixed::read_fixed`] over calling this trait directly.
///
/// # Errors
///
/// Returns [`ReadableError`] if the read fails or the bytes are not a valid encoding.
pub trait FixedReadable: Sized {
    /// Read exactly `size_of::<Self::Raw>()` bytes and decode them into `Self`.
    ///
    /// # Errors
    ///
    /// Returns [`ReadableError::Io`] on I/O failure or [`ReadableError::DecodeError`]
    /// if the bytes do not encode a valid `Self`.
    fn read_fixed_from(reader: &mut (impl Read + ?Sized)) -> Result<Self, ReadableError>;
}

impl<T: TryFromRawRepr> FixedReadable for T {
    #[inline]
    fn read_fixed_from(reader: &mut (impl Read + ?Sized)) -> Result<Self, ReadableError> {
        let mut b = T::Raw::zeroed();
        reader.read_exact(b.as_bytes_mut())?;
        let r = T::try_from_raw(b)?;
        Ok(r)
    }
}

/// Deserialize a value (possibly variable-length) from a [`Read`] source.
///
/// This is the more general counterpart to [`FixedReadable`]. Variable-length types such
/// as `Vec<T>`, `String`, `HashMap`, and `Option<T>` implement `Readable` directly.
/// Fixed-size types get a blanket impl that delegates to [`FixedReadable`].
///
/// Prefer the extension method [`ReadValue::read_value`] over calling this trait directly.
///
/// # Errors
///
/// Returns [`ReadableError`] if the read fails or the bytes are not a valid encoding.
pub trait Readable: Sized {
    /// Read a value from `reader`.
    ///
    /// # Errors
    ///
    /// Returns [`ReadableError`] on I/O failure or decode error.
    fn read_from(reader: &mut (impl Read + ?Sized)) -> Result<Self, ReadableError>;
}

impl<T: FixedReadable> Readable for T {
    #[inline]
    fn read_from(reader: &mut (impl Read + ?Sized)) -> Result<Self, ReadableError> {
        T::read_fixed_from(reader)
    }
}

/// Serialize a fixed-size value to a [`Write`] sink.
///
/// A blanket impl covers all types that implement [`RawRepr`]: it calls `to_raw()` and
/// writes the raw bytes with `write_all`.
///
/// Prefer the extension method [`WriteFixed::write_fixed`] over calling this trait directly.
pub trait FixedWritable {
    /// Write the fixed-size byte representation of `self` to `writer`.
    ///
    /// # Errors
    ///
    /// Returns [`io::Error`] if writing fails.
    fn write_fixed_to(&self, writer: &mut (impl Write + ?Sized)) -> io::Result<()>;
}

impl<T: RawRepr> FixedWritable for T {
    #[inline]
    fn write_fixed_to(&self, writer: &mut (impl Write + ?Sized)) -> io::Result<()> {
        let raw = self.to_raw();
        writer.write_all(raw.as_bytes())
    }
}

/// Serialize a value (possibly variable-length) to a [`Write`] sink.
///
/// This is the more general counterpart to [`FixedWritable`]. Variable-length types such
/// as `Vec<T>`, `String`, `HashMap`, and slices implement `Writable` directly.
/// Fixed-size types get a blanket impl that delegates to [`FixedWritable`].
///
/// Prefer the extension method [`WriteValue::write_value`] over calling this trait directly.
pub trait Writable {
    /// Write `self` to `writer`.
    ///
    /// # Errors
    ///
    /// Returns [`io::Error`] if writing fails.
    fn write_to(&self, writer: &mut (impl Write + ?Sized)) -> io::Result<()>;
}

impl<T: FixedWritable> Writable for T {
    #[inline]
    fn write_to(&self, writer: &mut (impl Write + ?Sized)) -> io::Result<()> {
        self.write_fixed_to(writer)
    }
}

/// Extension trait that adds [`read_fixed`](ReadFixed::read_fixed) to any [`Read`] impl.
///
/// Automatically implemented for all `T: Read`. Use this to read fixed-size types
/// ergonomically without naming the source trait explicitly:
///
/// ```rust
/// use byteable::io::ReadFixed;
/// use std::io::Cursor;
///
/// let mut cursor = Cursor::new([1u8, 0, 0, 0]);
/// let n: u32 = cursor.read_fixed().unwrap();
/// assert_eq!(n, 1u32);
/// ```
pub trait ReadFixed: Read {
    /// Read a [`FixedReadable`] value from this reader.
    ///
    /// # Errors
    ///
    /// Returns [`ReadableError`] on I/O failure or decode error.
    #[inline]
    fn read_fixed<T: FixedReadable>(&mut self) -> Result<T, ReadableError> {
        T::read_fixed_from(self)
    }

    /// Like [`read_fixed`](ReadFixed::read_fixed), but also returns the number of bytes
    /// consumed from the reader.
    ///
    /// # Errors
    ///
    /// Returns [`ReadableError`] on I/O failure or decode error.
    #[inline]
    fn read_fixed_counted<T: FixedReadable>(&mut self) -> Result<(T, usize), ReadableError> {
        let mut reader = CountingReader::new(self);
        let v = reader.read_fixed()?;
        Ok((v, reader.count()))
    }
}

impl<T: Read + ?Sized> ReadFixed for T {}

/// Extension trait that adds [`read_value`](ReadValue::read_value) to any [`Read`] impl.
///
/// Automatically implemented for all `T: Read`. Use this to read variable-length or
/// fixed-size [`Readable`] types ergonomically:
///
/// ```rust
/// use byteable::io::ReadValue;
/// use std::io::Cursor;
///
/// // Write a length-prefixed string manually, then read it back.
/// let mut data = Vec::new();
/// data.extend_from_slice(&4u64.to_le_bytes()); // length
/// data.extend_from_slice(b"test");
/// let s: String = Cursor::new(data).read_value().unwrap();
/// assert_eq!(s, "test");
/// ```
pub trait ReadValue: Read {
    /// Read a [`Readable`] value from this reader.
    ///
    /// # Errors
    ///
    /// Returns [`ReadableError`] on I/O failure or decode error.
    #[inline]
    fn read_value<T: Readable>(&mut self) -> Result<T, ReadableError> {
        T::read_from(self)
    }

    /// Like [`read_value`](ReadValue::read_value), but also returns the number of bytes
    /// consumed from the reader.
    ///
    /// # Errors
    ///
    /// Returns [`ReadableError`] on I/O failure or decode error.
    #[inline]
    fn read_value_counted<T: Readable>(&mut self) -> Result<(T, usize), ReadableError> {
        let mut reader = CountingReader::new(self);
        let v = reader.read_value()?;
        Ok((v, reader.count()))
    }
}

impl<T: Read> ReadValue for T {}

/// Extension trait that adds [`write_fixed`](WriteFixed::write_fixed) to any [`Write`] impl.
///
/// Automatically implemented for all `T: Write`.
pub trait WriteFixed: Write {
    /// Write a [`FixedWritable`] value to this writer.
    ///
    /// # Errors
    ///
    /// Returns [`io::Error`] if writing fails.
    #[inline]
    fn write_fixed(&mut self, val: &impl FixedWritable) -> io::Result<()> {
        val.write_fixed_to(self)
    }

    /// Like [`write_fixed`](WriteFixed::write_fixed), but returns the number of bytes written.
    ///
    /// # Errors
    ///
    /// Returns [`io::Error`] if writing fails.
    #[inline]
    fn write_fixed_counted(&mut self, val: &impl FixedWritable) -> io::Result<usize> {
        let mut w = CountingWriter::new(self);
        w.write_fixed(val)?;
        Ok(w.count())
    }
}

impl<T: Write> WriteFixed for T {}

/// Extension trait that adds [`write_value`](WriteValue::write_value) to any [`Write`] impl.
///
/// Automatically implemented for all `T: Write`. Use this to write variable-length or
/// fixed-size [`Writable`] types ergonomically:
///
/// ```rust
/// use byteable::io::WriteValue;
///
/// let mut buf = Vec::new();
/// buf.write_value("hello").unwrap();
/// // Wire format: 8-byte LE length prefix + UTF-8 bytes
/// assert_eq!(&buf[..8], &5u64.to_le_bytes());
/// assert_eq!(&buf[8..], b"hello");
/// ```
pub trait WriteValue: Write {
    /// Write a [`Writable`] value to this writer.
    ///
    /// # Errors
    ///
    /// Returns [`io::Error`] if writing fails.
    #[inline]
    fn write_value<T: Writable + ?Sized>(&mut self, data: &T) -> io::Result<()> {
        data.write_to(self)
    }

    /// Like [`write_value`](WriteValue::write_value), but returns the number of bytes written.
    ///
    /// # Errors
    ///
    /// Returns [`io::Error`] if writing fails.
    #[inline]
    fn write_value_counted<T: Writable + ?Sized>(&mut self, data: &T) -> io::Result<usize> {
        let mut w = CountingWriter::new(self);
        w.write_value(data)?;
        Ok(w.count())
    }
}

impl<T: Write> WriteValue for T {}