Struct bincode::config::Configuration

source ·
pub struct Configuration<E = LittleEndian, I = Varint, L = NoLimit> { /* private fields */ }
Expand description

The Configuration struct is used to build bincode configurations. The Config trait is implemented by this struct when a valid configuration has been constructed.

The following methods are mutually exclusive and will overwrite each other. The last call to one of these methods determines the behavior of the configuration:

Implementations§

source§

impl<E, I, L> Configuration<E, I, L>

source

pub const fn with_big_endian(self) -> Configuration<BigEndian, I, L>

Makes bincode encode all integer types in big endian.

source

pub const fn with_little_endian(self) -> Configuration<LittleEndian, I, L>

Makes bincode encode all integer types in little endian.

source

pub const fn with_variable_int_encoding(self) -> Configuration<E, Varint, L>

Makes bincode encode all integer types with a variable integer encoding.

Encoding an unsigned integer v (of any type excepting u8) works as follows:

  1. If u < 251, encode it as a single byte with that value.
  2. If 251 <= u < 2**16, encode it as a literal byte 251, followed by a u16 with value u.
  3. If 2**16 <= u < 2**32, encode it as a literal byte 252, followed by a u32 with value u.
  4. If 2**32 <= u < 2**64, encode it as a literal byte 253, followed by a u64 with value u.
  5. If 2**64 <= u < 2**128, encode it as a literal byte 254, followed by a u128 with value u.

Then, for signed integers, we first convert to unsigned using the zigzag algorithm, and then encode them as we do for unsigned integers generally. The reason we use this algorithm is that it encodes those values which are close to zero in less bytes; the obvious algorithm, where we encode the cast values, gives a very large encoding for all negative values.

The zigzag algorithm is defined as follows:

fn zigzag(v: Signed) -> Unsigned {
    match v {
        0 => 0,
        // To avoid the edge case of Signed::min_value()
        // !n is equal to `-n - 1`, so this is:
        // !n * 2 + 1 = 2(-n - 1) + 1 = -2n - 2 + 1 = -2n - 1
        v if v < 0 => !(v as Unsigned) * 2 - 1,
        v if v > 0 => (v as Unsigned) * 2,
    }
}

And works such that:

assert_eq!(zigzag(0), 0);
assert_eq!(zigzag(-1), 1);
assert_eq!(zigzag(1), 2);
assert_eq!(zigzag(-2), 3);
assert_eq!(zigzag(2), 4);
// etc
assert_eq!(zigzag(i64::min_value()), u64::max_value());

Note that u256 and the like are unsupported by this format; if and when they are added to the language, they may be supported via the extension point given by the 255 byte.

source

pub const fn with_fixed_int_encoding(self) -> Configuration<E, Fixint, L>

Fixed-size integer encoding.

  • Fixed size integers are encoded directly
  • Enum discriminants are encoded as u32
  • Lengths and usize are encoded as u64
source

pub const fn with_limit<const N: usize>(self) -> Configuration<E, I, Limit<N>>

Sets the byte limit to limit.

source

pub const fn with_no_limit(self) -> Configuration<E, I, NoLimit>

Clear the byte limit.

Trait Implementations§

source§

impl<E: Clone, I: Clone, L: Clone> Clone for Configuration<E, I, L>

source§

fn clone(&self) -> Configuration<E, I, L>

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<E, I, L> Default for Configuration<E, I, L>

source§

fn default() -> Self

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

impl<E: Copy, I: Copy, L: Copy> Copy for Configuration<E, I, L>

Auto Trait Implementations§

§

impl<E, I, L> Freeze for Configuration<E, I, L>

§

impl<E, I, L> RefUnwindSafe for Configuration<E, I, L>

§

impl<E, I, L> Send for Configuration<E, I, L>
where E: Send, I: Send, L: Send,

§

impl<E, I, L> Sync for Configuration<E, I, L>
where E: Sync, I: Sync, L: Sync,

§

impl<E, I, L> Unpin for Configuration<E, I, L>
where E: Unpin, I: Unpin, L: Unpin,

§

impl<E, I, L> UnwindSafe for Configuration<E, I, L>
where E: UnwindSafe, I: UnwindSafe, L: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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 T
where 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 T
where 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 T
where 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.
source§

impl<T> Config for T
where T: InternalEndianConfig + InternalIntEncodingConfig + InternalLimitConfig + Copy + Clone,