11
11
pub use self :: ExponentFormat :: * ;
12
12
pub use self :: SignificantDigits :: * ;
13
13
14
- use char:: { self , CharExt } ;
14
+ use prelude:: * ;
15
+
16
+ use char;
15
17
use fmt;
16
- use iter:: Iterator ;
17
- use num:: { cast, Float , ToPrimitive } ;
18
+ use num:: Float ;
18
19
use num:: FpCategory as Fp ;
19
- use ops:: FnOnce ;
20
- use result:: Result :: Ok ;
21
- use slice:: { self , SliceExt } ;
22
- use str:: { self , StrExt } ;
20
+ use ops:: { Div , Rem , Mul } ;
21
+ use slice;
22
+ use str;
23
23
24
24
/// A flag that specifies whether to use exponential (scientific) notation.
25
25
pub enum ExponentFormat {
@@ -42,6 +42,21 @@ pub enum SignificantDigits {
42
42
DigExact ( usize )
43
43
}
44
44
45
+ #[ doc( hidden) ]
46
+ pub trait MyFloat : Float + PartialEq + PartialOrd + Div < Output =Self > +
47
+ Mul < Output =Self > + Rem < Output =Self > + Copy {
48
+ fn from_u32 ( u : u32 ) -> Self ;
49
+ fn to_i32 ( & self ) -> i32 ;
50
+ }
51
+
52
+ macro_rules! doit {
53
+ ( $( $t: ident) * ) => ( $( impl MyFloat for $t {
54
+ fn from_u32( u: u32 ) -> $t { u as $t }
55
+ fn to_i32( & self ) -> i32 { * self as i32 }
56
+ } ) * )
57
+ }
58
+ doit ! { f32 f64 }
59
+
45
60
/// Converts a float number to its string representation.
46
61
/// This is meant to be a common base implementation for various formatting styles.
47
62
/// The number is assumed to be non-negative, callers use `Formatter::pad_integral`
@@ -63,7 +78,7 @@ pub enum SignificantDigits {
63
78
/// # Panics
64
79
///
65
80
/// - Panics if `num` is negative.
66
- pub fn float_to_str_bytes_common < T : Float , U , F > (
81
+ pub fn float_to_str_bytes_common < T : MyFloat , U , F > (
67
82
num : T ,
68
83
digits : SignificantDigits ,
69
84
exp_format : ExponentFormat ,
@@ -72,10 +87,10 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
72
87
) -> U where
73
88
F : FnOnce ( & str ) -> U ,
74
89
{
75
- let _0: T = Float :: zero ( ) ;
76
- let _1: T = Float :: one ( ) ;
90
+ let _0: T = T :: zero ( ) ;
91
+ let _1: T = T :: one ( ) ;
77
92
let radix: u32 = 10 ;
78
- let radix_f: T = cast ( radix) . unwrap ( ) ;
93
+ let radix_f = T :: from_u32 ( radix) ;
79
94
80
95
assert ! ( num. is_nan( ) || num >= _0, "float_to_str_bytes_common: number is negative" ) ;
81
96
@@ -99,7 +114,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
99
114
let ( num, exp) = match exp_format {
100
115
ExpDec if num != _0 => {
101
116
let exp = num. log10 ( ) . floor ( ) ;
102
- ( num / radix_f. powf ( exp) , cast :: < T , i32 > ( exp) . unwrap ( ) )
117
+ ( num / radix_f. powf ( exp) , exp. to_i32 ( ) )
103
118
}
104
119
_ => ( num, 0 )
105
120
} ;
@@ -114,7 +129,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
114
129
deccum = deccum / radix_f;
115
130
deccum = deccum. trunc ( ) ;
116
131
117
- let c = char:: from_digit ( current_digit. to_isize ( ) . unwrap ( ) as u32 , radix) ;
132
+ let c = char:: from_digit ( current_digit. to_i32 ( ) as u32 , radix) ;
118
133
buf[ end] = c. unwrap ( ) as u8 ;
119
134
end += 1 ;
120
135
@@ -158,7 +173,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
158
173
159
174
let current_digit = deccum. trunc ( ) ;
160
175
161
- let c = char:: from_digit ( current_digit. to_isize ( ) . unwrap ( ) as u32 , radix) ;
176
+ let c = char:: from_digit ( current_digit. to_i32 ( ) as u32 , radix) ;
162
177
buf[ end] = c. unwrap ( ) as u8 ;
163
178
end += 1 ;
164
179
0 commit comments