@@ -49,15 +49,13 @@ doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
49
49
#[ doc( hidden) ]
50
50
trait GenericRadix {
51
51
/// The number of digits.
52
- fn base ( & self ) -> u8 ;
52
+ const BASE : u8 ;
53
53
54
54
/// A radix-specific prefix string.
55
- fn prefix ( & self ) -> & ' static str {
56
- ""
57
- }
55
+ const PREFIX : & ' static str ;
58
56
59
57
/// Converts an integer to corresponding radix digit.
60
- fn digit ( & self , x : u8 ) -> u8 ;
58
+ fn digit ( x : u8 ) -> u8 ;
61
59
62
60
/// Format an integer using the radix using a formatter.
63
61
fn fmt_int < T : Int > ( & self , mut x : T , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -67,14 +65,14 @@ trait GenericRadix {
67
65
let is_nonnegative = x >= zero;
68
66
let mut buf = [ 0 ; 128 ] ;
69
67
let mut curr = buf. len ( ) ;
70
- let base = T :: from_u8 ( self . base ( ) ) ;
68
+ let base = T :: from_u8 ( Self :: BASE ) ;
71
69
if is_nonnegative {
72
70
// Accumulate each digit of the number from the least significant
73
71
// to the most significant figure.
74
72
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.
78
76
curr -= 1 ;
79
77
if x == zero {
80
78
// No more digits left to accumulate.
@@ -84,9 +82,9 @@ trait GenericRadix {
84
82
} else {
85
83
// Do the same as above, but accounting for two's complement.
86
84
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.
90
88
curr -= 1 ;
91
89
if x == zero {
92
90
// No more digits left to accumulate.
@@ -95,7 +93,7 @@ trait GenericRadix {
95
93
}
96
94
}
97
95
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)
99
97
}
100
98
}
101
99
@@ -122,12 +120,12 @@ struct UpperHex;
122
120
macro_rules! radix {
123
121
( $T: ident, $base: expr, $prefix: expr, $( $x: pat => $conv: expr) ,+) => {
124
122
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 {
128
126
match x {
129
127
$( $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) ,
131
129
}
132
130
}
133
131
}
0 commit comments