Struct bincode::config::Configuration
source · [−]pub struct Configuration<E = LittleEndian, I = Varint, A = WriteFixedArrayLength, 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
sourceimpl<E, I, A, L> Configuration<E, I, A, L>
impl<E, I, A, L> Configuration<E, I, A, L>
sourcepub const fn with_big_endian(self) -> Configuration<BigEndian, I, A, L>
pub const fn with_big_endian(self) -> Configuration<BigEndian, I, A, L>
Makes bincode encode all integer types in big endian.
sourcepub const fn with_little_endian(self) -> Configuration<LittleEndian, I, A, L>
pub const fn with_little_endian(self) -> Configuration<LittleEndian, I, A, L>
Makes bincode encode all integer types in little endian.
sourcepub const fn with_variable_int_encoding(self) -> Configuration<E, Varint, A, L>
pub const fn with_variable_int_encoding(self) -> Configuration<E, Varint, A, 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:
- If
u < 251
, encode it as a single byte with that value. - If
251 <= u < 2**16
, encode it as a literal byte 251, followed by a u16 with valueu
. - If
2**16 <= u < 2**32
, encode it as a literal byte 252, followed by a u32 with valueu
. - If
2**32 <= u < 2**64
, encode it as a literal byte 253, followed by a u64 with valueu
. - If
2**64 <= u < 2**128
, encode it as a literal byte 254, followed by a u128 with valueu
.
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.
sourcepub const fn with_fixed_int_encoding(self) -> Configuration<E, Fixint, A, L>
pub const fn with_fixed_int_encoding(self) -> Configuration<E, Fixint, A, L>
Fixed-size integer encoding.
- Fixed size integers are encoded directly
- Enum discriminants are encoded as u32
- Lengths and usize are encoded as u64
sourcepub const fn skip_fixed_array_length(
self
) -> Configuration<E, I, SkipFixedArrayLength, L>
pub const fn skip_fixed_array_length(
self
) -> Configuration<E, I, SkipFixedArrayLength, L>
Skip writing the length of fixed size arrays ([u8; N]
) before writing the array
NOTE: This is not supported if you’re using the bincode::serde::*
functions, the #[bincode(with_serde)]
attribute, or the Compat
struct.
sourcepub const fn write_fixed_array_length(
self
) -> Configuration<E, I, WriteFixedArrayLength, L>
pub const fn write_fixed_array_length(
self
) -> Configuration<E, I, WriteFixedArrayLength, L>
Write the length of fixed size arrays ([u8; N]
) before writing the array
sourcepub const fn with_limit<const N: usize>(
self
) -> Configuration<E, I, A, Limit<N>>
pub const fn with_limit<const N: usize>(
self
) -> Configuration<E, I, A, Limit<N>>
Sets the byte limit to limit
.
sourcepub const fn with_no_limit(self) -> Configuration<E, I, A, NoLimit>
pub const fn with_no_limit(self) -> Configuration<E, I, A, NoLimit>
Clear the byte limit.
Trait Implementations
sourceimpl<E: Clone, I: Clone, A: Clone, L: Clone> Clone for Configuration<E, I, A, L>
impl<E: Clone, I: Clone, A: Clone, L: Clone> Clone for Configuration<E, I, A, L>
sourcefn clone(&self) -> Configuration<E, I, A, L>
fn clone(&self) -> Configuration<E, I, A, L>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more