[][src]Trait byte_array::BinaryBuilder

pub trait BinaryBuilder: Sized {
    fn new() -> Self;
fn from_raw(ba: &mut ByteArray) -> Option<Self>;
fn to_raw(&self, ba: &mut ByteArray); }

This trait helps you handle user-defined structs via ByteArray.

Examples

use byte_array::{
   BinaryBuilder,
   ByteArray,
};

struct Foo(u64);

impl BinaryBuilder for Foo {
   fn new() -> Self {
       Self(123)
   }

   fn from_raw(ba: &mut ByteArray) -> Option<Self> {
       Some(Self(ba.read_safe()?))
   }
   fn to_raw(&self, mut ba: &mut ByteArray) {
       ba <<= &self.0;
   }
}

let mut ba = ByteArray::new();

// Write
ba <<=   &Foo::new();
ba <<=   &Foo(321);
ba.write(&Foo(222));

// Read
ba.seek_first();
assert_eq!(123, ba.read::<u64>());
assert_eq!(321, ba.read::<u64>());
assert_eq!(222, ba.read::<u64>());

Required methods

fn new() -> Self

Creates an empty Data.

fn from_raw(ba: &mut ByteArray) -> Option<Self>

Gets the data of the specified type in the ByteArray.

Panics

Panics when unexpected EOF.

fn to_raw(&self, ba: &mut ByteArray)

Adds data to the ByteArray.

Loading content...

Implementations on Foreign Types

impl BinaryBuilder for bool[src]

impl BinaryBuilder for u8[src]

impl BinaryBuilder for i8[src]

impl BinaryBuilder for u16[src]

impl BinaryBuilder for u32[src]

impl BinaryBuilder for u64[src]

impl BinaryBuilder for u128[src]

impl BinaryBuilder for i16[src]

impl BinaryBuilder for i32[src]

impl BinaryBuilder for i64[src]

impl BinaryBuilder for i128[src]

impl BinaryBuilder for usize[src]

impl BinaryBuilder for isize[src]

impl BinaryBuilder for f32[src]

impl BinaryBuilder for f64[src]

impl BinaryBuilder for Vec<u8>[src]

impl BinaryBuilder for String[src]

Loading content...

Implementors

impl BinaryBuilder for ByteArray[src]

Loading content...