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 { ... }
}
👎Deprecated since 0.3.0: This module is deprecated and will be removed in v0.4.0. Use the
Reader
and Writer
traits instead.Expand description
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)?
})
}
}
Warning: This module is deprecated and will be removed in v0.4.0.
Provided Methods§
sourcefn parse(&self) -> Result<Vec<u8>, BinaryError>
fn parse(&self) -> Result<Vec<u8>, BinaryError>
👎Deprecated since 0.3.0: This module is deprecated and will be removed in v0.4.0. Use the
Reader
and Writer
traits instead.Writes self
to the given buffer.
sourcefn fparse(&self) -> Vec<u8>
fn fparse(&self) -> Vec<u8>
👎Deprecated since 0.3.0: This module is deprecated and will be removed in v0.4.0. Use the
Reader
and Writer
traits instead.Writes and unwraps self
to the given buffer.
⚠️ This method is not fail safe, and will panic if result is Err.
sourcefn compose(source: &[u8], position: &mut usize) -> Result<T, BinaryError>where
Self: Sized,
fn compose(source: &[u8], position: &mut usize) -> Result<T, BinaryError>where Self: Sized,
👎Deprecated since 0.3.0: This module is deprecated and will be removed in v0.4.0. Use the
Reader
and Writer
traits instead.Reads self
from the given buffer.
sourcefn fcompose(source: &[u8], position: &mut usize) -> Twhere
Self: Sized,
fn fcompose(source: &[u8], position: &mut usize) -> Twhere Self: Sized,
👎Deprecated since 0.3.0: This module is deprecated and will be removed in v0.4.0. Use the
Reader
and Writer
traits instead.Reads and unwraps self
from the given buffer.
⚠️ This method is not fail safe, and will panic if result is Err.