1111pub use self :: ExponentFormat :: * ;
1212pub use self :: SignificantDigits :: * ;
1313
14- use char:: { self , CharExt } ;
14+ use prelude:: * ;
15+
16+ use char;
1517use fmt;
16- use iter:: Iterator ;
17- use num:: { cast, Float , ToPrimitive } ;
18+ use num:: Float ;
1819use 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;
2323
2424/// A flag that specifies whether to use exponential (scientific) notation.
2525pub enum ExponentFormat {
@@ -42,6 +42,21 @@ pub enum SignificantDigits {
4242 DigExact ( usize )
4343}
4444
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+
4560/// Converts a float number to its string representation.
4661/// This is meant to be a common base implementation for various formatting styles.
4762/// The number is assumed to be non-negative, callers use `Formatter::pad_integral`
@@ -63,7 +78,7 @@ pub enum SignificantDigits {
6378/// # Panics
6479///
6580/// - 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 > (
6782 num : T ,
6883 digits : SignificantDigits ,
6984 exp_format : ExponentFormat ,
@@ -72,10 +87,10 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
7287) -> U where
7388 F : FnOnce ( & str ) -> U ,
7489{
75- let _0: T = Float :: zero ( ) ;
76- let _1: T = Float :: one ( ) ;
90+ let _0: T = T :: zero ( ) ;
91+ let _1: T = T :: one ( ) ;
7792 let radix: u32 = 10 ;
78- let radix_f: T = cast ( radix) . unwrap ( ) ;
93+ let radix_f = T :: from_u32 ( radix) ;
7994
8095 assert ! ( num. is_nan( ) || num >= _0, "float_to_str_bytes_common: number is negative" ) ;
8196
@@ -99,7 +114,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
99114 let ( num, exp) = match exp_format {
100115 ExpDec if num != _0 => {
101116 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 ( ) )
103118 }
104119 _ => ( num, 0 )
105120 } ;
@@ -114,7 +129,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
114129 deccum = deccum / radix_f;
115130 deccum = deccum. trunc ( ) ;
116131
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) ;
118133 buf[ end] = c. unwrap ( ) as u8 ;
119134 end += 1 ;
120135
@@ -158,7 +173,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
158173
159174 let current_digit = deccum. trunc ( ) ;
160175
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) ;
162177 buf[ end] = c. unwrap ( ) as u8 ;
163178 end += 1 ;
164179
0 commit comments