pub struct ByteBuilder<T> {
    buffer: Vec<u8>,
    phantom: PhantomData<T>,
    expected: Option<NonZeroUsize>,
}
Expand description

A dynamically created buffer for T

Fields§

§buffer: Vec<u8>§phantom: PhantomData<T>§expected: Option<NonZeroUsize>

Implementations§

source§

impl<T> ByteBuilder<T>

source

pub fn new() -> Self

Create an unconstrained Builder

source

pub fn new_fixed(size: NonZeroUsize) -> Self

Create a builder of fixed size

source

pub fn u8(self, v: u8) -> Self

Append an u8 in the builder

source

pub fn bytes(self, v: &[u8]) -> Self

Append bytes in the builder

source

pub fn fold<F, I>(self, l: I, f: F) -> Selfwhere I: Iterator, F: FnMut(Self, I::Item) -> Self,

fold over an iterator

source

pub fn iter8<F, I>(self, l: I, f: F) -> Selfwhere I: IntoIterator, I::IntoIter: ExactSizeIterator, F: FnMut(Self, I::Item) -> Self,

Write an iterator of maximum 255 items using the closure f.

Note that the buffer contains a byte to represent the size of the list.

source

pub fn iter16<F, I>(self, l: I, f: F) -> Selfwhere I: IntoIterator, I::IntoIter: ExactSizeIterator, F: FnMut(Self, I::Item) -> Self,

Write an iterator of maximum 2^16 - 1 items using the closure f.

Note that the buffer contains 2 bytes to represent the size of the list.

source

pub fn iter32<F, I>(self, l: I, f: F) -> Selfwhere I: IntoIterator, I::IntoIter: ExactSizeIterator, F: FnMut(Self, I::Item) -> Self,

Write an iterator of maximum 2^32 - 1 items using the closure f.

Note that the buffer contains 4 bytes to represent the size of the list.

source

pub fn sub<F, U>(self, f: F) -> Selfwhere F: Fn(ByteBuilder<U>) -> ByteBuilder<U>,

source

pub fn u16(self, v: u16) -> Self

Append an u16 in the builder

source

pub fn u32(self, v: u32) -> Self

Append an u32 in the builder

source

pub fn u64(self, v: u64) -> Self

Append an u64 in the builder

source

pub fn u128(self, v: u128) -> Self

Append an u128 in the builder

source

pub fn option<F, V>(self, value: Option<V>, f: F) -> Selfwhere F: FnOnce(Self, V) -> Self,

Call ‘f’ on bytebuilder and the value if the value is present, then return the bytebuilder, otherwise just return the bytebuilder

source

pub fn option_or_else<F, G, V>(self, value: Option<V>, default: G, f: F) -> Selfwhere F: FnOnce(Self, V) -> Self, G: FnOnce(Self) -> Self,

Run the first closure if the value is not present, or the second closure with the present parameter.

this is loosely based on Option::map_or_else

The following call:

byte_builder.option_or_else(value, none_call, some_call);

equivalent to the construction:

if let Some(value) = value {
   some_call(byte_builder, value)
} else {
   none_call(byte_builder)
}
source

pub fn finalize(self) -> ByteArray<T>

Finalize the buffer and return a fixed ByteArray of T

source

pub fn finalize_as_vec(self) -> Vec<u8>

Finalize the buffer and return a fixed ByteArray of T

Trait Implementations§

source§

impl<T: Clone> Clone for ByteBuilder<T>

source§

fn clone(&self) -> ByteBuilder<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Default> Default for ByteBuilder<T>

source§

fn default() -> ByteBuilder<T>

Returns the “default value” for a type. Read more
source§

impl<T> From<ByteBuilder<T>> for Vec<u8>

source§

fn from(bb: ByteBuilder<T>) -> Vec<u8>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for ByteBuilder<T>where T: RefUnwindSafe,

§

impl<T> Send for ByteBuilder<T>where T: Send,

§

impl<T> Sync for ByteBuilder<T>where T: Sync,

§

impl<T> Unpin for ByteBuilder<T>where T: Unpin,

§

impl<T> UnwindSafe for ByteBuilder<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.