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 #[unstable(feature = "float_approx_unchecked_to", issue = "67058")]
18 unsafe fn approx_unchecked(self) -> Int;
21 macro_rules! impl_float_to_int {
22 ( $Float: ident => $( $Int: ident )+ ) => {
23 #[unstable(feature = "convert_float_to_int", issue = "67057")]
24 impl private::Sealed for $Float {}
26 #[unstable(feature = "convert_float_to_int", issue = "67057")]
27 impl FloatToInt<$Int> for $Float {
30 unsafe fn approx_unchecked(self) -> $Int {
31 crate::intrinsics::float_to_int_approx_unchecked(self)
38 impl_float_to_int!(f32 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
39 impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
41 // Conversion traits for primitive integer and float types
42 // Conversions T -> T are covered by a blanket impl and therefore excluded
43 // Some conversions from and to usize/isize are not implemented due to portability concerns
44 macro_rules! impl_from {
45 ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
48 impl From<$Small> for $Large {
50 fn from(small: $Small) -> Self {
55 ($Small: ty, $Large: ty, #[$attr:meta]) => {
67 macro_rules! impl_from_bool {
68 ($target: ty, #[$attr:meta]) => {
69 impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
70 stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
76 assert_eq!(", stringify!($target), "::from(true), 1);
77 assert_eq!(", stringify!($target), "::from(false), 0);
83 impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
84 impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
85 impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
86 impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
87 impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
88 impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
89 impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
90 impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
91 impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
92 impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
93 impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
94 impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
96 // Unsigned -> Unsigned
97 impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
98 impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
99 impl_from! { u8, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
100 impl_from! { u8, u128, #[stable(feature = "i128", since = "1.26.0")] }
101 impl_from! { u8, usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
102 impl_from! { u16, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
103 impl_from! { u16, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
104 impl_from! { u16, u128, #[stable(feature = "i128", since = "1.26.0")] }
105 impl_from! { u32, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
106 impl_from! { u32, u128, #[stable(feature = "i128", since = "1.26.0")] }
107 impl_from! { u64, u128, #[stable(feature = "i128", since = "1.26.0")] }
110 impl_from! { i8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
111 impl_from! { i8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
112 impl_from! { i8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
113 impl_from! { i8, i128, #[stable(feature = "i128", since = "1.26.0")] }
114 impl_from! { i8, isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
115 impl_from! { i16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
116 impl_from! { i16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
117 impl_from! { i16, i128, #[stable(feature = "i128", since = "1.26.0")] }
118 impl_from! { i32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
119 impl_from! { i32, i128, #[stable(feature = "i128", since = "1.26.0")] }
120 impl_from! { i64, i128, #[stable(feature = "i128", since = "1.26.0")] }
122 // Unsigned -> Signed
123 impl_from! { u8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
124 impl_from! { u8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
125 impl_from! { u8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
126 impl_from! { u8, i128, #[stable(feature = "i128", since = "1.26.0")] }
127 impl_from! { u16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
128 impl_from! { u16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
129 impl_from! { u16, i128, #[stable(feature = "i128", since = "1.26.0")] }
130 impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
131 impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
132 impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
134 // The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
135 // which imply that pointer-sized integers must be at least 16 bits:
136 // https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
137 impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
138 impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
139 impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
141 // RISC-V defines the possibility of a 128-bit address space (RV128).
143 // CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
144 // https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
145 // http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf
147 // Note: integers can only be represented with full precision in a float if
148 // they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
149 // Lossy float conversions are not implemented at this time.
152 impl_from! { i8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
153 impl_from! { i8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
154 impl_from! { i16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
155 impl_from! { i16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
156 impl_from! { i32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
159 impl_from! { u8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
160 impl_from! { u8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
161 impl_from! { u16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
162 impl_from! { u16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
163 impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
166 impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
168 // no possible bounds violation
169 macro_rules! try_from_unbounded {
170 ($source:ty, $($target:ty),*) => {$(
171 #[stable(feature = "try_from", since = "1.34.0")]
172 impl TryFrom<$source> for $target {
173 type Error = TryFromIntError;
175 /// Try to create the target number type from a source
176 /// number type. This returns an error if the source value
177 /// is outside of the range of the target type.
179 fn try_from(value: $source) -> Result<Self, Self::Error> {
186 // only negative bounds
187 macro_rules! try_from_lower_bounded {
188 ($source:ty, $($target:ty),*) => {$(
189 #[stable(feature = "try_from", since = "1.34.0")]
190 impl TryFrom<$source> for $target {
191 type Error = TryFromIntError;
193 /// Try to create the target number type from a source
194 /// number type. This returns an error if the source value
195 /// is outside of the range of the target type.
197 fn try_from(u: $source) -> Result<Self, Self::Error> {
201 Err(TryFromIntError(()))
208 // unsigned to signed (only positive bound)
209 macro_rules! try_from_upper_bounded {
210 ($source:ty, $($target:ty),*) => {$(
211 #[stable(feature = "try_from", since = "1.34.0")]
212 impl TryFrom<$source> for $target {
213 type Error = TryFromIntError;
215 /// Try to create the target number type from a source
216 /// number type. This returns an error if the source value
217 /// is outside of the range of the target type.
219 fn try_from(u: $source) -> Result<Self, Self::Error> {
220 if u > (Self::max_value() as $source) {
221 Err(TryFromIntError(()))
231 macro_rules! try_from_both_bounded {
232 ($source:ty, $($target:ty),*) => {$(
233 #[stable(feature = "try_from", since = "1.34.0")]
234 impl TryFrom<$source> for $target {
235 type Error = TryFromIntError;
237 /// Try to create the target number type from a source
238 /// number type. This returns an error if the source value
239 /// is outside of the range of the target type.
241 fn try_from(u: $source) -> Result<Self, Self::Error> {
242 let min = Self::min_value() as $source;
243 let max = Self::max_value() as $source;
244 if u < min || u > max {
245 Err(TryFromIntError(()))
255 ($mac:ident, $source:ty, $($target:ty),*) => {$(
256 $mac!($target, $source);
260 // intra-sign conversions
261 try_from_upper_bounded!(u16, u8);
262 try_from_upper_bounded!(u32, u16, u8);
263 try_from_upper_bounded!(u64, u32, u16, u8);
264 try_from_upper_bounded!(u128, u64, u32, u16, u8);
266 try_from_both_bounded!(i16, i8);
267 try_from_both_bounded!(i32, i16, i8);
268 try_from_both_bounded!(i64, i32, i16, i8);
269 try_from_both_bounded!(i128, i64, i32, i16, i8);
271 // unsigned-to-signed
272 try_from_upper_bounded!(u8, i8);
273 try_from_upper_bounded!(u16, i8, i16);
274 try_from_upper_bounded!(u32, i8, i16, i32);
275 try_from_upper_bounded!(u64, i8, i16, i32, i64);
276 try_from_upper_bounded!(u128, i8, i16, i32, i64, i128);
278 // signed-to-unsigned
279 try_from_lower_bounded!(i8, u8, u16, u32, u64, u128);
280 try_from_lower_bounded!(i16, u16, u32, u64, u128);
281 try_from_lower_bounded!(i32, u32, u64, u128);
282 try_from_lower_bounded!(i64, u64, u128);
283 try_from_lower_bounded!(i128, u128);
284 try_from_both_bounded!(i16, u8);
285 try_from_both_bounded!(i32, u16, u8);
286 try_from_both_bounded!(i64, u32, u16, u8);
287 try_from_both_bounded!(i128, u64, u32, u16, u8);
290 try_from_upper_bounded!(usize, isize);
291 try_from_lower_bounded!(isize, usize);
293 #[cfg(target_pointer_width = "16")]
294 mod ptr_try_from_impls {
295 use super::TryFromIntError;
296 use crate::convert::TryFrom;
298 try_from_upper_bounded!(usize, u8);
299 try_from_unbounded!(usize, u16, u32, u64, u128);
300 try_from_upper_bounded!(usize, i8, i16);
301 try_from_unbounded!(usize, i32, i64, i128);
303 try_from_both_bounded!(isize, u8);
304 try_from_lower_bounded!(isize, u16, u32, u64, u128);
305 try_from_both_bounded!(isize, i8);
306 try_from_unbounded!(isize, i16, i32, i64, i128);
308 rev!(try_from_upper_bounded, usize, u32, u64, u128);
309 rev!(try_from_lower_bounded, usize, i8, i16);
310 rev!(try_from_both_bounded, usize, i32, i64, i128);
312 rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
313 rev!(try_from_both_bounded, isize, i32, i64, i128);
316 #[cfg(target_pointer_width = "32")]
317 mod ptr_try_from_impls {
318 use super::TryFromIntError;
319 use crate::convert::TryFrom;
321 try_from_upper_bounded!(usize, u8, u16);
322 try_from_unbounded!(usize, u32, u64, u128);
323 try_from_upper_bounded!(usize, i8, i16, i32);
324 try_from_unbounded!(usize, i64, i128);
326 try_from_both_bounded!(isize, u8, u16);
327 try_from_lower_bounded!(isize, u32, u64, u128);
328 try_from_both_bounded!(isize, i8, i16);
329 try_from_unbounded!(isize, i32, i64, i128);
331 rev!(try_from_unbounded, usize, u32);
332 rev!(try_from_upper_bounded, usize, u64, u128);
333 rev!(try_from_lower_bounded, usize, i8, i16, i32);
334 rev!(try_from_both_bounded, usize, i64, i128);
336 rev!(try_from_unbounded, isize, u16);
337 rev!(try_from_upper_bounded, isize, u32, u64, u128);
338 rev!(try_from_unbounded, isize, i32);
339 rev!(try_from_both_bounded, isize, i64, i128);
342 #[cfg(target_pointer_width = "64")]
343 mod ptr_try_from_impls {
344 use super::TryFromIntError;
345 use crate::convert::TryFrom;
347 try_from_upper_bounded!(usize, u8, u16, u32);
348 try_from_unbounded!(usize, u64, u128);
349 try_from_upper_bounded!(usize, i8, i16, i32, i64);
350 try_from_unbounded!(usize, i128);
352 try_from_both_bounded!(isize, u8, u16, u32);
353 try_from_lower_bounded!(isize, u64, u128);
354 try_from_both_bounded!(isize, i8, i16, i32);
355 try_from_unbounded!(isize, i64, i128);
357 rev!(try_from_unbounded, usize, u32, u64);
358 rev!(try_from_upper_bounded, usize, u128);
359 rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
360 rev!(try_from_both_bounded, usize, i128);
362 rev!(try_from_unbounded, isize, u16, u32);
363 rev!(try_from_upper_bounded, isize, u64, u128);
364 rev!(try_from_unbounded, isize, i32, i64);
365 rev!(try_from_both_bounded, isize, i128);
368 // Conversion traits for non-zero integer types
369 use crate::num::NonZeroI128;
370 use crate::num::NonZeroI16;
371 use crate::num::NonZeroI32;
372 use crate::num::NonZeroI64;
373 use crate::num::NonZeroI8;
374 use crate::num::NonZeroIsize;
375 use crate::num::NonZeroU128;
376 use crate::num::NonZeroU16;
377 use crate::num::NonZeroU32;
378 use crate::num::NonZeroU64;
379 use crate::num::NonZeroU8;
380 use crate::num::NonZeroUsize;
382 macro_rules! nzint_impl_from {
383 ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
386 impl From<$Small> for $Large {
388 fn from(small: $Small) -> Self {
389 // SAFETY: input type guarantees the value is non-zero
391 Self::new_unchecked(small.get().into())
396 ($Small: ty, $Large: ty, #[$attr:meta]) => {
397 nzint_impl_from!($Small,
400 concat!("Converts `",
408 // Non-zero Unsigned -> Non-zero Unsigned
409 nzint_impl_from! { NonZeroU8, NonZeroU16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
410 nzint_impl_from! { NonZeroU8, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
411 nzint_impl_from! { NonZeroU8, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
412 nzint_impl_from! { NonZeroU8, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
413 nzint_impl_from! { NonZeroU8, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
414 nzint_impl_from! { NonZeroU16, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
415 nzint_impl_from! { NonZeroU16, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
416 nzint_impl_from! { NonZeroU16, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
417 nzint_impl_from! { NonZeroU16, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
418 nzint_impl_from! { NonZeroU32, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
419 nzint_impl_from! { NonZeroU32, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
420 nzint_impl_from! { NonZeroU64, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
422 // Non-zero Signed -> Non-zero Signed
423 nzint_impl_from! { NonZeroI8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
424 nzint_impl_from! { NonZeroI8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
425 nzint_impl_from! { NonZeroI8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
426 nzint_impl_from! { NonZeroI8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
427 nzint_impl_from! { NonZeroI8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
428 nzint_impl_from! { NonZeroI16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
429 nzint_impl_from! { NonZeroI16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
430 nzint_impl_from! { NonZeroI16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
431 nzint_impl_from! { NonZeroI16, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
432 nzint_impl_from! { NonZeroI32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
433 nzint_impl_from! { NonZeroI32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
434 nzint_impl_from! { NonZeroI64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
436 // NonZero UnSigned -> Non-zero Signed
437 nzint_impl_from! { NonZeroU8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
438 nzint_impl_from! { NonZeroU8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
439 nzint_impl_from! { NonZeroU8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
440 nzint_impl_from! { NonZeroU8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
441 nzint_impl_from! { NonZeroU8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
442 nzint_impl_from! { NonZeroU16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
443 nzint_impl_from! { NonZeroU16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
444 nzint_impl_from! { NonZeroU16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
445 nzint_impl_from! { NonZeroU32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
446 nzint_impl_from! { NonZeroU32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
447 nzint_impl_from! { NonZeroU64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }