Skip to content

Replace a macro with a function #105051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions compiler/rustc_serialize/src/leb128.rs
Original file line number Diff line number Diff line change
@@ -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 length of the longest LEB128 encoding for `T`, assuming `T` is an integer type
pub const fn max_leb128_len<T>() -> usize {
// The longest LEB128 encoding for an integer uses 7 bits per byte.
(std::mem::size_of::<T>() * 8 + 6) / 7
}

/// Returns the longest LEB128 encoding of all supported integer types.
pub const fn max_leb128_len() -> usize {
max_leb128_len!(u128)
/// Returns the length of the longest LEB128 encoding of all supported integer types.
pub const fn largest_max_leb128_len() -> usize {
max_leb128_len::<u128>()
}

macro_rules! impl_write_unsigned_leb128 {
($fn_name:ident, $int_ty:ty) => {
#[inline]
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,
) -> &[u8] {
let mut i = 0;
Expand Down Expand Up @@ -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<u8>; max_leb128_len!($int_ty)],
out: &mut [::std::mem::MaybeUninit<u8>; max_leb128_len::<$int_ty>()],
mut value: $int_ty,
) -> &[u8] {
let mut i = 0;
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::leb128::{self, 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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -186,12 +186,12 @@ impl FileEncoder {
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
// here, so that we don't have to check or handle this on every write.
assert!(capacity >= 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_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)
Expand Down Expand Up @@ -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);
Expand Down