Trait binary_util::interfaces::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§
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
.