Trait binary_util::interfaces::Streamable
source · pub trait Streamable<T>: Reader<T> + Writer {
// Provided methods
fn parse(&self) -> Result<Vec<u8>, BinaryError> { ... }
fn fparse(&self) -> Vec<u8> { ... }
fn compose(source: &[u8], position: &mut usize) -> Result<T, BinaryError>
where Self: Sized { ... }
fn fcompose(source: &[u8], position: &mut usize) -> T
where Self: Sized { ... }
}
Expand description
Deprecated
This trait exists only for backwards compatibility.
If you wish to read and write from a ByteReader
or ByteWriter
,
use the Reader
and Writer
traits.
New Implementation Example
ⓘ
use binary_util::io::{ByteReader, ByteWriter};
use binary_util::interfaces::{Reader, Writer};
pub struct MyStruct;
impl Reader for MyStruct;
impl Writer for MyStruct;
Streamable
A trait to parse and unparse header structs from a given buffer.
ⓘ
use binary_util::{Streamable, error::BinaryError};
struct Foo {
bar: u8,
foo_bar: u16
}
impl Streamable for Foo {
fn parse(&self) -> Result<Vec<u8>, BinaryError> {
use std::io::Write;
let mut stream = Vec::<u8>::new();
stream.write_all(&self.bar.parse()?[..])?;
stream.write_all(&self.bar.parse()?[..])?;
Ok(stream)
}
fn compose(source: &[u8], position: &mut usize) -> Result<Self, BinaryError> {
// Streamable is implemented for all primitives, so we can
// just use this implementation to read our properties.
Ok(Self {
bar: u8::compose(&source, position)?,
foo_bar: u16::compose(&source, position)?
})
}
}
Provided Methods§
sourcefn fparse(&self) -> Vec<u8>
fn fparse(&self) -> Vec<u8>
Writes and unwraps self
to the given buffer.
⚠️ This method is not fail safe, and will panic if result is Err.