core/convert/
num.rs

1use crate::num::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// Conversion traits for primitive integer and float types
43// Conversions T -> T are covered by a blanket impl and therefore excluded
44// Some conversions from and to usize/isize are not implemented due to portability concerns
45macro_rules! impl_from {
46    (bool => $Int:ty $(,)?) => {
47        impl_from!(
48            bool => $Int,
49            #[stable(feature = "from_bool", since = "1.28.0")],
50            concat!(
51                "Converts a [`bool`] to [`", stringify!($Int), "`] losslessly.\n",
52                "The resulting value is `0` for `false` and `1` for `true` values.\n",
53                "\n",
54                "# Examples\n",
55                "\n",
56                "```\n",
57                "assert_eq!(", stringify!($Int), "::from(true), 1);\n",
58                "assert_eq!(", stringify!($Int), "::from(false), 0);\n",
59                "```\n",
60            ),
61        );
62    };
63    ($Small:ty => $Large:ty, #[$attr:meta] $(,)?) => {
64        impl_from!(
65            $Small => $Large,
66            #[$attr],
67            concat!("Converts [`", stringify!($Small), "`] to [`", stringify!($Large), "`] losslessly."),
68        );
69    };
70    ($Small:ty => $Large:ty, #[$attr:meta], $doc:expr $(,)?) => {
71        #[$attr]
72        impl From<$Small> for $Large {
73            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
74            // Rustdocs on functions do not.
75            #[doc = $doc]
76            #[inline(always)]
77            fn from(small: $Small) -> Self {
78                small as Self
79            }
80        }
81    };
82}
83
84// boolean -> integer
85impl_from!(bool => u8);
86impl_from!(bool => u16);
87impl_from!(bool => u32);
88impl_from!(bool => u64);
89impl_from!(bool => u128);
90impl_from!(bool => usize);
91impl_from!(bool => i8);
92impl_from!(bool => i16);
93impl_from!(bool => i32);
94impl_from!(bool => i64);
95impl_from!(bool => i128);
96impl_from!(bool => isize);
97
98// unsigned integer -> unsigned integer
99impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
100impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
101impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
102impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
103impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
104impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
105impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
106impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
107impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
108impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
109impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
110
111// signed integer -> signed integer
112impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
113impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
114impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
115impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
116impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
117impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
118impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
119impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
120impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
121impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
122impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
123
124// unsigned integer -> signed integer
125impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
126impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
127impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
128impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
129impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
130impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
131impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
132impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
133impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
134impl_from!(u64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
135
136// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
137// which imply that pointer-sized integers must be at least 16 bits:
138// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
139impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
140impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
141impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
142
143// RISC-V defines the possibility of a 128-bit address space (RV128).
144
145// CHERI proposes 128-bit “capabilities”. Unclear if this would be relevant to usize/isize.
146// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
147// https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf
148
149// Note: integers can only be represented with full precision in a float if
150// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
151// Lossy float conversions are not implemented at this time.
152
153// signed integer -> float
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!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159
160// unsigned integer -> float
161impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
162impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
163impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
164impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
165impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
166
167// float -> float
168// FIXME(f16_f128): adding additional `From<{float}>` impls to `f32` breaks inference. See
169// <https://github.com/rust-lang/rust/issues/123831>
170impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
171impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
172impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
173impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
174impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
175
176macro_rules! impl_float_from_bool {
177    ($float:ty) => {
178        #[stable(feature = "float_from_bool", since = "1.68.0")]
179        impl From<bool> for $float {
180            #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
181            /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
182            ///
183            /// # Examples
184            /// ```
185            #[doc = concat!("let x: ", stringify!($float)," = false.into();")]
186            /// assert_eq!(x, 0.0);
187            /// assert!(x.is_sign_positive());
188            ///
189            #[doc = concat!("let y: ", stringify!($float)," = true.into();")]
190            /// assert_eq!(y, 1.0);
191            /// ```
192            #[inline]
193            fn from(small: bool) -> Self {
194                small as u8 as Self
195            }
196        }
197    };
198}
199
200// boolean -> float
201impl_float_from_bool!(f32);
202impl_float_from_bool!(f64);
203
204// no possible bounds violation
205macro_rules! impl_try_from_unbounded {
206    ($source:ty => $($target:ty),+) => {$(
207        #[stable(feature = "try_from", since = "1.34.0")]
208        impl TryFrom<$source> for $target {
209            type Error = TryFromIntError;
210
211            /// Tries to create the target number type from a source
212            /// number type. This returns an error if the source value
213            /// is outside of the range of the target type.
214            #[inline]
215            fn try_from(value: $source) -> Result<Self, Self::Error> {
216                Ok(value as Self)
217            }
218        }
219    )*}
220}
221
222// only negative bounds
223macro_rules! impl_try_from_lower_bounded {
224    ($source:ty => $($target:ty),+) => {$(
225        #[stable(feature = "try_from", since = "1.34.0")]
226        impl TryFrom<$source> for $target {
227            type Error = TryFromIntError;
228
229            /// Tries to create the target number type from a source
230            /// number type. This returns an error if the source value
231            /// is outside of the range of the target type.
232            #[inline]
233            fn try_from(u: $source) -> Result<Self, Self::Error> {
234                if u >= 0 {
235                    Ok(u as Self)
236                } else {
237                    Err(TryFromIntError(()))
238                }
239            }
240        }
241    )*}
242}
243
244// unsigned to signed (only positive bound)
245macro_rules! impl_try_from_upper_bounded {
246    ($source:ty => $($target:ty),+) => {$(
247        #[stable(feature = "try_from", since = "1.34.0")]
248        impl TryFrom<$source> for $target {
249            type Error = TryFromIntError;
250
251            /// Tries to create the target number type from a source
252            /// number type. This returns an error if the source value
253            /// is outside of the range of the target type.
254            #[inline]
255            fn try_from(u: $source) -> Result<Self, Self::Error> {
256                if u > (Self::MAX as $source) {
257                    Err(TryFromIntError(()))
258                } else {
259                    Ok(u as Self)
260                }
261            }
262        }
263    )*}
264}
265
266// all other cases
267macro_rules! impl_try_from_both_bounded {
268    ($source:ty => $($target:ty),+) => {$(
269        #[stable(feature = "try_from", since = "1.34.0")]
270        impl 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                let min = Self::MIN as $source;
279                let max = Self::MAX as $source;
280                if u < min || u > max {
281                    Err(TryFromIntError(()))
282                } else {
283                    Ok(u as Self)
284                }
285            }
286        }
287    )*}
288}
289
290macro_rules! rev {
291    ($mac:ident, $source:ty => $($target:ty),+) => {$(
292        $mac!($target => $source);
293    )*}
294}
295
296// unsigned integer -> unsigned integer
297impl_try_from_upper_bounded!(u16 => u8);
298impl_try_from_upper_bounded!(u32 => u8, u16);
299impl_try_from_upper_bounded!(u64 => u8, u16, u32);
300impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
301
302// signed integer -> signed integer
303impl_try_from_both_bounded!(i16 => i8);
304impl_try_from_both_bounded!(i32 => i8, i16);
305impl_try_from_both_bounded!(i64 => i8, i16, i32);
306impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
307
308// unsigned integer -> signed integer
309impl_try_from_upper_bounded!(u8 => i8);
310impl_try_from_upper_bounded!(u16 => i8, i16);
311impl_try_from_upper_bounded!(u32 => i8, i16, i32);
312impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
313impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
314
315// signed integer -> unsigned integer
316impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
317impl_try_from_both_bounded!(i16 => u8);
318impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
319impl_try_from_both_bounded!(i32 => u8, u16);
320impl_try_from_lower_bounded!(i32 => u32, u64, u128);
321impl_try_from_both_bounded!(i64 => u8, u16, u32);
322impl_try_from_lower_bounded!(i64 => u64, u128);
323impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
324impl_try_from_lower_bounded!(i128 => u128);
325
326// usize/isize
327impl_try_from_upper_bounded!(usize => isize);
328impl_try_from_lower_bounded!(isize => usize);
329
330#[cfg(target_pointer_width = "16")]
331mod ptr_try_from_impls {
332    use super::TryFromIntError;
333
334    impl_try_from_upper_bounded!(usize => u8);
335    impl_try_from_unbounded!(usize => u16, u32, u64, u128);
336    impl_try_from_upper_bounded!(usize => i8, i16);
337    impl_try_from_unbounded!(usize => i32, i64, i128);
338
339    impl_try_from_both_bounded!(isize => u8);
340    impl_try_from_lower_bounded!(isize => u16, u32, u64, u128);
341    impl_try_from_both_bounded!(isize => i8);
342    impl_try_from_unbounded!(isize => i16, i32, i64, i128);
343
344    rev!(impl_try_from_upper_bounded, usize => u32, u64, u128);
345    rev!(impl_try_from_lower_bounded, usize => i8, i16);
346    rev!(impl_try_from_both_bounded, usize => i32, i64, i128);
347
348    rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128);
349    rev!(impl_try_from_both_bounded, isize => i32, i64, i128);
350}
351
352#[cfg(target_pointer_width = "32")]
353mod ptr_try_from_impls {
354    use super::TryFromIntError;
355
356    impl_try_from_upper_bounded!(usize => u8, u16);
357    impl_try_from_unbounded!(usize => u32, u64, u128);
358    impl_try_from_upper_bounded!(usize => i8, i16, i32);
359    impl_try_from_unbounded!(usize => i64, i128);
360
361    impl_try_from_both_bounded!(isize => u8, u16);
362    impl_try_from_lower_bounded!(isize => u32, u64, u128);
363    impl_try_from_both_bounded!(isize => i8, i16);
364    impl_try_from_unbounded!(isize => i32, i64, i128);
365
366    rev!(impl_try_from_unbounded, usize => u32);
367    rev!(impl_try_from_upper_bounded, usize => u64, u128);
368    rev!(impl_try_from_lower_bounded, usize => i8, i16, i32);
369    rev!(impl_try_from_both_bounded, usize => i64, i128);
370
371    rev!(impl_try_from_unbounded, isize => u16);
372    rev!(impl_try_from_upper_bounded, isize => u32, u64, u128);
373    rev!(impl_try_from_unbounded, isize => i32);
374    rev!(impl_try_from_both_bounded, isize => i64, i128);
375}
376
377#[cfg(target_pointer_width = "64")]
378mod ptr_try_from_impls {
379    use super::TryFromIntError;
380
381    impl_try_from_upper_bounded!(usize => u8, u16, u32);
382    impl_try_from_unbounded!(usize => u64, u128);
383    impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
384    impl_try_from_unbounded!(usize => i128);
385
386    impl_try_from_both_bounded!(isize => u8, u16, u32);
387    impl_try_from_lower_bounded!(isize => u64, u128);
388    impl_try_from_both_bounded!(isize => i8, i16, i32);
389    impl_try_from_unbounded!(isize => i64, i128);
390
391    rev!(impl_try_from_unbounded, usize => u32, u64);
392    rev!(impl_try_from_upper_bounded, usize => u128);
393    rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
394    rev!(impl_try_from_both_bounded, usize => i128);
395
396    rev!(impl_try_from_unbounded, isize => u16, u32);
397    rev!(impl_try_from_upper_bounded, isize => u64, u128);
398    rev!(impl_try_from_unbounded, isize => i32, i64);
399    rev!(impl_try_from_both_bounded, isize => i128);
400}
401
402// Conversion traits for non-zero integer types
403use crate::num::NonZero;
404
405macro_rules! impl_nonzero_int_from_nonzero_int {
406    ($Small:ty => $Large:ty) => {
407        #[stable(feature = "nz_int_conv", since = "1.41.0")]
408        impl From<NonZero<$Small>> for NonZero<$Large> {
409            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
410            // Rustdocs on functions do not.
411            #[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
412            #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Large), "]></code> losslessly.")]
413            #[inline]
414            fn from(small: NonZero<$Small>) -> Self {
415                // SAFETY: input type guarantees the value is non-zero
416                unsafe { Self::new_unchecked(From::from(small.get())) }
417            }
418        }
419    };
420}
421
422// non-zero unsigned integer -> non-zero unsigned integer
423impl_nonzero_int_from_nonzero_int!(u8 => u16);
424impl_nonzero_int_from_nonzero_int!(u8 => u32);
425impl_nonzero_int_from_nonzero_int!(u8 => u64);
426impl_nonzero_int_from_nonzero_int!(u8 => u128);
427impl_nonzero_int_from_nonzero_int!(u8 => usize);
428impl_nonzero_int_from_nonzero_int!(u16 => u32);
429impl_nonzero_int_from_nonzero_int!(u16 => u64);
430impl_nonzero_int_from_nonzero_int!(u16 => u128);
431impl_nonzero_int_from_nonzero_int!(u16 => usize);
432impl_nonzero_int_from_nonzero_int!(u32 => u64);
433impl_nonzero_int_from_nonzero_int!(u32 => u128);
434impl_nonzero_int_from_nonzero_int!(u64 => u128);
435
436// non-zero signed integer -> non-zero signed integer
437impl_nonzero_int_from_nonzero_int!(i8 => i16);
438impl_nonzero_int_from_nonzero_int!(i8 => i32);
439impl_nonzero_int_from_nonzero_int!(i8 => i64);
440impl_nonzero_int_from_nonzero_int!(i8 => i128);
441impl_nonzero_int_from_nonzero_int!(i8 => isize);
442impl_nonzero_int_from_nonzero_int!(i16 => i32);
443impl_nonzero_int_from_nonzero_int!(i16 => i64);
444impl_nonzero_int_from_nonzero_int!(i16 => i128);
445impl_nonzero_int_from_nonzero_int!(i16 => isize);
446impl_nonzero_int_from_nonzero_int!(i32 => i64);
447impl_nonzero_int_from_nonzero_int!(i32 => i128);
448impl_nonzero_int_from_nonzero_int!(i64 => i128);
449
450// non-zero unsigned -> non-zero signed integer
451impl_nonzero_int_from_nonzero_int!(u8 => i16);
452impl_nonzero_int_from_nonzero_int!(u8 => i32);
453impl_nonzero_int_from_nonzero_int!(u8 => i64);
454impl_nonzero_int_from_nonzero_int!(u8 => i128);
455impl_nonzero_int_from_nonzero_int!(u8 => isize);
456impl_nonzero_int_from_nonzero_int!(u16 => i32);
457impl_nonzero_int_from_nonzero_int!(u16 => i64);
458impl_nonzero_int_from_nonzero_int!(u16 => i128);
459impl_nonzero_int_from_nonzero_int!(u32 => i64);
460impl_nonzero_int_from_nonzero_int!(u32 => i128);
461impl_nonzero_int_from_nonzero_int!(u64 => i128);
462
463macro_rules! impl_nonzero_int_try_from_int {
464    ($Int:ty) => {
465        #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
466        impl TryFrom<$Int> for NonZero<$Int> {
467            type Error = TryFromIntError;
468
469            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
470            // Rustdocs on functions do not.
471            #[doc = concat!("Attempts to convert [`", stringify!($Int), "`] ")]
472            #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Int), "]></code>.")]
473            #[inline]
474            fn try_from(value: $Int) -> Result<Self, Self::Error> {
475                Self::new(value).ok_or(TryFromIntError(()))
476            }
477        }
478    };
479}
480
481// integer -> non-zero integer
482impl_nonzero_int_try_from_int!(u8);
483impl_nonzero_int_try_from_int!(u16);
484impl_nonzero_int_try_from_int!(u32);
485impl_nonzero_int_try_from_int!(u64);
486impl_nonzero_int_try_from_int!(u128);
487impl_nonzero_int_try_from_int!(usize);
488impl_nonzero_int_try_from_int!(i8);
489impl_nonzero_int_try_from_int!(i16);
490impl_nonzero_int_try_from_int!(i32);
491impl_nonzero_int_try_from_int!(i64);
492impl_nonzero_int_try_from_int!(i128);
493impl_nonzero_int_try_from_int!(isize);
494
495macro_rules! impl_nonzero_int_try_from_nonzero_int {
496    ($source:ty => $($target:ty),+) => {$(
497        #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
498        impl TryFrom<NonZero<$source>> for NonZero<$target> {
499            type Error = TryFromIntError;
500
501            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
502            // Rustdocs on functions do not.
503            #[doc = concat!("Attempts to convert <code>[NonZero]\\<[", stringify!($source), "]></code> ")]
504            #[doc = concat!("to <code>[NonZero]\\<[", stringify!($target), "]></code>.")]
505            #[inline]
506            fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> {
507                // SAFETY: Input is guaranteed to be non-zero.
508                Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
509            }
510        }
511    )*};
512}
513
514// unsigned non-zero integer -> unsigned non-zero integer
515impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
516impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
517impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
518impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
519impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
520
521// signed non-zero integer -> signed non-zero integer
522impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
523impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
524impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
525impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
526impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
527
528// unsigned non-zero integer -> signed non-zero integer
529impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
530impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
531impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
532impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
533impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
534impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
535
536// signed non-zero integer -> unsigned non-zero integer
537impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
538impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
539impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
540impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
541impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
542impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);