1 use super::{From, TryFrom};
2 use crate::num::TryFromIntError;
5 /// This trait being unreachable from outside the crate
6 /// prevents other implementations of the `FloatToInt` trait,
7 /// which allows potentially adding more trait methods after the trait is `#[stable]`.
8 #[unstable(feature = "convert_float_to_int", issue = "67057")]
12 /// Supporting trait for inherent methods of `f32` and `f64` such as `round_unchecked_to`.
13 /// Typically doesn’t need to be used directly.
14 #[unstable(feature = "convert_float_to_int", issue = "67057")]
15 pub trait FloatToInt<Int>: private::Sealed + Sized {
16 #[cfg(not(bootstrap))]
17 #[unstable(feature = "float_approx_unchecked_to", issue = "67058")]
19 unsafe fn approx_unchecked(self) -> Int;
22 macro_rules! impl_float_to_int {
23 ( $Float: ident => $( $Int: ident )+ ) => {
24 #[unstable(feature = "convert_float_to_int", issue = "67057")]
25 impl private::Sealed for $Float {}
27 #[unstable(feature = "convert_float_to_int", issue = "67057")]
28 impl FloatToInt<$Int> for $Float {
29 #[cfg(not(bootstrap))]
32 unsafe fn approx_unchecked(self) -> $Int {
33 crate::intrinsics::float_to_int_approx_unchecked(self)
40 impl_float_to_int!(f32 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
41 impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
43 // Conversion traits for primitive integer and float types
44 // Conversions T -> T are covered by a blanket impl and therefore excluded
45 // Some conversions from and to usize/isize are not implemented due to portability concerns
46 macro_rules! impl_from {
47 ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
50 impl From<$Small> for $Large {
52 fn from(small: $Small) -> $Large {
57 ($Small: ty, $Large: ty, #[$attr:meta]) => {
69 macro_rules! impl_from_bool {
70 ($target: ty, #[$attr:meta]) => {
71 impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
72 stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
78 assert_eq!(", stringify!($target), "::from(true), 1);
79 assert_eq!(", stringify!($target), "::from(false), 0);
85 impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
86 impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
87 impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
88 impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
89 impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
90 impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
91 impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
92 impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
93 impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
94 impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
95 impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
96 impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
98 // Unsigned -> Unsigned
99 impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
100 impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
101 impl_from! { u8, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
102 impl_from! { u8, u128, #[stable(feature = "i128", since = "1.26.0")] }
103 impl_from! { u8, usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
104 impl_from! { u16, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
105 impl_from! { u16, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
106 impl_from! { u16, u128, #[stable(feature = "i128", since = "1.26.0")] }
107 impl_from! { u32, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
108 impl_from! { u32, u128, #[stable(feature = "i128", since = "1.26.0")] }
109 impl_from! { u64, u128, #[stable(feature = "i128", since = "1.26.0")] }
112 impl_from! { i8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
113 impl_from! { i8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
114 impl_from! { i8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
115 impl_from! { i8, i128, #[stable(feature = "i128", since = "1.26.0")] }
116 impl_from! { i8, isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
117 impl_from! { i16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
118 impl_from! { i16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
119 impl_from! { i16, i128, #[stable(feature = "i128", since = "1.26.0")] }
120 impl_from! { i32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
121 impl_from! { i32, i128, #[stable(feature = "i128", since = "1.26.0")] }
122 impl_from! { i64, i128, #[stable(feature = "i128", since = "1.26.0")] }
124 // Unsigned -> Signed
125 impl_from! { u8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
126 impl_from! { u8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
127 impl_from! { u8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
128 impl_from! { u8, i128, #[stable(feature = "i128", since = "1.26.0")] }
129 impl_from! { u16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
130 impl_from! { u16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
131 impl_from! { u16, i128, #[stable(feature = "i128", since = "1.26.0")] }
132 impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
133 impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
134 impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
136 // The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
137 // which imply that pointer-sized integers must be at least 16 bits:
138 // https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
139 impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
140 impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
141 impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
143 // RISC-V defines the possibility of a 128-bit address space (RV128).
145 // CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
146 // https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
147 // http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf
149 // Note: integers can only be represented with full precision in a float if
150 // they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
151 // Lossy float conversions are not implemented at this time.
154 impl_from! { i8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
155 impl_from! { i8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
156 impl_from! { i16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
157 impl_from! { i16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
158 impl_from! { i32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
161 impl_from! { u8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
162 impl_from! { u8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
163 impl_from! { u16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
164 impl_from! { u16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
165 impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
168 impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
170 // no possible bounds violation
171 macro_rules! try_from_unbounded {
172 ($source:ty, $($target:ty),*) => {$(
173 #[stable(feature = "try_from", since = "1.34.0")]
174 impl TryFrom<$source> for $target {
175 type Error = TryFromIntError;
177 /// Try to create the target number type from a source
178 /// number type. This returns an error if the source value
179 /// is outside of the range of the target type.
181 fn try_from(value: $source) -> Result<Self, Self::Error> {
188 // only negative bounds
189 macro_rules! try_from_lower_bounded {
190 ($source:ty, $($target:ty),*) => {$(
191 #[stable(feature = "try_from", since = "1.34.0")]
192 impl TryFrom<$source> for $target {
193 type Error = TryFromIntError;
195 /// Try to create the target number type from a source
196 /// number type. This returns an error if the source value
197 /// is outside of the range of the target type.
199 fn try_from(u: $source) -> Result<$target, TryFromIntError> {
203 Err(TryFromIntError(()))
210 // unsigned to signed (only positive bound)
211 macro_rules! try_from_upper_bounded {
212 ($source:ty, $($target:ty),*) => {$(
213 #[stable(feature = "try_from", since = "1.34.0")]
214 impl TryFrom<$source> for $target {
215 type Error = TryFromIntError;
217 /// Try to create the target number type from a source
218 /// number type. This returns an error if the source value
219 /// is outside of the range of the target type.
221 fn try_from(u: $source) -> Result<$target, TryFromIntError> {
222 if u > (<$target>::max_value() as $source) {
223 Err(TryFromIntError(()))
233 macro_rules! try_from_both_bounded {
234 ($source:ty, $($target:ty),*) => {$(
235 #[stable(feature = "try_from", since = "1.34.0")]
236 impl TryFrom<$source> for $target {
237 type Error = TryFromIntError;
239 /// Try to create the target number type from a source
240 /// number type. This returns an error if the source value
241 /// is outside of the range of the target type.
243 fn try_from(u: $source) -> Result<$target, TryFromIntError> {
244 let min = <$target>::min_value() as $source;
245 let max = <$target>::max_value() as $source;
246 if u < min || u > max {
247 Err(TryFromIntError(()))
257 ($mac:ident, $source:ty, $($target:ty),*) => {$(
258 $mac!($target, $source);
262 // intra-sign conversions
263 try_from_upper_bounded!(u16, u8);
264 try_from_upper_bounded!(u32, u16, u8);
265 try_from_upper_bounded!(u64, u32, u16, u8);
266 try_from_upper_bounded!(u128, u64, u32, u16, u8);
268 try_from_both_bounded!(i16, i8);
269 try_from_both_bounded!(i32, i16, i8);
270 try_from_both_bounded!(i64, i32, i16, i8);
271 try_from_both_bounded!(i128, i64, i32, i16, i8);
273 // unsigned-to-signed
274 try_from_upper_bounded!(u8, i8);
275 try_from_upper_bounded!(u16, i8, i16);
276 try_from_upper_bounded!(u32, i8, i16, i32);
277 try_from_upper_bounded!(u64, i8, i16, i32, i64);
278 try_from_upper_bounded!(u128, i8, i16, i32, i64, i128);
280 // signed-to-unsigned
281 try_from_lower_bounded!(i8, u8, u16, u32, u64, u128);
282 try_from_lower_bounded!(i16, u16, u32, u64, u128);
283 try_from_lower_bounded!(i32, u32, u64, u128);
284 try_from_lower_bounded!(i64, u64, u128);
285 try_from_lower_bounded!(i128, u128);
286 try_from_both_bounded!(i16, u8);
287 try_from_both_bounded!(i32, u16, u8);
288 try_from_both_bounded!(i64, u32, u16, u8);
289 try_from_both_bounded!(i128, u64, u32, u16, u8);
292 try_from_upper_bounded!(usize, isize);
293 try_from_lower_bounded!(isize, usize);
295 #[cfg(target_pointer_width = "16")]
296 mod ptr_try_from_impls {
297 use super::TryFromIntError;
298 use crate::convert::TryFrom;
300 try_from_upper_bounded!(usize, u8);
301 try_from_unbounded!(usize, u16, u32, u64, u128);
302 try_from_upper_bounded!(usize, i8, i16);
303 try_from_unbounded!(usize, i32, i64, i128);
305 try_from_both_bounded!(isize, u8);
306 try_from_lower_bounded!(isize, u16, u32, u64, u128);
307 try_from_both_bounded!(isize, i8);
308 try_from_unbounded!(isize, i16, i32, i64, i128);
310 rev!(try_from_upper_bounded, usize, u32, u64, u128);
311 rev!(try_from_lower_bounded, usize, i8, i16);
312 rev!(try_from_both_bounded, usize, i32, i64, i128);
314 rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
315 rev!(try_from_both_bounded, isize, i32, i64, i128);
318 #[cfg(target_pointer_width = "32")]
319 mod ptr_try_from_impls {
320 use super::TryFromIntError;
321 use crate::convert::TryFrom;
323 try_from_upper_bounded!(usize, u8, u16);
324 try_from_unbounded!(usize, u32, u64, u128);
325 try_from_upper_bounded!(usize, i8, i16, i32);
326 try_from_unbounded!(usize, i64, i128);
328 try_from_both_bounded!(isize, u8, u16);
329 try_from_lower_bounded!(isize, u32, u64, u128);
330 try_from_both_bounded!(isize, i8, i16);
331 try_from_unbounded!(isize, i32, i64, i128);
333 rev!(try_from_unbounded, usize, u32);
334 rev!(try_from_upper_bounded, usize, u64, u128);
335 rev!(try_from_lower_bounded, usize, i8, i16, i32);
336 rev!(try_from_both_bounded, usize, i64, i128);
338 rev!(try_from_unbounded, isize, u16);
339 rev!(try_from_upper_bounded, isize, u32, u64, u128);
340 rev!(try_from_unbounded, isize, i32);
341 rev!(try_from_both_bounded, isize, i64, i128);
344 #[cfg(target_pointer_width = "64")]
345 mod ptr_try_from_impls {
346 use super::TryFromIntError;
347 use crate::convert::TryFrom;
349 try_from_upper_bounded!(usize, u8, u16, u32);
350 try_from_unbounded!(usize, u64, u128);
351 try_from_upper_bounded!(usize, i8, i16, i32, i64);
352 try_from_unbounded!(usize, i128);
354 try_from_both_bounded!(isize, u8, u16, u32);
355 try_from_lower_bounded!(isize, u64, u128);
356 try_from_both_bounded!(isize, i8, i16, i32);
357 try_from_unbounded!(isize, i64, i128);
359 rev!(try_from_unbounded, usize, u32, u64);
360 rev!(try_from_upper_bounded, usize, u128);
361 rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
362 rev!(try_from_both_bounded, usize, i128);
364 rev!(try_from_unbounded, isize, u16, u32);
365 rev!(try_from_upper_bounded, isize, u64, u128);
366 rev!(try_from_unbounded, isize, i32, i64);
367 rev!(try_from_both_bounded, isize, i128);