Struct fadroma::bin_serde::ByteLen

source ·
pub struct ByteLen { /* private fields */ }
Expand description

Used to encode the number of items that a sequence type (such as String::len or a Vec::len) has. This allows us to represent that length with as few bytes as possible while allowing it to be dynamic.

Implementations§

source§

impl ByteLen

source

pub const MAX: u32 = 268_435_455u32

source

pub const MAX_SIZE: usize = 4usize

The maximum size in bytes.

source

pub fn encode(len: usize) -> Result<Self>

Encodes the given length as bytes to be serialized. Returns an Error::ByteLenTooLong if the length is bigger than ByteLen::MAX.

source

pub fn as_bytes(&self) -> &[u8]

The raw bytes the represent the encoded length. The size of the slice is always between 1 and ByteLen::MAX_SIZE.

source

pub fn size(&self) -> usize

The size in bytes of the encoded length. Is always between 1 and ByteLen::MAX_SIZE.

source

pub fn decode(de: &mut Deserializer<'_>) -> Result<usize>

Decodes a length from a sequence of bytes as a usize. It relies on the bytes to have previously been encoded using ByteLen::encode and for the given Deserializer to be at the correct position at which those were written before. It is up to the implementor to ensure that these invariants hold true.

Trait Implementations§

source§

impl AsRef<[u8]> for ByteLen

source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for ByteLen

source§

fn clone(&self) -> ByteLen

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 Debug for ByteLen

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Copy for ByteLen

Auto Trait Implementations§

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> DynClone for Twhere T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToHex for Twhere T: AsRef<[u8]>,

source§

fn encode_hex<U>(&self) -> Uwhere U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
source§

fn encode_hex_upper<U>(&self) -> Uwhere U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V