Skip to main content

Configuration

Struct Configuration 

Source
pub struct Configuration<E = LittleEndian, I = Varint, L = NoLimit, B = SkipBitPacking, O = LsbFirst, F = FingerprintDisabled, FO = BincodeFormat> { /* 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:

  • [with_little_endian] and [with_big_endian]
  • [with_fixed_int_encoding] and [with_variable_int_encoding]
  • [with_bit_packing] and [with_no_bit_packing]

Implementations§

Source§

impl<E, I, L, B, O, F, FO> Configuration<E, I, L, B, O, F, FO>

Source

pub const fn with_big_endian( self, ) -> Configuration<BigEndian, I, L, B, MsbFirst, F, FO>

Makes bincode encode all integer types in big endian.

Source

pub const fn with_little_endian( self, ) -> Configuration<LittleEndian, I, L, B, LsbFirst, F, FO>

Makes bincode encode all integer types in little endian.

Source

pub const fn with_variable_int_encoding( self, ) -> Configuration<E, Varint, L, B, O, F, FO>

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, B, O, F, FO>

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>, B, O, F, FO>

Sets the byte limit to limit.

Source

pub const fn with_no_limit(self) -> Configuration<E, I, NoLimit, B, O, F, FO>

Clear the byte limit.

Source

pub const fn with_bit_packing( self, ) -> Configuration<E, I, L, AllowBitPacking, O, F, FO>

Enables bit-packing for types that support it.

Source

pub const fn with_no_bit_packing( self, ) -> Configuration<E, I, L, SkipBitPacking, O, F, FO>

Disables bit-packing.

Source

pub const fn with_fingerprint( self, ) -> Configuration<E, I, L, B, O, FingerprintEnabled<0>, FO>

Enables fingerprinting with the default deterministic seed (0).

The fingerprint is a 64-bit hash that covers:

  • The type name
  • For structs: field names, types, and their order
  • For enums: variant names, field names, types, and their order
  • Configuration settings such as endianness and integer encoding
Source

pub const fn with_fingerprint_and_seed<const SEED: u64>( self, ) -> Configuration<E, I, L, B, O, FingerprintEnabled<SEED>, FO>

Enables fingerprinting with a custom seed.

This is similar to with_fingerprint, but allows specifying a custom seed to further differentiate fingerprints.

Source

pub const fn with_legacy_fingerprint<const EXPECTED: u64>( self, ) -> Configuration<E, I, L, B, O, FingerprintLegacy<EXPECTED>, FO>

Enables legacy fingerprinting with an expected hash.

In this mode, bincode will not calculate the fingerprint automatically. Instead, it will use the provided EXPECTED hash for verification.

Source

pub const fn with_cbor_format( self, ) -> Configuration<E, I, L, B, O, F, CborFormat>

Sets the format to CBOR with default options.

Source

pub const fn with_deterministic_cbor( self, ) -> Configuration<E, I, L, B, O, F, CborDeterministicFormat>

Sets the format to CBOR with deterministic encoding (Core mode).

Source

pub const fn with_deterministic_bincode( self, ) -> Configuration<E, I, L, B, O, F, BincodeDeterministicFormat>

Sets the format to Bincode with deterministic encoding.

Source

pub const fn with_cbor_options<const DET: u8, const PREF: bool, const NAN: bool, const NZ: bool, const INDEF: bool, const TAGS: bool>( self, ) -> Configuration<E, I, L, B, O, F, CborConfig<DET, PREF, NAN, NZ, INDEF, TAGS>>

Sets the format to CBOR with custom options.

Trait Implementations§

Source§

impl<E: Clone, I: Clone, L: Clone, B: Clone, O: Clone, F: Clone, FO: Clone> Clone for Configuration<E, I, L, B, O, F, FO>

Source§

fn clone(&self) -> Configuration<E, I, L, B, O, F, FO>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<E: Copy, I: Copy, L: Copy, B: Copy, O: Copy, F: Copy, FO: Copy> Copy for Configuration<E, I, L, B, O, F, FO>

Source§

impl<E: Debug, I: Debug, L: Debug, B: Debug, O: Debug, F: Debug, FO: Debug> Debug for Configuration<E, I, L, B, O, F, FO>

Source§

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

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

impl<E, I, L, B, O, F, FO> Default for Configuration<E, I, L, B, O, F, FO>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<E, I, L, B, O, F, FO> Freeze for Configuration<E, I, L, B, O, F, FO>

§

impl<E, I, L, B, O, F, FO> RefUnwindSafe for Configuration<E, I, L, B, O, F, FO>

§

impl<E, I, L, B, O, F, FO> Send for Configuration<E, I, L, B, O, F, FO>
where E: Send, I: Send, L: Send, B: Send, O: Send, F: Send, FO: Send,

§

impl<E, I, L, B, O, F, FO> Sync for Configuration<E, I, L, B, O, F, FO>
where E: Sync, I: Sync, L: Sync, B: Sync, O: Sync, F: Sync, FO: Sync,

§

impl<E, I, L, B, O, F, FO> Unpin for Configuration<E, I, L, B, O, F, FO>
where E: Unpin, I: Unpin, L: Unpin, B: Unpin, O: Unpin, F: Unpin, FO: Unpin,

§

impl<E, I, L, B, O, F, FO> UnsafeUnpin for Configuration<E, I, L, B, O, F, FO>

§

impl<E, I, L, B, O, F, FO> UnwindSafe for Configuration<E, I, L, B, O, F, FO>

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

impl<T> Config for T
where T: InternalEndianConfig + InternalIntEncodingConfig + InternalLimitConfig + InternalBitPackingConfig + InternalBitOrderingConfig + InternalFingerprintConfig + InternalFormatConfig + InternalConfigFingerprint + InternalFingerprintConfigExt + Copy + Clone,

Source§

fn endianness(&self) -> Endianness

This configuration’s Endianness
Source§

fn int_encoding(&self) -> IntEncoding

This configuration’s Integer Encoding
Source§

fn limit(&self) -> Option<usize>

This configuration’s byte limit, or None if no limit is configured
Source§

fn bit_packing_enabled(&self) -> bool

Whether bit-packing is enabled for this configuration
Source§

fn bit_ordering(&self) -> BitOrdering

Returns the bit ordering of this configuration
Source§

fn fingerprint_mode(&self) -> FingerprintMode

Returns the fingerprint mode of this configuration
Source§

fn format(&self) -> Format

Returns the format of this configuration
Source§

fn cbor_options(&self) -> CborOptions

Returns the CBOR options of this configuration. Returns default options if the format is not CBOR.
Source§

fn is_compact_net(&self) -> bool

Returns true if this configuration uses compact network encoding (omitting flowinfo/scope_id).
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,

Source§

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>,

Source§

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>,

Source§

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.