Replace a macro with a function
This commit is contained in:
parent
1ef685ea39
commit
050cee48f8
@ -1,22 +1,19 @@
|
|||||||
#![macro_use]
|
/// Returns the longest LEB128 encoding for `T`, assuming `T` is an integer type
|
||||||
|
pub const fn max_leb128_len<T>() -> usize {
|
||||||
macro_rules! max_leb128_len {
|
|
||||||
($int_ty:ty) => {
|
|
||||||
// The longest LEB128 encoding for an integer uses 7 bits per byte.
|
// The longest LEB128 encoding for an integer uses 7 bits per byte.
|
||||||
(std::mem::size_of::<$int_ty>() * 8 + 6) / 7
|
(std::mem::size_of::<T>() * 8 + 6) / 7
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the longest LEB128 encoding of all supported integer types.
|
/// Returns the longest LEB128 encoding of all supported integer types.
|
||||||
pub const fn max_leb128_len() -> usize {
|
pub const fn max_max_leb128_len() -> usize {
|
||||||
max_leb128_len!(u128)
|
max_leb128_len::<u128>()
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_write_unsigned_leb128 {
|
macro_rules! impl_write_unsigned_leb128 {
|
||||||
($fn_name:ident, $int_ty:ty) => {
|
($fn_name:ident, $int_ty:ty) => {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn $fn_name(
|
pub fn $fn_name(
|
||||||
out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len!($int_ty)],
|
out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len::<$int_ty>()],
|
||||||
mut value: $int_ty,
|
mut value: $int_ty,
|
||||||
) -> &[u8] {
|
) -> &[u8] {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
@ -90,7 +87,7 @@ macro_rules! impl_write_signed_leb128 {
|
|||||||
($fn_name:ident, $int_ty:ty) => {
|
($fn_name:ident, $int_ty:ty) => {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn $fn_name(
|
pub fn $fn_name(
|
||||||
out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len!($int_ty)],
|
out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len::<$int_ty>()],
|
||||||
mut value: $int_ty,
|
mut value: $int_ty,
|
||||||
) -> &[u8] {
|
) -> &[u8] {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::leb128::{self, max_leb128_len};
|
use crate::leb128::{self, max_max_leb128_len};
|
||||||
use crate::serialize::{Decodable, Decoder, Encodable, Encoder};
|
use crate::serialize::{Decodable, Decoder, Encodable, Encoder};
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@ -32,7 +32,7 @@ pub fn finish(self) -> Vec<u8> {
|
|||||||
|
|
||||||
macro_rules! write_leb128 {
|
macro_rules! write_leb128 {
|
||||||
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
|
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
|
||||||
const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty);
|
const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>();
|
||||||
let old_len = $enc.data.len();
|
let old_len = $enc.data.len();
|
||||||
|
|
||||||
if MAX_ENCODED_LEN > $enc.data.capacity() - old_len {
|
if MAX_ENCODED_LEN > $enc.data.capacity() - old_len {
|
||||||
@ -186,12 +186,12 @@ pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
|
|||||||
pub fn with_capacity<P: AsRef<Path>>(path: P, capacity: usize) -> io::Result<Self> {
|
pub fn with_capacity<P: AsRef<Path>>(path: P, capacity: usize) -> io::Result<Self> {
|
||||||
// Require capacity at least as large as the largest LEB128 encoding
|
// Require capacity at least as large as the largest LEB128 encoding
|
||||||
// here, so that we don't have to check or handle this on every write.
|
// here, so that we don't have to check or handle this on every write.
|
||||||
assert!(capacity >= max_leb128_len());
|
assert!(capacity >= max_max_leb128_len());
|
||||||
|
|
||||||
// Require capacity small enough such that some capacity checks can be
|
// Require capacity small enough such that some capacity checks can be
|
||||||
// done using guaranteed non-overflowing add rather than sub, which
|
// done using guaranteed non-overflowing add rather than sub, which
|
||||||
// shaves an instruction off those code paths (on x86 at least).
|
// shaves an instruction off those code paths (on x86 at least).
|
||||||
assert!(capacity <= usize::MAX - max_leb128_len());
|
assert!(capacity <= usize::MAX - max_max_leb128_len());
|
||||||
|
|
||||||
// Create the file for reading and writing, because some encoders do both
|
// Create the file for reading and writing, because some encoders do both
|
||||||
// (e.g. the metadata encoder when -Zmeta-stats is enabled)
|
// (e.g. the metadata encoder when -Zmeta-stats is enabled)
|
||||||
@ -411,7 +411,7 @@ fn drop(&mut self) {
|
|||||||
|
|
||||||
macro_rules! file_encoder_write_leb128 {
|
macro_rules! file_encoder_write_leb128 {
|
||||||
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
|
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
|
||||||
const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty);
|
const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>();
|
||||||
|
|
||||||
// We ensure this during `FileEncoder` construction.
|
// We ensure this during `FileEncoder` construction.
|
||||||
debug_assert!($enc.capacity() >= MAX_ENCODED_LEN);
|
debug_assert!($enc.capacity() >= MAX_ENCODED_LEN);
|
||||||
|
Loading…
Reference in New Issue
Block a user