mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Reduce integer Display
implementation size
This commit is contained in:
parent
7d40450b2d
commit
d318878c5f
@ -199,32 +199,12 @@ static DEC_DIGITS_LUT: &[u8; 200] = b"0001020304050607080910111213141516171819\
|
|||||||
8081828384858687888990919293949596979899";
|
8081828384858687888990919293949596979899";
|
||||||
|
|
||||||
macro_rules! impl_Display {
|
macro_rules! impl_Display {
|
||||||
($($t:ident $(as $positive:ident)? named $name:ident,)* ; as $u:ident via $conv_fn:ident named $gen_name:ident) => {
|
($($signed:ident, $unsigned:ident,)* ; as $u:ident via $conv_fn:ident named $gen_name:ident) => {
|
||||||
|
|
||||||
$(
|
$(
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl fmt::Display for $t {
|
impl fmt::Display for $unsigned {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
// If it's a signed integer.
|
|
||||||
$(
|
|
||||||
let is_nonnegative = *self >= 0;
|
|
||||||
|
|
||||||
#[cfg(not(feature = "optimize_for_size"))]
|
|
||||||
{
|
|
||||||
if !is_nonnegative {
|
|
||||||
// convert the negative num to positive by summing 1 to its 2s complement
|
|
||||||
return (!self as $positive).wrapping_add(1)._fmt(false, f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[cfg(feature = "optimize_for_size")]
|
|
||||||
{
|
|
||||||
if !is_nonnegative {
|
|
||||||
// convert the negative num to positive by summing 1 to its 2s complement
|
|
||||||
return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)?
|
|
||||||
// If it's a positive integer.
|
|
||||||
#[cfg(not(feature = "optimize_for_size"))]
|
#[cfg(not(feature = "optimize_for_size"))]
|
||||||
{
|
{
|
||||||
self._fmt(true, f)
|
self._fmt(true, f)
|
||||||
@ -236,10 +216,40 @@ macro_rules! impl_Display {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl fmt::Display for $signed {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let is_nonnegative = *self >= 0;
|
||||||
|
|
||||||
|
if !is_nonnegative {
|
||||||
|
#[cfg(not(feature = "optimize_for_size"))]
|
||||||
|
{
|
||||||
|
// convert the negative num to positive by summing 1 to its 2s complement
|
||||||
|
return (!self as $unsigned).wrapping_add(1)._fmt(false, f);
|
||||||
|
}
|
||||||
|
#[cfg(feature = "optimize_for_size")]
|
||||||
|
{
|
||||||
|
// convert the negative num to positive by summing 1 to its 2s complement
|
||||||
|
return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's a positive integer.
|
||||||
|
#[cfg(not(feature = "optimize_for_size"))]
|
||||||
|
{
|
||||||
|
(*self as $unsigned)._fmt(true, f)
|
||||||
|
}
|
||||||
|
#[cfg(feature = "optimize_for_size")]
|
||||||
|
{
|
||||||
|
$gen_name(self.$conv_fn(), true, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "optimize_for_size"))]
|
#[cfg(not(feature = "optimize_for_size"))]
|
||||||
impl $t {
|
impl $unsigned {
|
||||||
fn _fmt(mut self: $t, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn _fmt(mut self, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
const SIZE: usize = $t::MAX.ilog(10) as usize + 1;
|
const SIZE: usize = $unsigned::MAX.ilog(10) as usize + 1;
|
||||||
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
|
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
|
||||||
let mut curr = SIZE;
|
let mut curr = SIZE;
|
||||||
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
|
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
|
||||||
@ -258,7 +268,7 @@ macro_rules! impl_Display {
|
|||||||
#[allow(unused_comparisons)]
|
#[allow(unused_comparisons)]
|
||||||
// This block will be removed for smaller types at compile time and in the worst
|
// This block will be removed for smaller types at compile time and in the worst
|
||||||
// case, it will prevent to have the `10000` literal to overflow for `i8` and `u8`.
|
// case, it will prevent to have the `10000` literal to overflow for `i8` and `u8`.
|
||||||
if core::mem::size_of::<$t>() >= 2 {
|
if core::mem::size_of::<$unsigned>() >= 2 {
|
||||||
// eagerly decode 4 characters at a time
|
// eagerly decode 4 characters at a time
|
||||||
while self >= 10000 {
|
while self >= 10000 {
|
||||||
let rem = (self % 10000) as usize;
|
let rem = (self % 10000) as usize;
|
||||||
@ -312,8 +322,8 @@ macro_rules! impl_Display {
|
|||||||
|
|
||||||
#[cfg(feature = "optimize_for_size")]
|
#[cfg(feature = "optimize_for_size")]
|
||||||
fn $gen_name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn $gen_name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
|
const SIZE: usize = $u::MAX.ilog(10) as usize + 1;
|
||||||
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
|
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
|
||||||
let mut curr = buf.len();
|
let mut curr = buf.len();
|
||||||
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
|
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
|
||||||
|
|
||||||
@ -523,16 +533,11 @@ impl_Debug! {
|
|||||||
mod imp {
|
mod imp {
|
||||||
use super::*;
|
use super::*;
|
||||||
impl_Display!(
|
impl_Display!(
|
||||||
i8 as u8 named fmt_i8,
|
i8, u8,
|
||||||
u8 named fmt_u8,
|
i16, u16,
|
||||||
i16 as u16 named fmt_i16,
|
i32, u32,
|
||||||
u16 named fmt_u16,
|
i64, u64,
|
||||||
i32 as u32 named fmt_i32,
|
isize, usize,
|
||||||
u32 named fmt_u32,
|
|
||||||
i64 as u64 named fmt_i64,
|
|
||||||
u64 named fmt_u64,
|
|
||||||
isize as usize named fmt_isize,
|
|
||||||
usize named fmt_usize,
|
|
||||||
; as u64 via to_u64 named fmt_u64
|
; as u64 via to_u64 named fmt_u64
|
||||||
);
|
);
|
||||||
impl_Exp!(
|
impl_Exp!(
|
||||||
@ -545,18 +550,13 @@ mod imp {
|
|||||||
mod imp {
|
mod imp {
|
||||||
use super::*;
|
use super::*;
|
||||||
impl_Display!(
|
impl_Display!(
|
||||||
i8 as u8 named fmt_i8,
|
i8, u8,
|
||||||
u8 named fmt_u8,
|
i16, u16,
|
||||||
i16 as u16 named fmt_i16,
|
i32, u32,
|
||||||
u16 named fmt_u16,
|
isize, usize,
|
||||||
i32 as u32 named fmt_i32,
|
|
||||||
u32 named fmt_u32,
|
|
||||||
isize as usize named fmt_isize,
|
|
||||||
usize named fmt_usize,
|
|
||||||
; as u32 via to_u32 named fmt_u32);
|
; as u32 via to_u32 named fmt_u32);
|
||||||
impl_Display!(
|
impl_Display!(
|
||||||
i64 as u64 named fmt_i64,
|
i64, u64,
|
||||||
u64 named fmt_u64,
|
|
||||||
; as u64 via to_u64 named fmt_u64);
|
; as u64 via to_u64 named fmt_u64);
|
||||||
|
|
||||||
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
|
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
|
||||||
|
Loading…
Reference in New Issue
Block a user