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