[][src]Trait abin::BinFactory

pub trait BinFactory {
    type T: AnyBin;
    fn empty() -> Self::T;
fn from_static(slice: &'static [u8]) -> Self::T;
fn copy_from_slice(slice: &[u8]) -> Self::T;
fn from_iter_with_config<T: GivenVecConfig, TIterator>(
        iter: TIterator
    ) -> Self::T
    where
        TIterator: IntoIterator<Item = u8>
;
fn from_iter(iter: impl IntoIterator<Item = u8>) -> Self::T;
fn from_segments_with_config<'a, T: GivenVecConfig, TIterator>(
        iter: TIterator
    ) -> Self::T
    where
        TIterator: SegmentIterator<BinSegment<'a, Self::T>>
;
fn from_segments<'a>(
        iter: impl SegmentIterator<BinSegment<'a, Self::T>>
    ) -> Self::T;
fn from_segment_with_config<'a, T: GivenVecConfig, TSegment>(
        segment: TSegment
    ) -> Self::T
    where
        TSegment: Into<BinSegment<'a, Self::T>>
;
fn from_segment<'a>(segment: impl Into<BinSegment<'a, Self::T>>) -> Self::T;
fn from_given_vec(vec: Vec<u8>) -> Self::T;
fn from_given_vec_with_config<T: GivenVecConfig>(vec: Vec<u8>) -> Self::T; }

Use this factory to create binaries. There's a built-in implementation in this crate; custom implementations (that implement this trait) are possible.

Associated Types

type T: AnyBin

The type this factory produces.

Loading content...

Required methods

fn empty() -> Self::T

Empty binary.

use abin::{NewBin, Bin, BinFactory};
let bin : Bin = NewBin::empty();
assert_eq!(0, bin.len());

fn from_static(slice: &'static [u8]) -> Self::T

A binary from a &'static [u8].

use abin::{Bin, NewBin, BinFactory, AnyBin};
let bin : Bin = NewBin::from_static("Hello".as_bytes());
assert_eq!("Hello".as_bytes(), bin.as_slice());

fn copy_from_slice(slice: &[u8]) -> Self::T

A binary from a &[u8]; prefer from_static if there's a static lifetime.

use abin::{Bin, NewBin, BinFactory, AnyBin};
let bin : Bin = NewBin::copy_from_slice("Hello".as_bytes());
assert_eq!("Hello".as_bytes(), bin.as_slice());

fn from_iter_with_config<T: GivenVecConfig, TIterator>(
    iter: TIterator
) -> Self::T where
    TIterator: IntoIterator<Item = u8>, 

Create a binary from an iterator. To be efficient, the iterator should provide correct hints (see Iterator::size_hint).

fn from_iter(iter: impl IntoIterator<Item = u8>) -> Self::T

Create a binary from an iterator. To be efficient, the iterator should provide correct hints (see Iterator::size_hint).

use abin::{Bin, NewBin, BinFactory, AnyBin};
let bin : Bin = NewBin::from_iter((0..6).map(|i| i as u8));
assert_eq!(&[0u8, 1u8, 2u8, 3u8, 4u8, 5u8], bin.as_slice());

fn from_segments_with_config<'a, T: GivenVecConfig, TIterator>(
    iter: TIterator
) -> Self::T where
    TIterator: SegmentIterator<BinSegment<'a, Self::T>>, 

Create a binary by joining multiple segments (see BinSegment).

fn from_segments<'a>(
    iter: impl SegmentIterator<BinSegment<'a, Self::T>>
) -> Self::T

Create a binary by joining multiple segments (see BinSegment).

use abin::{BinSegment, SegmentsSlice, Bin, NewBin, BinFactory, AnyBin};

let segments = &mut [BinSegment::Static("Hello, ".as_bytes()),
    BinSegment::Static("World!".as_bytes())];
let iterator = SegmentsSlice::new(segments);
let bin : Bin = NewBin::from_segments(iterator);
assert_eq!("Hello, World!".as_bytes(), bin.as_slice());

fn from_segment_with_config<'a, T: GivenVecConfig, TSegment>(
    segment: TSegment
) -> Self::T where
    TSegment: Into<BinSegment<'a, Self::T>>, 

Convert a BinSegment to a binary.

fn from_segment<'a>(segment: impl Into<BinSegment<'a, Self::T>>) -> Self::T

Convert a BinSegment to a binary.

use abin::{NewBin, BinFactory, BinSegment};

// both lines are equivalent (`from_segment` will just call `from_static`).
let bin_1 = NewBin::from_static("Hello".as_bytes());
let bin_2 = NewBin::from_segment(BinSegment::Static("Hello".as_bytes()));

assert_eq!(bin_1, bin_2);

fn from_given_vec(vec: Vec<u8>) -> Self::T

Creates a binary from given vec. Important: Only use this method if you're given a Vec<u8> from outside (something you can't control). If you're in control, use any of the other methods provided by this factory (such as from_iter, from_segments).

See from_given_vec_with_config with a default config chosen by the implementation.

use abin::{NewBin, BinFactory, AnyBin};
let vec_from_string = "Hello".to_owned().into_bytes();
let bin = NewBin::from_given_vec(vec_from_string);
assert_eq!("Hello".as_bytes(), bin.as_slice());

fn from_given_vec_with_config<T: GivenVecConfig>(vec: Vec<u8>) -> Self::T

Creates a binary from given vec. Important: Only use this method if you're given a Vec<u8> from outside (something you can't control). If you're in control, use any of the other methods provided by this factory (such as from_iter, from_segments).

Loading content...

Implementors

impl<TCf> BinFactory for TCf where
    TCf: CommonFactory,
    <TCf::TAnyRc as AnyRc>::T: AnyBin
[src]

type T = <TCf::TAnyRc as AnyRc>::T

Loading content...