postcard/
varint.rs

1/// Returns the maximum number of bytes required to encode T.
2pub const fn varint_max<T: Sized>() -> usize {
3    const BITS_PER_BYTE: usize = 8;
4    const BITS_PER_VARINT_BYTE: usize = 7;
5
6    // How many data bits do we need for this type?
7    let bits = core::mem::size_of::<T>() * BITS_PER_BYTE;
8
9    // We add (BITS_PER_VARINT_BYTE - 1), to ensure any integer divisions
10    // with a remainder will always add exactly one full byte, but
11    // an evenly divided number of bits will be the same
12    let roundup_bits = bits + (BITS_PER_VARINT_BYTE - 1);
13
14    // Apply division, using normal "round down" integer division
15    roundup_bits / BITS_PER_VARINT_BYTE
16}
17
18/// Returns the maximum value stored in the last encoded byte.
19pub const fn max_of_last_byte<T: Sized>() -> u8 {
20    let max_bits = core::mem::size_of::<T>() * 8;
21    let extra_bits = max_bits % 7;
22    (1 << extra_bits) - 1
23}
24
25#[inline]
26pub fn varint_usize(n: usize, out: &mut [u8; varint_max::<usize>()]) -> &mut [u8] {
27    let mut value = n;
28    for i in 0..varint_max::<usize>() {
29        out[i] = value.to_le_bytes()[0];
30        if value < 128 {
31            return &mut out[..=i];
32        }
33
34        out[i] |= 0x80;
35        value >>= 7;
36    }
37    debug_assert_eq!(value, 0);
38    &mut out[..]
39}
40
41#[inline]
42pub fn varint_u16(n: u16, out: &mut [u8; varint_max::<u16>()]) -> &mut [u8] {
43    let mut value = n;
44    for i in 0..varint_max::<u16>() {
45        out[i] = value.to_le_bytes()[0];
46        if value < 128 {
47            return &mut out[..=i];
48        }
49
50        out[i] |= 0x80;
51        value >>= 7;
52    }
53    debug_assert_eq!(value, 0);
54    &mut out[..]
55}
56
57#[inline]
58pub fn varint_u32(n: u32, out: &mut [u8; varint_max::<u32>()]) -> &mut [u8] {
59    let mut value = n;
60    for i in 0..varint_max::<u32>() {
61        out[i] = value.to_le_bytes()[0];
62        if value < 128 {
63            return &mut out[..=i];
64        }
65
66        out[i] |= 0x80;
67        value >>= 7;
68    }
69    debug_assert_eq!(value, 0);
70    &mut out[..]
71}
72
73#[inline]
74pub fn varint_u64(n: u64, out: &mut [u8; varint_max::<u64>()]) -> &mut [u8] {
75    let mut value = n;
76    for i in 0..varint_max::<u64>() {
77        out[i] = value.to_le_bytes()[0];
78        if value < 128 {
79            return &mut out[..=i];
80        }
81
82        out[i] |= 0x80;
83        value >>= 7;
84    }
85    debug_assert_eq!(value, 0);
86    &mut out[..]
87}
88
89#[inline]
90pub fn varint_u128(n: u128, out: &mut [u8; varint_max::<u128>()]) -> &mut [u8] {
91    let mut value = n;
92    for i in 0..varint_max::<u128>() {
93        out[i] = value.to_le_bytes()[0];
94        if value < 128 {
95            return &mut out[..=i];
96        }
97
98        out[i] |= 0x80;
99        value >>= 7;
100    }
101    debug_assert_eq!(value, 0);
102    &mut out[..]
103}