Factor out more repeated code in {write,read}_leb128!.

Make them generate the entire function, not just the function body.
This commit is contained in:
Nicholas Nethercote 2023-05-04 13:10:22 +10:00
parent 4ac959a3c0
commit 723ca2a33d

View File

@ -266,51 +266,40 @@ fn drop(&mut self) {
}
macro_rules! write_leb128 {
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>();
($this_fn:ident, $int_ty:ty, $write_leb_fn:ident) => {
#[inline]
fn $this_fn(&mut self, v: $int_ty) {
const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>();
// We ensure this during `FileEncoder` construction.
debug_assert!($enc.capacity() >= MAX_ENCODED_LEN);
// We ensure this during `FileEncoder` construction.
debug_assert!(self.capacity() >= MAX_ENCODED_LEN);
let mut buffered = $enc.buffered;
let mut buffered = self.buffered;
// This can't overflow. See assertion in `FileEncoder::with_capacity`.
if std::intrinsics::unlikely(buffered + MAX_ENCODED_LEN > $enc.capacity()) {
$enc.flush();
buffered = 0;
// This can't overflow. See assertion in `FileEncoder::with_capacity`.
if std::intrinsics::unlikely(buffered + MAX_ENCODED_LEN > self.capacity()) {
self.flush();
buffered = 0;
}
// SAFETY: The above check and flush ensures that there is enough
// room to write the encoded value to the buffer.
let buf = unsafe {
&mut *(self.buf.as_mut_ptr().add(buffered)
as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN])
};
let encoded = leb128::$write_leb_fn(buf, v);
self.buffered = buffered + encoded.len();
}
// SAFETY: The above check and flush ensures that there is enough
// room to write the encoded value to the buffer.
let buf = unsafe {
&mut *($enc.buf.as_mut_ptr().add(buffered) as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN])
};
let encoded = leb128::$fun(buf, $value);
$enc.buffered = buffered + encoded.len();
}};
};
}
impl Encoder for FileEncoder {
#[inline]
fn emit_usize(&mut self, v: usize) {
write_leb128!(self, v, usize, write_usize_leb128)
}
#[inline]
fn emit_u128(&mut self, v: u128) {
write_leb128!(self, v, u128, write_u128_leb128)
}
#[inline]
fn emit_u64(&mut self, v: u64) {
write_leb128!(self, v, u64, write_u64_leb128)
}
#[inline]
fn emit_u32(&mut self, v: u32) {
write_leb128!(self, v, u32, write_u32_leb128)
}
write_leb128!(emit_usize, usize, write_usize_leb128);
write_leb128!(emit_u128, u128, write_u128_leb128);
write_leb128!(emit_u64, u64, write_u64_leb128);
write_leb128!(emit_u32, u32, write_u32_leb128);
#[inline]
fn emit_u16(&mut self, v: u16) {
@ -322,25 +311,10 @@ fn emit_u8(&mut self, v: u8) {
self.write_one(v);
}
#[inline]
fn emit_isize(&mut self, v: isize) {
write_leb128!(self, v, isize, write_isize_leb128)
}
#[inline]
fn emit_i128(&mut self, v: i128) {
write_leb128!(self, v, i128, write_i128_leb128)
}
#[inline]
fn emit_i64(&mut self, v: i64) {
write_leb128!(self, v, i64, write_i64_leb128)
}
#[inline]
fn emit_i32(&mut self, v: i32) {
write_leb128!(self, v, i32, write_i32_leb128)
}
write_leb128!(emit_isize, isize, write_isize_leb128);
write_leb128!(emit_i128, i128, write_i128_leb128);
write_leb128!(emit_i64, i64, write_i64_leb128);
write_leb128!(emit_i32, i32, write_i32_leb128);
#[inline]
fn emit_i16(&mut self, v: i16) {
@ -437,29 +411,19 @@ fn drop(&mut self) {
}
macro_rules! read_leb128 {
($dec:expr, $fun:ident) => {{ leb128::$fun($dec) }};
($this_fn:ident, $int_ty:ty, $read_leb_fn:ident) => {
#[inline]
fn $this_fn(&mut self) -> $int_ty {
leb128::$read_leb_fn(self)
}
};
}
impl<'a> Decoder for MemDecoder<'a> {
#[inline]
fn read_usize(&mut self) -> usize {
read_leb128!(self, read_usize_leb128)
}
#[inline]
fn read_u128(&mut self) -> u128 {
read_leb128!(self, read_u128_leb128)
}
#[inline]
fn read_u64(&mut self) -> u64 {
read_leb128!(self, read_u64_leb128)
}
#[inline]
fn read_u32(&mut self) -> u32 {
read_leb128!(self, read_u32_leb128)
}
read_leb128!(read_usize, usize, read_usize_leb128);
read_leb128!(read_u128, u128, read_u128_leb128);
read_leb128!(read_u64, u64, read_u64_leb128);
read_leb128!(read_u32, u32, read_u32_leb128);
#[inline]
fn read_u16(&mut self) -> u16 {
@ -479,25 +443,10 @@ fn read_u8(&mut self) -> u8 {
}
}
#[inline]
fn read_isize(&mut self) -> isize {
read_leb128!(self, read_isize_leb128)
}
#[inline]
fn read_i128(&mut self) -> i128 {
read_leb128!(self, read_i128_leb128)
}
#[inline]
fn read_i64(&mut self) -> i64 {
read_leb128!(self, read_i64_leb128)
}
#[inline]
fn read_i32(&mut self) -> i32 {
read_leb128!(self, read_i32_leb128)
}
read_leb128!(read_isize, isize, read_isize_leb128);
read_leb128!(read_i128, i128, read_i128_leb128);
read_leb128!(read_i64, i64, read_i64_leb128);
read_leb128!(read_i32, i32, read_i32_leb128);
#[inline]
fn read_i16(&mut self) -> i16 {