Rollup merge of #49099 - glandium:master, r=sfackler

Use associated consts for GenericRadix base and prefix

The trait being private, this does not imply an API change.
This commit is contained in:
kennytm 2018-03-20 07:15:22 +08:00 committed by GitHub
commit 63ab36190d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,15 +49,13 @@ doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
#[doc(hidden)] #[doc(hidden)]
trait GenericRadix { trait GenericRadix {
/// The number of digits. /// The number of digits.
fn base(&self) -> u8; const BASE: u8;
/// A radix-specific prefix string. /// A radix-specific prefix string.
fn prefix(&self) -> &'static str { const PREFIX: &'static str;
""
}
/// Converts an integer to corresponding radix digit. /// Converts an integer to corresponding radix digit.
fn digit(&self, x: u8) -> u8; fn digit(x: u8) -> u8;
/// Format an integer using the radix using a formatter. /// Format an integer using the radix using a formatter.
fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result { fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
@ -67,14 +65,14 @@ trait GenericRadix {
let is_nonnegative = x >= zero; let is_nonnegative = x >= zero;
let mut buf = [0; 128]; let mut buf = [0; 128];
let mut curr = buf.len(); let mut curr = buf.len();
let base = T::from_u8(self.base()); let base = T::from_u8(Self::BASE);
if is_nonnegative { if is_nonnegative {
// Accumulate each digit of the number from the least significant // Accumulate each digit of the number from the least significant
// to the most significant figure. // to the most significant figure.
for byte in buf.iter_mut().rev() { for byte in buf.iter_mut().rev() {
let n = x % base; // Get the current place value. let n = x % base; // Get the current place value.
x = x / base; // Deaccumulate the number. x = x / base; // Deaccumulate the number.
*byte = self.digit(n.to_u8()); // Store the digit in the buffer. *byte = Self::digit(n.to_u8()); // Store the digit in the buffer.
curr -= 1; curr -= 1;
if x == zero { if x == zero {
// No more digits left to accumulate. // No more digits left to accumulate.
@ -86,7 +84,7 @@ trait GenericRadix {
for byte in buf.iter_mut().rev() { for byte in buf.iter_mut().rev() {
let n = zero - (x % base); // Get the current place value. let n = zero - (x % base); // Get the current place value.
x = x / base; // Deaccumulate the number. x = x / base; // Deaccumulate the number.
*byte = self.digit(n.to_u8()); // Store the digit in the buffer. *byte = Self::digit(n.to_u8()); // Store the digit in the buffer.
curr -= 1; curr -= 1;
if x == zero { if x == zero {
// No more digits left to accumulate. // No more digits left to accumulate.
@ -95,7 +93,7 @@ trait GenericRadix {
} }
} }
let buf = unsafe { str::from_utf8_unchecked(&buf[curr..]) }; let buf = unsafe { str::from_utf8_unchecked(&buf[curr..]) };
f.pad_integral(is_nonnegative, self.prefix(), buf) f.pad_integral(is_nonnegative, Self::PREFIX, buf)
} }
} }
@ -122,12 +120,12 @@ struct UpperHex;
macro_rules! radix { macro_rules! radix {
($T:ident, $base:expr, $prefix:expr, $($x:pat => $conv:expr),+) => { ($T:ident, $base:expr, $prefix:expr, $($x:pat => $conv:expr),+) => {
impl GenericRadix for $T { impl GenericRadix for $T {
fn base(&self) -> u8 { $base } const BASE: u8 = $base;
fn prefix(&self) -> &'static str { $prefix } const PREFIX: &'static str = $prefix;
fn digit(&self, x: u8) -> u8 { fn digit(x: u8) -> u8 {
match x { match x {
$($x => $conv,)+ $($x => $conv,)+
x => panic!("number not in the range 0..{}: {}", self.base() - 1, x), x => panic!("number not in the range 0..{}: {}", Self::BASE - 1, x),
} }
} }
} }