Skip to content

Commit b910d6b

Browse files
committedMar 17, 2018
Use associated consts for GenericRadix base and prefix
1 parent 55c984e commit b910d6b

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed
 

‎src/libcore/fmt/num.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
4949
#[doc(hidden)]
5050
trait GenericRadix {
5151
/// The number of digits.
52-
fn base(&self) -> u8;
52+
const BASE: u8;
5353

5454
/// A radix-specific prefix string.
55-
fn prefix(&self) -> &'static str {
56-
""
57-
}
55+
const PREFIX: &'static str;
5856

5957
/// Converts an integer to corresponding radix digit.
60-
fn digit(&self, x: u8) -> u8;
58+
fn digit(x: u8) -> u8;
6159

6260
/// Format an integer using the radix using a formatter.
6361
fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
@@ -67,14 +65,14 @@ trait GenericRadix {
6765
let is_nonnegative = x >= zero;
6866
let mut buf = [0; 128];
6967
let mut curr = buf.len();
70-
let base = T::from_u8(self.base());
68+
let base = T::from_u8(Self::BASE);
7169
if is_nonnegative {
7270
// Accumulate each digit of the number from the least significant
7371
// to the most significant figure.
7472
for byte in buf.iter_mut().rev() {
75-
let n = x % base; // Get the current place value.
76-
x = x / base; // Deaccumulate the number.
77-
*byte = self.digit(n.to_u8()); // Store the digit in the buffer.
73+
let n = x % base; // Get the current place value.
74+
x = x / base; // Deaccumulate the number.
75+
*byte = Self::digit(n.to_u8()); // Store the digit in the buffer.
7876
curr -= 1;
7977
if x == zero {
8078
// No more digits left to accumulate.
@@ -84,9 +82,9 @@ trait GenericRadix {
8482
} else {
8583
// Do the same as above, but accounting for two's complement.
8684
for byte in buf.iter_mut().rev() {
87-
let n = zero - (x % base); // Get the current place value.
88-
x = x / base; // Deaccumulate the number.
89-
*byte = self.digit(n.to_u8()); // Store the digit in the buffer.
85+
let n = zero - (x % base); // Get the current place value.
86+
x = x / base; // Deaccumulate the number.
87+
*byte = Self::digit(n.to_u8()); // Store the digit in the buffer.
9088
curr -= 1;
9189
if x == zero {
9290
// No more digits left to accumulate.
@@ -95,7 +93,7 @@ trait GenericRadix {
9593
}
9694
}
9795
let buf = unsafe { str::from_utf8_unchecked(&buf[curr..]) };
98-
f.pad_integral(is_nonnegative, self.prefix(), buf)
96+
f.pad_integral(is_nonnegative, Self::PREFIX, buf)
9997
}
10098
}
10199

@@ -122,12 +120,12 @@ struct UpperHex;
122120
macro_rules! radix {
123121
($T:ident, $base:expr, $prefix:expr, $($x:pat => $conv:expr),+) => {
124122
impl GenericRadix for $T {
125-
fn base(&self) -> u8 { $base }
126-
fn prefix(&self) -> &'static str { $prefix }
127-
fn digit(&self, x: u8) -> u8 {
123+
const BASE: u8 = $base;
124+
const PREFIX: &'static str = $prefix;
125+
fn digit(x: u8) -> u8 {
128126
match x {
129127
$($x => $conv,)+
130-
x => panic!("number not in the range 0..{}: {}", self.base() - 1, x),
128+
x => panic!("number not in the range 0..{}: {}", Self::BASE - 1, x),
131129
}
132130
}
133131
}

0 commit comments

Comments
 (0)
Please sign in to comment.