1 /// Returns the length of the longest LEB128 encoding for `T`, assuming `T` is an integer type
2 pub const fn max_leb128_len<T>() -> usize {
3 // The longest LEB128 encoding for an integer uses 7 bits per byte.
4 (std::mem::size_of::<T>() * 8 + 6) / 7
7 /// Returns the length of the longest LEB128 encoding of all supported integer types.
8 pub const fn largest_max_leb128_len() -> usize {
9 max_leb128_len::<u128>()
12 macro_rules! impl_write_unsigned_leb128 {
13 ($fn_name:ident, $int_ty:ty) => {
16 out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len::<$int_ty>()],
24 *out.get_unchecked_mut(i).as_mut_ptr() = value as u8;
31 *out.get_unchecked_mut(i).as_mut_ptr() = ((value & 0x7f) | 0x80) as u8;
39 unsafe { ::std::mem::MaybeUninit::slice_assume_init_ref(&out.get_unchecked(..i)) }
44 impl_write_unsigned_leb128!(write_u16_leb128, u16);
45 impl_write_unsigned_leb128!(write_u32_leb128, u32);
46 impl_write_unsigned_leb128!(write_u64_leb128, u64);
47 impl_write_unsigned_leb128!(write_u128_leb128, u128);
48 impl_write_unsigned_leb128!(write_usize_leb128, usize);
50 macro_rules! impl_read_unsigned_leb128 {
51 ($fn_name:ident, $int_ty:ty) => {
53 pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
54 // The first iteration of this loop is unpeeled. This is a
55 // performance win because this code is hot and integer values less
56 // than 128 are very common, typically occurring 50-80% or more of
57 // the time, even for u64 and u128.
58 let byte = slice[*position];
60 if (byte & 0x80) == 0 {
61 return byte as $int_ty;
63 let mut result = (byte & 0x7F) as $int_ty;
66 let byte = slice[*position];
68 if (byte & 0x80) == 0 {
69 result |= (byte as $int_ty) << shift;
72 result |= ((byte & 0x7F) as $int_ty) << shift;
80 impl_read_unsigned_leb128!(read_u16_leb128, u16);
81 impl_read_unsigned_leb128!(read_u32_leb128, u32);
82 impl_read_unsigned_leb128!(read_u64_leb128, u64);
83 impl_read_unsigned_leb128!(read_u128_leb128, u128);
84 impl_read_unsigned_leb128!(read_usize_leb128, usize);
86 macro_rules! impl_write_signed_leb128 {
87 ($fn_name:ident, $int_ty:ty) => {
90 out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len::<$int_ty>()],
96 let mut byte = (value as u8) & 0x7f;
98 let more = !(((value == 0) && ((byte & 0x40) == 0))
99 || ((value == -1) && ((byte & 0x40) != 0)));
102 byte |= 0x80; // Mark this byte to show that more bytes will follow.
106 *out.get_unchecked_mut(i).as_mut_ptr() = byte;
116 unsafe { ::std::mem::MaybeUninit::slice_assume_init_ref(&out.get_unchecked(..i)) }
121 impl_write_signed_leb128!(write_i16_leb128, i16);
122 impl_write_signed_leb128!(write_i32_leb128, i32);
123 impl_write_signed_leb128!(write_i64_leb128, i64);
124 impl_write_signed_leb128!(write_i128_leb128, i128);
125 impl_write_signed_leb128!(write_isize_leb128, isize);
127 macro_rules! impl_read_signed_leb128 {
128 ($fn_name:ident, $int_ty:ty) => {
130 pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
136 byte = slice[*position];
138 result |= <$int_ty>::from(byte & 0x7F) << shift;
141 if (byte & 0x80) == 0 {
146 if (shift < <$int_ty>::BITS) && ((byte & 0x40) != 0) {
148 result |= (!0 << shift);
156 impl_read_signed_leb128!(read_i16_leb128, i16);
157 impl_read_signed_leb128!(read_i32_leb128, i32);
158 impl_read_signed_leb128!(read_i64_leb128, i64);
159 impl_read_signed_leb128!(read_i128_leb128, i128);
160 impl_read_signed_leb128!(read_isize_leb128, isize);