pub trait FromStream {
// Required method
fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
where R: Read + Seek;
}
Expand description
A trait for types that can be deserialized from a stream.
This trait provides a method read_from
that allows a type to implement how it reads
data from a stream. Types that implement this trait can be read from a reader (e.g., a
file or buffer) using the from_stream
function.
§Method
read_from
: Reads data from a stream into the implementing type.
§Example
use amebazii:types::FromStream;
struct MyStruct {
field1: u32,
field2: String,
}
impl FromStream for MyStruct {
fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
where
R: std::io::Read + std::io::Seek,
{
// Implement logic to read from the reader and populate `self`
Ok(())
}
}
Required Methods§
Sourcefn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
fn read_from<R>(&mut self, reader: &mut R) -> Result<(), Error>
Reads data from a stream and populates the fields of the type.
This method is called by the from_stream
function to read data from a provided
reader and deserialize it into the implementing type.
§Parameters
reader
: A mutable reference to the reader from which the data will be read.
§Returns
Ok(())
: If the data is successfully read and the type is populated.Err(Error)
: If an error occurs while reading from the stream.
§Example
use amebazii:types::FromStream;
let mut reader = std::io::Cursor::new(vec![1, 2, 3, 4]);
let mut my_struct = MyStruct::default();
my_struct.read_from(&mut reader).unwrap();
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.