From 050cee48f83b39a0f532e4905d02f07716bf2ee3 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 29 Nov 2022 11:52:48 +0000 Subject: [PATCH 1/2] Replace a macro with a function --- compiler/rustc_serialize/src/leb128.rs | 19 ++++++++----------- compiler/rustc_serialize/src/opaque.rs | 10 +++++----- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_serialize/src/leb128.rs b/compiler/rustc_serialize/src/leb128.rs index a6cdd32f23b8e..c5297bff193c3 100644 --- a/compiler/rustc_serialize/src/leb128.rs +++ b/compiler/rustc_serialize/src/leb128.rs @@ -1,22 +1,19 @@ -#![macro_use] - -macro_rules! max_leb128_len { - ($int_ty:ty) => { - // The longest LEB128 encoding for an integer uses 7 bits per byte. - (std::mem::size_of::<$int_ty>() * 8 + 6) / 7 - }; +/// Returns the longest LEB128 encoding for `T`, assuming `T` is an integer type +pub const fn max_leb128_len() -> usize { + // The longest LEB128 encoding for an integer uses 7 bits per byte. + (std::mem::size_of::() * 8 + 6) / 7 } /// Returns the longest LEB128 encoding of all supported integer types. -pub const fn max_leb128_len() -> usize { - max_leb128_len!(u128) +pub const fn max_max_leb128_len() -> usize { + max_leb128_len::() } macro_rules! impl_write_unsigned_leb128 { ($fn_name:ident, $int_ty:ty) => { #[inline] pub fn $fn_name( - out: &mut [::std::mem::MaybeUninit; max_leb128_len!($int_ty)], + out: &mut [::std::mem::MaybeUninit; max_leb128_len::<$int_ty>()], mut value: $int_ty, ) -> &[u8] { let mut i = 0; @@ -90,7 +87,7 @@ macro_rules! impl_write_signed_leb128 { ($fn_name:ident, $int_ty:ty) => { #[inline] pub fn $fn_name( - out: &mut [::std::mem::MaybeUninit; max_leb128_len!($int_ty)], + out: &mut [::std::mem::MaybeUninit; max_leb128_len::<$int_ty>()], mut value: $int_ty, ) -> &[u8] { let mut i = 0; diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 7c54df809f179..7cd513e1eb925 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -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 std::convert::TryInto; use std::fs::File; @@ -32,7 +32,7 @@ impl MemEncoder { macro_rules! write_leb128 { ($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(); if MAX_ENCODED_LEN > $enc.data.capacity() - old_len { @@ -186,12 +186,12 @@ impl FileEncoder { pub fn with_capacity>(path: P, capacity: usize) -> io::Result { // 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. - assert!(capacity >= max_leb128_len()); + assert!(capacity >= max_max_leb128_len()); // Require capacity small enough such that some capacity checks can be // done using guaranteed non-overflowing add rather than sub, which // 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 // (e.g. the metadata encoder when -Zmeta-stats is enabled) @@ -411,7 +411,7 @@ impl Drop for FileEncoder { macro_rules! file_encoder_write_leb128 { ($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. debug_assert!($enc.capacity() >= MAX_ENCODED_LEN); From 8c0951511b25dbe911dd7b1f880deaf06aad0351 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 29 Nov 2022 17:58:09 +0000 Subject: [PATCH 2/2] rename `{max=>largest}_max_leb128_len` --- compiler/rustc_serialize/src/leb128.rs | 6 +++--- compiler/rustc_serialize/src/opaque.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_serialize/src/leb128.rs b/compiler/rustc_serialize/src/leb128.rs index c5297bff193c3..7dad9aa01fafd 100644 --- a/compiler/rustc_serialize/src/leb128.rs +++ b/compiler/rustc_serialize/src/leb128.rs @@ -1,11 +1,11 @@ -/// Returns the longest LEB128 encoding for `T`, assuming `T` is an integer type +/// Returns the length of the longest LEB128 encoding for `T`, assuming `T` is an integer type pub const fn max_leb128_len() -> usize { // The longest LEB128 encoding for an integer uses 7 bits per byte. (std::mem::size_of::() * 8 + 6) / 7 } -/// Returns the longest LEB128 encoding of all supported integer types. -pub const fn max_max_leb128_len() -> usize { +/// Returns the length of the longest LEB128 encoding of all supported integer types. +pub const fn largest_max_leb128_len() -> usize { max_leb128_len::() } diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 7cd513e1eb925..0afeb86fceb24 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,4 +1,4 @@ -use crate::leb128::{self, max_max_leb128_len}; +use crate::leb128::{self, largest_max_leb128_len}; use crate::serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::fs::File; @@ -186,12 +186,12 @@ impl FileEncoder { pub fn with_capacity>(path: P, capacity: usize) -> io::Result { // 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. - assert!(capacity >= max_max_leb128_len()); + assert!(capacity >= largest_max_leb128_len()); // Require capacity small enough such that some capacity checks can be // done using guaranteed non-overflowing add rather than sub, which // shaves an instruction off those code paths (on x86 at least). - assert!(capacity <= usize::MAX - max_max_leb128_len()); + assert!(capacity <= usize::MAX - largest_max_leb128_len()); // Create the file for reading and writing, because some encoders do both // (e.g. the metadata encoder when -Zmeta-stats is enabled)