@@ -969,9 +969,8 @@ pub enum FpCategory {
969969}
970970
971971#[ doc( hidden) ]
972- trait FromStrRadixHelper : PartialOrd + Copy {
972+ trait FromStrRadixHelper : PartialOrd + Copy + Default {
973973 const MIN : Self ;
974- fn from_u32 ( u : u32 ) -> Self ;
975974 fn checked_mul ( & self , other : u32 ) -> Option < Self > ;
976975 fn checked_sub ( & self , other : u32 ) -> Option < Self > ;
977976 fn checked_add ( & self , other : u32 ) -> Option < Self > ;
@@ -997,8 +996,6 @@ macro_rules! impl_helper_for {
997996 ( $( $t: ty) * ) => ( $( impl FromStrRadixHelper for $t {
998997 const MIN : Self = Self :: MIN ;
999998 #[ inline]
1000- fn from_u32( u: u32 ) -> Self { u as Self }
1001- #[ inline]
1002999 fn checked_mul( & self , other: u32 ) -> Option <Self > {
10031000 Self :: checked_mul( * self , other as Self )
10041001 }
@@ -1035,8 +1032,14 @@ macro_rules! impl_helper_for {
10351032}
10361033impl_helper_for ! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
10371034
1035+ /// Determins if a string of text of that length of that radix could be guaranteed to be
1036+ /// stored in the given type T.
1037+ /// Note that if the radix is known to the compiler, it is just the check of digits.len that
1038+ /// is done at runtime.
1039+ #[ doc( hidden) ]
10381040#[ inline( always) ]
1039- pub ( crate ) fn can_not_overflow < T > ( radix : u32 , is_signed_ty : bool , digits : & [ u8 ] ) -> bool {
1041+ #[ unstable( issue = "none" , feature = "std_internals" ) ]
1042+ pub fn can_not_overflow < T > ( radix : u32 , is_signed_ty : bool , digits : & [ u8 ] ) -> bool {
10401043 radix <= 16 && digits. len ( ) <= mem:: size_of :: < T > ( ) * 2 - is_signed_ty as usize
10411044}
10421045
@@ -1054,7 +1057,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
10541057 return Err ( PIE { kind : Empty } ) ;
10551058 }
10561059
1057- let is_signed_ty = T :: from_u32 ( 0 ) > T :: MIN ;
1060+ let is_signed_ty = T :: default ( ) > T :: MIN ;
10581061
10591062 // all valid digits are ascii, so we will just iterate over the utf8 bytes
10601063 // and cast them to chars. .to_digit() will safely return None for anything
@@ -1071,7 +1074,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
10711074 _ => ( true , src) ,
10721075 } ;
10731076
1074- let mut result = T :: from_u32 ( 0 ) ;
1077+ let mut result = T :: default ( ) ;
10751078
10761079 if can_not_overflow :: < T > ( radix, is_signed_ty, digits) {
10771080 // SAFETY: If the len of the str is short compared to the range of the type
@@ -1127,11 +1130,3 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
11271130 }
11281131 Ok ( result)
11291132}
1130-
1131- mod tests {
1132- #[ test]
1133- fn test_can_not_overflow ( ) {
1134- assert_eq ! ( can_not_overflow:: <i8 >( 10 , true , "99" . as_bytes( ) ) , true ) ;
1135- assert_eq ! ( can_not_overflow:: <i8 >( 10 , true , "129" . as_bytes( ) ) , false ) ;
1136- }
1137- }
0 commit comments