Skip to main content

core/convert/
num.rs

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