Trait Reader

Source
pub trait Reader<Output> {
    // Required method
    fn read(buf: &mut ByteReader) -> Result<Output, Error>;

    // Provided method
    fn read_from_slice(buf: &[u8]) -> Result<Output, Error> { ... }
}
Expand description

Allows you to read from a ByteReader without needing to know the type.

use binary_util::io::{ByteReader, Reader};

pub struct MyStruct {
   pub a: u8,
   pub b: u8
}

impl Reader for MyStruct {
    fn read(&self, buf: &mut ByteReader) -> Result<Self, std::io::Error> {
       let a = buf.read_u8()?;
       let b = buf.read_u8()?;
       Ok(Self { a, b })
    }
}

Required Methods§

Source

fn read(buf: &mut ByteReader) -> Result<Output, Error>

Reads Self from a ByteReader.

For automatic implementations, use the #[derive(BinaryIo)] macro.

Provided Methods§

Source

fn read_from_slice(buf: &[u8]) -> Result<Output, Error>

Reads Self from a &[u8].

This is a convenience method that creates a ByteReader from the slice and calls read.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Reader<SocketAddr> for SocketAddr

Source§

impl Reader<bool> for bool

Source§

impl Reader<char> for char

Source§

impl Reader<f32> for f32

Source§

impl Reader<f64> for f64

Source§

impl Reader<i8> for i8

Source§

impl Reader<i16> for i16

Source§

impl Reader<i32> for i32

Source§

impl Reader<i64> for i64

Source§

impl Reader<i128> for i128

Source§

impl Reader<u8> for u8

Source§

impl Reader<u16> for u16

Source§

impl Reader<u32> for u32

Source§

impl Reader<u64> for u64

Source§

impl Reader<u128> for u128

Source§

impl Reader<String> for String

Source§

impl<T> Reader<Option<T>> for Option<T>
where T: Reader<T> + Sized,

Source§

fn read(buf: &mut ByteReader) -> Result<Option<T>, Error>

Source§

impl<T> Reader<Vec<T>> for Vec<T>
where T: Reader<T> + Sized,

Source§

fn read(buf: &mut ByteReader) -> Result<Vec<T>, Error>

Implementors§