Struct bincode::config::VarintEncoding
source · pub struct VarintEncoding;
Expand description
Variable-size integer encoding (excepting [ui]8).
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,
v if v < 0 => |v| * 2 - 1
v if v > 0 => v * 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);
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.
Implementations§
source§impl VarintEncoding
impl VarintEncoding
fn varint_size(n: u64) -> u64
fn zigzag_encode(n: i64) -> u64
fn zigzag_decode(n: u64) -> i64
fn serialize_varint<W: Write, O: Options>( ser: &mut Serializer<W, O>, n: u64, ) -> Result<()>
fn deserialize_varint<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<u64>
fn zigzag128_encode(n: i128) -> u128
fn zigzag128_decode(n: u128) -> i128
fn varint128_size(n: u128) -> u64
fn serialize_varint128<W: Write, O: Options>( ser: &mut Serializer<W, O>, n: u128, ) -> Result<()>
fn deserialize_varint128<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<u128>
Trait Implementations§
source§impl Clone for VarintEncoding
impl Clone for VarintEncoding
source§fn clone(&self) -> VarintEncoding
fn clone(&self) -> VarintEncoding
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl IntEncoding for VarintEncoding
impl IntEncoding for VarintEncoding
fn serialize_u16<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: u16, ) -> Result<()>
fn serialize_u32<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: u32, ) -> Result<()>
fn serialize_u64<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: u64, ) -> Result<()>
fn serialize_i16<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: i16, ) -> Result<()>
fn serialize_i32<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: i32, ) -> Result<()>
fn serialize_i64<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: i64, ) -> Result<()>
fn deserialize_u16<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<u16>
fn deserialize_u32<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<u32>
fn deserialize_u64<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<u64>
fn deserialize_i16<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<i16>
fn deserialize_i32<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<i32>
fn deserialize_i64<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<i64>
fn u128_size(n: u128) -> u64
fn i128_size(n: i128) -> u64
fn serialize_u128<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: u128, ) -> Result<()>
fn serialize_i128<W: Write, O: Options>( ser: &mut Serializer<W, O>, val: i128, ) -> Result<()>
fn deserialize_u128<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<u128>
fn deserialize_i128<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<i128>
fn len_size(len: usize) -> u64
source§fn serialize_len<W: Write, O: Options>(
ser: &mut Serializer<W, O>,
len: usize,
) -> Result<()>
fn serialize_len<W: Write, O: Options>( ser: &mut Serializer<W, O>, len: usize, ) -> Result<()>
Serializes a sequence length.
source§fn deserialize_len<'de, R: BincodeRead<'de>, O: Options>(
de: &mut Deserializer<R, O>,
) -> Result<usize>
fn deserialize_len<'de, R: BincodeRead<'de>, O: Options>( de: &mut Deserializer<R, O>, ) -> Result<usize>
Deserializes a sequence length.
impl Copy for VarintEncoding
Auto Trait Implementations§
impl Freeze for VarintEncoding
impl RefUnwindSafe for VarintEncoding
impl Send for VarintEncoding
impl Sync for VarintEncoding
impl Unpin for VarintEncoding
impl UnwindSafe for VarintEncoding
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more