@@ -969,9 +969,8 @@ pub enum FpCategory {
969
969
}
970
970
971
971
#[ doc( hidden) ]
972
- trait FromStrRadixHelper : PartialOrd + Copy {
972
+ trait FromStrRadixHelper : PartialOrd + Copy + Default {
973
973
const MIN : Self ;
974
- fn from_u32 ( u : u32 ) -> Self ;
975
974
fn checked_mul ( & self , other : u32 ) -> Option < Self > ;
976
975
fn checked_sub ( & self , other : u32 ) -> Option < Self > ;
977
976
fn checked_add ( & self , other : u32 ) -> Option < Self > ;
@@ -997,8 +996,6 @@ macro_rules! impl_helper_for {
997
996
( $( $t: ty) * ) => ( $( impl FromStrRadixHelper for $t {
998
997
const MIN : Self = Self :: MIN ;
999
998
#[ inline]
1000
- fn from_u32( u: u32 ) -> Self { u as Self }
1001
- #[ inline]
1002
999
fn checked_mul( & self , other: u32 ) -> Option <Self > {
1003
1000
Self :: checked_mul( * self , other as Self )
1004
1001
}
@@ -1035,8 +1032,14 @@ macro_rules! impl_helper_for {
1035
1032
}
1036
1033
impl_helper_for ! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
1037
1034
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) ]
1038
1040
#[ 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 {
1040
1043
radix <= 16 && digits. len ( ) <= mem:: size_of :: < T > ( ) * 2 - is_signed_ty as usize
1041
1044
}
1042
1045
@@ -1054,7 +1057,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
1054
1057
return Err ( PIE { kind : Empty } ) ;
1055
1058
}
1056
1059
1057
- let is_signed_ty = T :: from_u32 ( 0 ) > T :: MIN ;
1060
+ let is_signed_ty = T :: default ( ) > T :: MIN ;
1058
1061
1059
1062
// all valid digits are ascii, so we will just iterate over the utf8 bytes
1060
1063
// 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
1071
1074
_ => ( true , src) ,
1072
1075
} ;
1073
1076
1074
- let mut result = T :: from_u32 ( 0 ) ;
1077
+ let mut result = T :: default ( ) ;
1075
1078
1076
1079
if can_not_overflow :: < T > ( radix, is_signed_ty, digits) {
1077
1080
// 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
1127
1130
}
1128
1131
Ok ( result)
1129
1132
}
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