Text of rust.docs.kernel.org › next › src › core › convert › num.rs.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

num.rs - source

Skip to main content core/convert/ num.rs 1 use crate ::num::{ IntErrorKind , TryFromIntError }; 2 3 /// Supporting trait for inherent methods of `f32` and `f64` such as `to_int_unchecked`. 4 /// Typically doesn’t need to be used directly. 5 #[unstable(feature = "convert_float_to_int" , issue = "67057" )] 6 pub impl ( self ) trait FloatToInt <Int>: Sized { 7 #[unstable(feature = "convert_float_to_int" , issue = "67057" )] 8 #[doc(hidden)] 9 unsafe fn to_int_unchecked( self ) -> Int; 10 11 #[unstable(feature = "float_conversions" , issue = "159913" )] 12 #[doc(hidden)] 13 fn to_int_saturating( self ) -> Int; 14 15 #[unstable(feature = "float_conversions" , issue = "159913" )] 16 #[doc(hidden)] 17 fn to_int_checked( self ) -> Option <Int>; 18 } 19 20 macro_rules! impl_float_to_int { 21 ( $Float :ty => $( $Int :ty),+) => { 22 $( 23 #[unstable(feature = "convert_float_to_int" , issue = "67057" )] 24 impl FloatToInt< $Int > for $Float { 25 #[inline] 26 unsafe fn to_int_unchecked( self ) -> $Int { 27 // SAFETY: the safety contract must be upheld by the caller.

28 unsafe { crate ::intrinsics::float_to_int_unchecked( self ) } 29 } 30 #[inline] 31 fn to_int_saturating( self ) -> $Int { 32 // `as` already saturates and maps `NaN` to zero. 33 self as $Int 34 } 35 #[inline] 36 fn to_int_checked( self ) -> Option < $Int > { 37 // We will compute LOW_THRESHOLD and HIGH_THRESHOLD 38 // as <$Int>::MIN - 1 and <$Int>::MAX + 1 respectively, 39 // or the next lower/higher value in case those are not 40 // representable exactly.

These thresholds represent the 41 // first disallowed values. 42 43 const LOW_THRESHOLD: $Float = { 44 // The minimum of an integer type is representable or -INF 45 // for all floats, as it is zero or a power of two. 46 let int_min = < $Int >::MIN as $Float ; 47 let one_below_if_representable = int_min - 1.0 ; 48 if one_below_if_representable == int_min { 49 // We must allow int_min itself. In case int_min is 50 // -INF this stays -INF, allowing any finite value.

