pub trait FromBytes: Sized {
type Error;
// Required method
fn from_bytes(bytes: &mut Bytes<'_>) -> Result<Self, Self::Error>;
}Expand description
Fallible conversion of bytes to a new type.
§Examples
use frombytes::{Bytes, Error, FromBytes};
struct MyStruct {
a: u32,
b: i16,
}
impl FromBytes for MyStruct {
// we simply use the same error that primitives use
// but any error could be used
type Error = Error;
fn from_bytes(bytes: &mut Bytes) -> Result<Self, Self::Error> {
// uses type inference to know to read a `u32`
let a = bytes.read()?;
// uses type inference to know to read a `i16`
let b = bytes.read()?;
Ok(Self { a, b })
}
}Required Associated Types§
Required Methods§
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.