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§
Sourcefn read(buf: &mut ByteReader) -> Result<Output, Error>
fn read(buf: &mut ByteReader) -> Result<Output, Error>
Reads Self
from a ByteReader
.
For automatic implementations, use the #[derive(BinaryIo)]
macro.
Provided Methods§
Sourcefn read_from_slice(buf: &[u8]) -> Result<Output, Error>
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.