Skip to main content

core/convert/
num.rs

1use crate::num::{IntErrorKind, TryFromIntError};
2
3mod private {
4    /// This trait being unreachable from outside the crate
5    /// prevents other implementations of the `FloatToInt` trait,
6    /// which allows potentially adding more trait methods after the trait is `#[stable]`.
7    #[unstable(feature = "convert_float_to_int", issue = "67057")]
8    pub trait Sealed {}
9}
10
11/// Supporting trait for inherent methods of `f32` and `f64` such as `to_int_unchecked`.
12/// Typically doesn’t need to be used directly.
13#[unstable(feature = "convert_float_to_int", issue = "67057")]
14pub trait FloatToInt<Int>: private::Sealed + Sized {
15    #[unstable(feature = "convert_float_to_int", issue = "67057")]
16    #[doc(hidden)]
17    unsafe fn to_int_unchecked(self) -> Int;
18}
19
20macro_rules! impl_float_to_int {
21    ($Float:ty => $($Int:ty),+) => {
22        #[unstable(feature = "convert_float_to_int", issue = "67057")]
23        impl private::Sealed for $Float {}
24        $(
25            #[unstable(feature = "convert_float_to_int", issue = "67057")]
26            impl FloatToInt<$Int> for $Float {
27                #[inline]
28                unsafe fn to_int_unchecked(self) -> $Int {
29                    // SAFETY: the safety contract must be upheld by the caller.
30                    unsafe { crate::intrinsics::float_to_int_unchecked(self) }
31                }
32            }
33        )+
34    }
35}
36
37impl_float_to_int!(f16 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
38impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
39impl_float_to_int!(f64 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
40impl_float_to_int!(f128 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
41
42/// Implement `From<bool>` for integers
43macro_rules! impl_from_bool {
44    ($($int:ty)*) => {$(
45        #[stable(feature = "from_bool", since = "1.28.0")]
46        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
47        impl const From<bool> for $int {
48            /// Converts from [`bool`] to
49            #[doc = concat!("[`", stringify!($int), "`]")]
50            /// , by turning `false` into `0` and `true` into `1`.
51            ///
52            /// # Examples
53            ///
54            /// ```
55            #[doc = concat!("assert_eq!(", stringify!($int), "::from(false), 0);")]
56            ///
57            #[doc = concat!("assert_eq!(", stringify!($int), "::from(true), 1);")]
58            /// ```
59            #[inline(always)]
60            #[ferrocene::prevalidated]
61            fn from(b: bool) -> Self {
62                b as Self
63            }
64        }
65    )*}
66}
67
68// boolean -> integer
69impl_from_bool!(u8 u16 u32 u64 u128 usize);
70impl_from_bool!(i8 i16 i32 i64 i128 isize);
71
72/// Implement `From<$small>` for `$large`
73macro_rules! impl_from {
74    ($small:ty => $large:ty, $(#[$attrs:meta]),+) => {
75        $(#[$attrs])+
76        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
77        impl const From<$small> for $large {
78            #[doc = concat!("Converts from [`", stringify!($small), "`] to [`", stringify!($large), "`] losslessly.")]
79            #[inline(always)]
80            #[ferrocene::prevalidated]
81            fn from(small: $small) -> Self {
82                debug_assert!(<$large>::MIN as i128 <= <$small>::MIN as i128);
83                debug_assert!(<$small>::MAX as u128 <= <$large>::MAX as u128);
84                small as Self
85            }
86        }
87    }
88}
89
90// unsigned integer -> unsigned integer
91impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
92impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
93impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
94impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
95impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
96impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
97impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
98impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
99impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
100impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
101impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
102
103// signed integer -> signed integer
104impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
105impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
106impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
107impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
108impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
109impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
110impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
111impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
112impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
113impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
114impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
115
116// unsigned integer -> signed integer
117impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
118impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
119impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
120impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
121impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
122impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
123impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
124impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
125impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
126impl_from!(u64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
127
128// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
129// which imply that pointer-sized integers must be at least 16 bits:
130// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
131impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
132impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
133impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
134
135// RISC-V defines the possibility of a 128-bit address space (RV128).
136
137// CHERI proposes 128-bit “capabilities”. Unclear if this would be relevant to usize/isize.
138// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
139// https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf
140
141// Note: integers can only be represented with full precision in a float if
142// they fit in the significand, which is:
143// * 11 bits in f16
144// * 24 bits in f32
145// * 53 bits in f64
146// * 113 bits in f128
147// Lossy float conversions are not implemented at this time.
148// FIXME(f16,f128): The `f16`/`f128` impls `#[stable]` attributes should be changed to reference
149// `f16`/`f128` when they are stabilised (trait impls have to have a `#[stable]` attribute, but none
150// of the `f16`/`f128` impls can be used on stable as the `f16` and `f128` types are unstable).
151
152// signed integer -> float
153impl_from!(i8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
154impl_from!(i8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
155impl_from!(i8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
156impl_from!(i8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157impl_from!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159impl_from!(i16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
160impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
161impl_from!(i32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
162impl_from!(i64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
163
164// unsigned integer -> float
165impl_from!(u8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
166impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
167impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
168impl_from!(u8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
169impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
170impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
171impl_from!(u16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
172impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
173impl_from!(u32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
174impl_from!(u64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
175
176// float -> float
177
178// FIXME(f16): adding the additional `From<{float}>` impl to `f32` would break inference in cases
179// like `f32::from(1.0)`. The type checker has a custom workaround to keep that and similar code
180// compiling even with the second `From<16> for f32` instance. We keep this instance unstable for
181// now so that we can later remove the workaround.
182//
183// See also <https://github.com/rust-lang/rust/issues/123831>.
184impl_from!(f16 => f32, #[unstable(feature = "f32_from_f16", issue = "154005")], #[unstable_feature_bound(f32_from_f16)]);
185impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
186impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
187impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
188impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
189impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
190
191macro_rules! impl_float_from_bool {
192    (
193        $float:ty $(;
194            doctest_prefix: $(#[doc = $doctest_prefix:literal])*
195            doctest_suffix: $(#[doc = $doctest_suffix:literal])*
196        )?
197    ) => {
198        #[stable(feature = "float_from_bool", since = "1.68.0")]
199        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
200            impl const From<bool> for $float {
201            #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
202            /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
203            ///
204            /// # Examples
205            /// ```
206            $($(#[doc = $doctest_prefix])*)?
207            #[doc = concat!("let x = ", stringify!($float), "::from(false);")]
208            /// assert_eq!(x, 0.0);
209            /// assert!(x.is_sign_positive());
210            ///
211            #[doc = concat!("let y = ", stringify!($float), "::from(true);")]
212            /// assert_eq!(y, 1.0);
213            $($(#[doc = $doctest_suffix])*)?
214            /// ```
215            #[inline]
216            fn from(small: bool) -> Self {
217                small as u8 as Self
218            }
219        }
220    };
221}
222
223// boolean -> float
224impl_float_from_bool!(
225    f16;
226    doctest_prefix:
227    // rustdoc doesn't remove the conventional space after the `///`
228    ///# #![allow(unused_features)]
229    ///#![feature(f16)]
230    ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
231    ///
232    doctest_suffix:
233    ///# }
234);
235impl_float_from_bool!(f32);
236impl_float_from_bool!(f64);
237impl_float_from_bool!(
238    f128;
239    doctest_prefix:
240    ///# #![allow(unused_features)]
241    ///#![feature(f128)]
242    ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
243    ///
244    doctest_suffix:
245    ///# }
246);
247
248// no possible bounds violation
249macro_rules! impl_try_from_unbounded {
250    ($source:ty => $($target:ty),+) => {$(
251        #[stable(feature = "try_from", since = "1.34.0")]
252        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
253        impl const TryFrom<$source> for $target {
254            type Error = TryFromIntError;
255
256            /// Tries to create the target number type from a source
257            /// number type. This returns an error if the source value
258            /// is outside of the range of the target type.
259            #[inline]
260            #[ferrocene::prevalidated]
261            fn try_from(value: $source) -> Result<Self, Self::Error> {
262                Ok(value as Self)
263            }
264        }
265    )*}
266}
267
268// only negative bounds
269macro_rules! impl_try_from_lower_bounded {
270    ($source:ty => $($target:ty),+) => {$(
271        #[stable(feature = "try_from", since = "1.34.0")]
272        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
273        impl const TryFrom<$source> for $target {
274            type Error = TryFromIntError;
275
276            /// Tries to create the target number type from a source
277            /// number type. This returns an error if the source value
278            /// is outside of the range of the target type.
279            #[inline]
280            #[ferrocene::prevalidated]
281            fn try_from(u: $source) -> Result<Self, Self::Error> {
282                if u >= 0 {
283                    Ok(u as Self)
284                } else {
285                    Err(TryFromIntError(IntErrorKind::NegOverflow))
286                }
287            }
288        }
289    )*}
290}
291
292// unsigned to signed (only positive bound)
293macro_rules! impl_try_from_upper_bounded {
294    ($source:ty => $($target:ty),+) => {$(
295        #[stable(feature = "try_from", since = "1.34.0")]
296        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
297        impl const TryFrom<$source> for $target {
298            type Error = TryFromIntError;
299
300            /// Tries to create the target number type from a source
301            /// number type. This returns an error if the source value
302            /// is outside of the range of the target type.
303            #[inline]
304            #[ferrocene::prevalidated]
305            fn try_from(u: $source) -> Result<Self, Self::Error> {
306                if u > (Self::MAX as $source) {
307                    Err(TryFromIntError(IntErrorKind::PosOverflow))
308                } else {
309                    Ok(u as Self)
310                }
311            }
312        }
313    )*}
314}
315
316// all other cases
317macro_rules! impl_try_from_both_bounded {
318    ($source:ty => $($target:ty),+) => {$(
319        #[stable(feature = "try_from", since = "1.34.0")]
320        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
321        impl const TryFrom<$source> for $target {
322            type Error = TryFromIntError;
323
324            /// Tries to create the target number type from a source
325            /// number type. This returns an error if the source value
326            /// is outside of the range of the target type.
327            #[inline]
328            #[ferrocene::prevalidated]
329            fn try_from(u: $source) -> Result<Self, Self::Error> {
330                let min = Self::MIN as $source;
331                let max = Self::MAX as $source;
332                if u < min {
333                    Err(TryFromIntError(IntErrorKind::NegOverflow))
334                } else if u > max {
335                    Err(TryFromIntError(IntErrorKind::PosOverflow))
336                } else {
337                    Ok(u as Self)
338                }
339            }
340        }
341    )*}
342}
343
344/// Implement `TryFrom<integer>` for `bool`
345macro_rules! impl_try_from_integer_for_bool {
346    ($signedness:ident $($int:ty)+) => {$(
347        #[stable(feature = "bool_try_from_int", since = "1.95.0")]
348        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
349        impl const TryFrom<$int> for bool {
350            type Error = TryFromIntError;
351
352            /// Tries to create a bool from an integer type.
353            /// Returns an error if the integer is not 0 or 1.
354            ///
355            /// # Examples
356            ///
357            /// ```
358            #[doc = concat!("assert_eq!(bool::try_from(0_", stringify!($int), "), Ok(false));")]
359            ///
360            #[doc = concat!("assert_eq!(bool::try_from(1_", stringify!($int), "), Ok(true));")]
361            ///
362            #[doc = concat!("assert!(bool::try_from(2_", stringify!($int), ").is_err());")]
363            /// ```
364            #[inline]
365            #[ferrocene::prevalidated]
366            fn try_from(i: $int) -> Result<Self, Self::Error> {
367                sign_dependent_expr!{
368                    $signedness ?
369                    if signed {
370                        match i {
371                            0 => Ok(false),
372                            1 => Ok(true),
373                            ..0 => Err(TryFromIntError(IntErrorKind::NegOverflow)),
374                            2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
375                        }
376                    }
377                    if unsigned {
378                        match i {
379                            0 => Ok(false),
380                            1 => Ok(true),
381                            2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
382                        }
383                    }
384                }
385            }
386        }
387    )*}
388}
389
390macro_rules! rev {
391    ($mac:ident, $source:ty => $($target:ty),+) => {$(
392        $mac!($target => $source);
393    )*}
394}
395
396// integer -> bool
397impl_try_from_integer_for_bool!(unsigned u128 u64 u32 u16 u8);
398impl_try_from_integer_for_bool!(signed i128 i64 i32 i16 i8);
399
400// unsigned integer -> unsigned integer
401impl_try_from_upper_bounded!(u16 => u8);
402impl_try_from_upper_bounded!(u32 => u8, u16);
403impl_try_from_upper_bounded!(u64 => u8, u16, u32);
404impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
405
406// signed integer -> signed integer
407impl_try_from_both_bounded!(i16 => i8);
408impl_try_from_both_bounded!(i32 => i8, i16);
409impl_try_from_both_bounded!(i64 => i8, i16, i32);
410impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
411
412// unsigned integer -> signed integer
413impl_try_from_upper_bounded!(u8 => i8);
414impl_try_from_upper_bounded!(u16 => i8, i16);
415impl_try_from_upper_bounded!(u32 => i8, i16, i32);
416impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
417impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
418
419// signed integer -> unsigned integer
420impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
421impl_try_from_both_bounded!(i16 => u8);
422impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
423impl_try_from_both_bounded!(i32 => u8, u16);
424impl_try_from_lower_bounded!(i32 => u32, u64, u128);
425impl_try_from_both_bounded!(i64 => u8, u16, u32);
426impl_try_from_lower_bounded!(i64 => u64, u128);
427impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
428impl_try_from_lower_bounded!(i128 => u128);
429
430// usize/isize
431impl_try_from_upper_bounded!(usize => isize);
432impl_try_from_lower_bounded!(isize => usize);
433
434#[cfg(target_pointer_width = "16")]
435mod ptr_try_from_impls {
436    use super::{IntErrorKind, TryFromIntError};
437
438    impl_try_from_upper_bounded!(usize => u8);
439    impl_try_from_unbounded!(usize => u16, u32, u64, u128);
440    impl_try_from_upper_bounded!(usize => i8, i16);
441    impl_try_from_unbounded!(usize => i32, i64, i128);
442
443    impl_try_from_both_bounded!(isize => u8);
444    impl_try_from_lower_bounded!(isize => u16, u32, u64, u128);
445    impl_try_from_both_bounded!(isize => i8);
446    impl_try_from_unbounded!(isize => i16, i32, i64, i128);
447
448    rev!(impl_try_from_upper_bounded, usize => u32, u64, u128);
449    rev!(impl_try_from_lower_bounded, usize => i8, i16);
450    rev!(impl_try_from_both_bounded, usize => i32, i64, i128);
451
452    rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128);
453    rev!(impl_try_from_both_bounded, isize => i32, i64, i128);
454}
455
456#[cfg(target_pointer_width = "32")]
457mod ptr_try_from_impls {
458    use super::{IntErrorKind, TryFromIntError};
459
460    impl_try_from_upper_bounded!(usize => u8, u16);
461    impl_try_from_unbounded!(usize => u32, u64, u128);
462    impl_try_from_upper_bounded!(usize => i8, i16, i32);
463    impl_try_from_unbounded!(usize => i64, i128);
464
465    impl_try_from_both_bounded!(isize => u8, u16);
466    impl_try_from_lower_bounded!(isize => u32, u64, u128);
467    impl_try_from_both_bounded!(isize => i8, i16);
468    impl_try_from_unbounded!(isize => i32, i64, i128);
469
470    rev!(impl_try_from_unbounded, usize => u32);
471    rev!(impl_try_from_upper_bounded, usize => u64, u128);
472    rev!(impl_try_from_lower_bounded, usize => i8, i16, i32);
473    rev!(impl_try_from_both_bounded, usize => i64, i128);
474
475    rev!(impl_try_from_unbounded, isize => u16);
476    rev!(impl_try_from_upper_bounded, isize => u32, u64, u128);
477    rev!(impl_try_from_unbounded, isize => i32);
478    rev!(impl_try_from_both_bounded, isize => i64, i128);
479}
480
481#[cfg(target_pointer_width = "64")]
482mod ptr_try_from_impls {
483    use super::{IntErrorKind, TryFromIntError};
484
485    impl_try_from_upper_bounded!(usize => u8, u16, u32);
486    impl_try_from_unbounded!(usize => u64, u128);
487    impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
488    impl_try_from_unbounded!(usize => i128);
489
490    impl_try_from_both_bounded!(isize => u8, u16, u32);
491    impl_try_from_lower_bounded!(isize => u64, u128);
492    impl_try_from_both_bounded!(isize => i8, i16, i32);
493    impl_try_from_unbounded!(isize => i64, i128);
494
495    rev!(impl_try_from_unbounded, usize => u32, u64);
496    rev!(impl_try_from_upper_bounded, usize => u128);
497    rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
498    rev!(impl_try_from_both_bounded, usize => i128);
499
500    rev!(impl_try_from_unbounded, isize => u16, u32);
501    rev!(impl_try_from_upper_bounded, isize => u64, u128);
502    rev!(impl_try_from_unbounded, isize => i32, i64);
503    rev!(impl_try_from_both_bounded, isize => i128);
504}
505
506// Conversion traits for non-zero integer types
507use crate::num::NonZero;
508
509macro_rules! impl_nonzero_int_from_nonzero_int {
510    ($Small:ty => $Large:ty) => {
511        #[stable(feature = "nz_int_conv", since = "1.41.0")]
512        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
513        impl const From<NonZero<$Small>> for NonZero<$Large> {
514            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
515            // Rustdocs on functions do not.
516            #[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
517            #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Large), "]></code> losslessly.")]
518            #[inline]
519            fn from(small: NonZero<$Small>) -> Self {
520                // SAFETY: input type guarantees the value is non-zero
521                unsafe { Self::new_unchecked(From::from(small.get())) }
522            }
523        }
524    };
525}
526
527// non-zero unsigned integer -> non-zero unsigned integer
528impl_nonzero_int_from_nonzero_int!(u8 => u16);
529impl_nonzero_int_from_nonzero_int!(u8 => u32);
530impl_nonzero_int_from_nonzero_int!(u8 => u64);
531impl_nonzero_int_from_nonzero_int!(u8 => u128);
532impl_nonzero_int_from_nonzero_int!(u8 => usize);
533impl_nonzero_int_from_nonzero_int!(u16 => u32);
534impl_nonzero_int_from_nonzero_int!(u16 => u64);
535impl_nonzero_int_from_nonzero_int!(u16 => u128);
536impl_nonzero_int_from_nonzero_int!(u16 => usize);
537impl_nonzero_int_from_nonzero_int!(u32 => u64);
538impl_nonzero_int_from_nonzero_int!(u32 => u128);
539impl_nonzero_int_from_nonzero_int!(u64 => u128);
540
541// non-zero signed integer -> non-zero signed integer
542impl_nonzero_int_from_nonzero_int!(i8 => i16);
543impl_nonzero_int_from_nonzero_int!(i8 => i32);
544impl_nonzero_int_from_nonzero_int!(i8 => i64);
545impl_nonzero_int_from_nonzero_int!(i8 => i128);
546impl_nonzero_int_from_nonzero_int!(i8 => isize);
547impl_nonzero_int_from_nonzero_int!(i16 => i32);
548impl_nonzero_int_from_nonzero_int!(i16 => i64);
549impl_nonzero_int_from_nonzero_int!(i16 => i128);
550impl_nonzero_int_from_nonzero_int!(i16 => isize);
551impl_nonzero_int_from_nonzero_int!(i32 => i64);
552impl_nonzero_int_from_nonzero_int!(i32 => i128);
553impl_nonzero_int_from_nonzero_int!(i64 => i128);
554
555// non-zero unsigned -> non-zero signed integer
556impl_nonzero_int_from_nonzero_int!(u8 => i16);
557impl_nonzero_int_from_nonzero_int!(u8 => i32);
558impl_nonzero_int_from_nonzero_int!(u8 => i64);
559impl_nonzero_int_from_nonzero_int!(u8 => i128);
560impl_nonzero_int_from_nonzero_int!(u8 => isize);
561impl_nonzero_int_from_nonzero_int!(u16 => i32);
562impl_nonzero_int_from_nonzero_int!(u16 => i64);
563impl_nonzero_int_from_nonzero_int!(u16 => i128);
564impl_nonzero_int_from_nonzero_int!(u32 => i64);
565impl_nonzero_int_from_nonzero_int!(u32 => i128);
566impl_nonzero_int_from_nonzero_int!(u64 => i128);
567
568macro_rules! impl_nonzero_int_try_from_int {
569    ($Int:ty) => {
570        #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
571        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
572        impl const TryFrom<$Int> for NonZero<$Int> {
573            type Error = TryFromIntError;
574
575            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
576            // Rustdocs on functions do not.
577            #[doc = concat!("Attempts to convert [`", stringify!($Int), "`] ")]
578            #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Int), "]></code>.")]
579            #[inline]
580            fn try_from(value: $Int) -> Result<Self, Self::Error> {
581                Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
582            }
583        }
584    };
585}
586
587// integer -> non-zero integer
588impl_nonzero_int_try_from_int!(u8);
589impl_nonzero_int_try_from_int!(u16);
590impl_nonzero_int_try_from_int!(u32);
591impl_nonzero_int_try_from_int!(u64);
592impl_nonzero_int_try_from_int!(u128);
593impl_nonzero_int_try_from_int!(usize);
594impl_nonzero_int_try_from_int!(i8);
595impl_nonzero_int_try_from_int!(i16);
596impl_nonzero_int_try_from_int!(i32);
597impl_nonzero_int_try_from_int!(i64);
598impl_nonzero_int_try_from_int!(i128);
599impl_nonzero_int_try_from_int!(isize);
600
601macro_rules! impl_nonzero_int_try_from_nonzero_int {
602    ($source:ty => $($target:ty),+) => {$(
603        #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
604        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
605        impl const TryFrom<NonZero<$source>> for NonZero<$target> {
606            type Error = TryFromIntError;
607
608            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
609            // Rustdocs on functions do not.
610            #[doc = concat!("Attempts to convert <code>[NonZero]\\<[", stringify!($source), "]></code> ")]
611            #[doc = concat!("to <code>[NonZero]\\<[", stringify!($target), "]></code>.")]
612            #[inline]
613            fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> {
614                // SAFETY: Input is guaranteed to be non-zero.
615                Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
616            }
617        }
618    )*};
619}
620
621// unsigned non-zero integer -> unsigned non-zero integer
622impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
623impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
624impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
625impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
626impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
627
628// signed non-zero integer -> signed non-zero integer
629impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
630impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
631impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
632impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
633impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
634
635// unsigned non-zero integer -> signed non-zero integer
636impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
637impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
638impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
639impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
640impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
641impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
642
643// signed non-zero integer -> unsigned non-zero integer
644impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
645impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
646impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
647impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
648impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
649impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);