51 int_min.next_down() 52 } else { 53 one_below_if_representable 54 } 55 }; 56 57 const HIGH_THRESHOLD: $Float = { 58 // The maximum of an integer type is always of the form 59 // 2^k - 1 for some reasonable k. We can construct 2^k 60 // exactly by summing 2^(k-1) twice, which fits in the 61 // integer. The outcome will be exactly <$Int>::MAX + 1, 62 // or INF allowing any finite value. 63 let half = (< $Int >::MAX / 2 ) + 1 ; 64 half as $Float + half as $Float 65 }; 66 67 // NaN always fails these comparisons, and infinities at 68 // least one.

69 if LOW_THRESHOLD < self && self < HIGH_THRESHOLD { 70 // SAFETY: we made sure we are in-bounds and finite. 71 Some ( unsafe { self .to_int_unchecked() }) 72 } else { 73 None 74 } 75 } 76 } 77 )+ 78 } 79 } 80 81 impl_float_to_int! ( f16 => u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize ); 82 impl_float_to_int! ( f32 => u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize ); 83 impl_float_to_int! ( f64 => u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize ); 84 impl_float_to_int! ( f128 => u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize ); 85 86 /// Supporting trait for the inherent `cast` method converting between float types.

87 /// Typically doesn’t need to be used directly. 88 #[unstable(feature = "float_conversions" , issue = "159913" )] 89 pub impl ( self ) trait FloatToFloat <Flt>: Sized { 90 #[unstable(feature = "float_conversions" , issue = "159913" )] 91 #[doc(hidden)] 92 fn cast( self ) -> Flt; 93 } 94 95 macro_rules! impl_float_to_float { 96 ( $Float :ty => $( $Flt :ty),+) => { 97 $( 98 #[unstable(feature = "float_conversions" , issue = "159913" )] 99 impl FloatToFloat< $Flt > for $Float { 100 #[inline] 101 fn cast( self ) -> $Flt { 102 self as $Flt 103 } 104 } 105 )+ 106 } 107 } 108 109 impl_float_to_float! ( f16 => f16 , f32 , f64 , f128 ); 110 impl_float_to_float! ( f32 => f16 , f32 , f64 , f128 ); 111 impl_float_to_float! ( f64 => f16 , f32 , f64 , f128 ); 112 impl_float_to_float! ( f128 => f16 , f32 , f64 , f128 ); 113 114 /// Implement `From<bool>` for integers 115 macro_rules! impl_from_bool { 116 ($( $int :ty) * ) => {$( 117 #[stable(feature = "from_bool" , since = "1.28.0" )] 118 #[rustc_const_unstable(feature = "const_convert" , issue = "143773" )] 119 const impl From<bool> for $int { 120 /// Converts from [`bool`] to 121 #[doc = concat! ( "[`" , stringify! ( $int ), "`]" )] 122 /// , by turning `false` into `0` and `true` into `1`.

123 /// 124 /// # Examples 125 /// 126 /// ``` 127 #[doc = concat! ( "assert_eq!(" , stringify! ( $int ), "::from(false), 0);" )] 128 /// 129 #[doc = concat! ( "assert_eq!(" , stringify! ( $int ), "::from(true), 1);" )] 130 /// ``` 131 #[inline(always)] 132 fn from(b: bool ) -> Self { 133 b as Self 134 } 135 } 136 ) * } 137 } 138 139 // boolean -> integer 140 impl_from_bool! ( u8 u16 u32 u64 u128 usize ); 141 impl_from_bool! ( i8 i16 i32 i64 i128 isize ); 142 143 /// Implement `From<$small>` for `$large` 144 macro_rules! impl_from { 145 ( $small :ty => $large :ty, $( #[ $attrs :meta] ),+) => { 146 $( #[ $attrs ] )+ 147 #[rustc_const_unstable(feature = "const_convert" , issue = "143773" )] 148 const impl From< $small > for $large { 149 #[doc = concat! ( "Converts from [`" , stringify! ( $small ), "`] to [`" , stringify! ( $large ), "`] losslessly." )] 150 #[inline(always)] 151 fn from(small: $small ) -> Self { 152 debug_assert! (< $large >::MIN as i128 <= < $small >::MIN as i128); 153 debug_assert! (< $small >::MAX as u128 <= < $large >::MAX as u128); 154 small as Self 155 } 156 } 157 } 158 } 159 160 // unsigned integer -> unsigned integer 161 impl_from! ( u8 => u16 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 162 impl_from! ( u8 => u32 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 163 impl_from! ( u8 => u64 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 164 impl_from! ( u8 => u128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 165 impl_from! ( u8 => usize , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 166 impl_from! ( u16 => u32 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 167 impl_from! ( u16 => u64 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 168 impl_from! ( u16 => u128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 169 impl_from! ( u32 => u64 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 170 impl_from! ( u32 => u128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 171 impl_from! ( u64 => u128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 172 173 // signed integer -> signed integer 174 impl_from! ( i8 => i16 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 175 impl_from! ( i8 => i32 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 176 impl_from! ( i8 => i64 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 177 impl_from! ( i8 => i128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 178 impl_from! ( i8 => isize , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 179 impl_from! ( i16 => i32 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 180 impl_from! ( i16 => i64 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 181 impl_from! ( i16 => i128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 182 impl_from! ( i32 => i64 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 183 impl_from! ( i32 => i128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 184 impl_from! ( i64 => i128 , #[stable(feature = "i128" , since = "1.26.0" )] ); 185 186 // unsigned integer -> signed integer 187 impl_from! ( u8 => i16 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 188 impl_from! ( u8 => i32 , #[stable(feature = "lossless_int_conv" , since = "1.5.0" )] ); 189 impl_from! ( u8 => i64 , #[stable(feature = "lossless_int_conv" , sin

The rest of this page is on the live site.