]> git.lizzy.rs Git - rust.git/blob - library/core/src/convert/num.rs
Make integer-to-integer `From` impls `#[inline(always)]`
[rust.git] / library / core / src / convert / num.rs
1 use super::{From, TryFrom};
2 use crate::num::TryFromIntError;
3
4 mod private {
5     /// This trait being unreachable from outside the crate
6     /// prevents other implementations of the `FloatToInt` trait,
7     /// which allows potentially adding more trait methods after the trait is `#[stable]`.
8     #[unstable(feature = "convert_float_to_int", issue = "67057")]
9     pub trait Sealed {}
10 }
11
12 /// Supporting trait for inherent methods of `f32` and `f64` such as `to_int_unchecked`.
13 /// Typically doesn’t need to be used directly.
14 #[unstable(feature = "convert_float_to_int", issue = "67057")]
15 pub trait FloatToInt<Int>: private::Sealed + Sized {
16     #[unstable(feature = "convert_float_to_int", issue = "67057")]
17     #[doc(hidden)]
18     unsafe fn to_int_unchecked(self) -> Int;
19 }
20
21 macro_rules! impl_float_to_int {
22     ( $Float: ident => $( $Int: ident )+ ) => {
23         #[unstable(feature = "convert_float_to_int", issue = "67057")]
24         impl private::Sealed for $Float {}
25         $(
26             #[unstable(feature = "convert_float_to_int", issue = "67057")]
27             impl FloatToInt<$Int> for $Float {
28                 #[inline]
29                 unsafe fn to_int_unchecked(self) -> $Int {
30                     // SAFETY: the safety contract must be upheld by the caller.
31                     unsafe { crate::intrinsics::float_to_int_unchecked(self) }
32                 }
33             }
34         )+
35     }
36 }
37
38 impl_float_to_int!(f32 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
39 impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
40
41 // Conversion traits for primitive integer and float types
42 // Conversions T -> T are covered by a blanket impl and therefore excluded
43 // Some conversions from and to usize/isize are not implemented due to portability concerns
44 macro_rules! impl_from {
45     ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
46         #[$attr]
47         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
48         impl const From<$Small> for $Large {
49             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
50             // Rustdocs on functions do not.
51             #[doc = $doc]
52             #[inline(always)]
53             fn from(small: $Small) -> Self {
54                 small as Self
55             }
56         }
57     };
58     ($Small: ty, $Large: ty, #[$attr:meta]) => {
59         impl_from!($Small,
60                    $Large,
61                    #[$attr],
62                    concat!("Converts `",
63                            stringify!($Small),
64                            "` to `",
65                            stringify!($Large),
66                            "` losslessly."));
67     }
68 }
69
70 macro_rules! impl_from_bool {
71     ($target: ty, #[$attr:meta]) => {
72         impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
73             stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
74 values.
75
76 # Examples
77
78 ```
79 assert_eq!(", stringify!($target), "::from(true), 1);
80 assert_eq!(", stringify!($target), "::from(false), 0);
81 ```"));
82     };
83 }
84
85 // Bool -> Any
86 impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
87 impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
88 impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
89 impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
90 impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
91 impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
92 impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
93 impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
94 impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
95 impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
96 impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
97 impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
98
99 // Unsigned -> Unsigned
100 impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
101 impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
102 impl_from! { u8, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
103 impl_from! { u8, u128, #[stable(feature = "i128", since = "1.26.0")] }
104 impl_from! { u8, usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
105 impl_from! { u16, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
106 impl_from! { u16, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
107 impl_from! { u16, u128, #[stable(feature = "i128", since = "1.26.0")] }
108 impl_from! { u32, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
109 impl_from! { u32, u128, #[stable(feature = "i128", since = "1.26.0")] }
110 impl_from! { u64, u128, #[stable(feature = "i128", since = "1.26.0")] }
111
112 // Signed -> Signed
113 impl_from! { i8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
114 impl_from! { i8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
115 impl_from! { i8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
116 impl_from! { i8, i128, #[stable(feature = "i128", since = "1.26.0")] }
117 impl_from! { i8, isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
118 impl_from! { i16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
119 impl_from! { i16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
120 impl_from! { i16, i128, #[stable(feature = "i128", since = "1.26.0")] }
121 impl_from! { i32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
122 impl_from! { i32, i128, #[stable(feature = "i128", since = "1.26.0")] }
123 impl_from! { i64, i128, #[stable(feature = "i128", since = "1.26.0")] }
124
125 // Unsigned -> Signed
126 impl_from! { u8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
127 impl_from! { u8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
128 impl_from! { u8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
129 impl_from! { u8, i128, #[stable(feature = "i128", since = "1.26.0")] }
130 impl_from! { u16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
131 impl_from! { u16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
132 impl_from! { u16, i128, #[stable(feature = "i128", since = "1.26.0")] }
133 impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
134 impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
135 impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
136
137 // The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
138 // which imply that pointer-sized integers must be at least 16 bits:
139 // https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
140 impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
141 impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
142 impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
143
144 // RISC-V defines the possibility of a 128-bit address space (RV128).
145
146 // CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
147 // https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
148 // https://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf
149
150 // Note: integers can only be represented with full precision in a float if
151 // they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
152 // Lossy float conversions are not implemented at this time.
153
154 // Signed -> Float
155 impl_from! { i8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
156 impl_from! { i8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
157 impl_from! { i16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
158 impl_from! { i16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
159 impl_from! { i32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
160
161 // Unsigned -> Float
162 impl_from! { u8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
163 impl_from! { u8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
164 impl_from! { u16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
165 impl_from! { u16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
166 impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
167
168 // Float -> Float
169 impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
170
171 // no possible bounds violation
172 macro_rules! try_from_unbounded {
173     ($source:ty, $($target:ty),*) => {$(
174         #[stable(feature = "try_from", since = "1.34.0")]
175         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
176         impl const TryFrom<$source> for $target {
177             type Error = TryFromIntError;
178
179             /// Try to create the target number type from a source
180             /// number type. This returns an error if the source value
181             /// is outside of the range of the target type.
182             #[inline]
183             fn try_from(value: $source) -> Result<Self, Self::Error> {
184                 Ok(value as Self)
185             }
186         }
187     )*}
188 }
189
190 // only negative bounds
191 macro_rules! try_from_lower_bounded {
192     ($source:ty, $($target:ty),*) => {$(
193         #[stable(feature = "try_from", since = "1.34.0")]
194         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
195         impl const TryFrom<$source> for $target {
196             type Error = TryFromIntError;
197
198             /// Try to create the target number type from a source
199             /// number type. This returns an error if the source value
200             /// is outside of the range of the target type.
201             #[inline]
202             fn try_from(u: $source) -> Result<Self, Self::Error> {
203                 if u >= 0 {
204                     Ok(u as Self)
205                 } else {
206                     Err(TryFromIntError(()))
207                 }
208             }
209         }
210     )*}
211 }
212
213 // unsigned to signed (only positive bound)
214 macro_rules! try_from_upper_bounded {
215     ($source:ty, $($target:ty),*) => {$(
216         #[stable(feature = "try_from", since = "1.34.0")]
217         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
218         impl const TryFrom<$source> for $target {
219             type Error = TryFromIntError;
220
221             /// Try to create the target number type from a source
222             /// number type. This returns an error if the source value
223             /// is outside of the range of the target type.
224             #[inline]
225             fn try_from(u: $source) -> Result<Self, Self::Error> {
226                 if u > (Self::MAX as $source) {
227                     Err(TryFromIntError(()))
228                 } else {
229                     Ok(u as Self)
230                 }
231             }
232         }
233     )*}
234 }
235
236 // all other cases
237 macro_rules! try_from_both_bounded {
238     ($source:ty, $($target:ty),*) => {$(
239         #[stable(feature = "try_from", since = "1.34.0")]
240         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
241         impl const TryFrom<$source> for $target {
242             type Error = TryFromIntError;
243
244             /// Try to create the target number type from a source
245             /// number type. This returns an error if the source value
246             /// is outside of the range of the target type.
247             #[inline]
248             fn try_from(u: $source) -> Result<Self, Self::Error> {
249                 let min = Self::MIN as $source;
250                 let max = Self::MAX as $source;
251                 if u < min || u > max {
252                     Err(TryFromIntError(()))
253                 } else {
254                     Ok(u as Self)
255                 }
256             }
257         }
258     )*}
259 }
260
261 macro_rules! rev {
262     ($mac:ident, $source:ty, $($target:ty),*) => {$(
263         $mac!($target, $source);
264     )*}
265 }
266
267 // intra-sign conversions
268 try_from_upper_bounded!(u16, u8);
269 try_from_upper_bounded!(u32, u16, u8);
270 try_from_upper_bounded!(u64, u32, u16, u8);
271 try_from_upper_bounded!(u128, u64, u32, u16, u8);
272
273 try_from_both_bounded!(i16, i8);
274 try_from_both_bounded!(i32, i16, i8);
275 try_from_both_bounded!(i64, i32, i16, i8);
276 try_from_both_bounded!(i128, i64, i32, i16, i8);
277
278 // unsigned-to-signed
279 try_from_upper_bounded!(u8, i8);
280 try_from_upper_bounded!(u16, i8, i16);
281 try_from_upper_bounded!(u32, i8, i16, i32);
282 try_from_upper_bounded!(u64, i8, i16, i32, i64);
283 try_from_upper_bounded!(u128, i8, i16, i32, i64, i128);
284
285 // signed-to-unsigned
286 try_from_lower_bounded!(i8, u8, u16, u32, u64, u128);
287 try_from_lower_bounded!(i16, u16, u32, u64, u128);
288 try_from_lower_bounded!(i32, u32, u64, u128);
289 try_from_lower_bounded!(i64, u64, u128);
290 try_from_lower_bounded!(i128, u128);
291 try_from_both_bounded!(i16, u8);
292 try_from_both_bounded!(i32, u16, u8);
293 try_from_both_bounded!(i64, u32, u16, u8);
294 try_from_both_bounded!(i128, u64, u32, u16, u8);
295
296 // usize/isize
297 try_from_upper_bounded!(usize, isize);
298 try_from_lower_bounded!(isize, usize);
299
300 #[cfg(target_pointer_width = "16")]
301 mod ptr_try_from_impls {
302     use super::TryFromIntError;
303     use crate::convert::TryFrom;
304
305     try_from_upper_bounded!(usize, u8);
306     try_from_unbounded!(usize, u16, u32, u64, u128);
307     try_from_upper_bounded!(usize, i8, i16);
308     try_from_unbounded!(usize, i32, i64, i128);
309
310     try_from_both_bounded!(isize, u8);
311     try_from_lower_bounded!(isize, u16, u32, u64, u128);
312     try_from_both_bounded!(isize, i8);
313     try_from_unbounded!(isize, i16, i32, i64, i128);
314
315     rev!(try_from_upper_bounded, usize, u32, u64, u128);
316     rev!(try_from_lower_bounded, usize, i8, i16);
317     rev!(try_from_both_bounded, usize, i32, i64, i128);
318
319     rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
320     rev!(try_from_both_bounded, isize, i32, i64, i128);
321 }
322
323 #[cfg(target_pointer_width = "32")]
324 mod ptr_try_from_impls {
325     use super::TryFromIntError;
326     use crate::convert::TryFrom;
327
328     try_from_upper_bounded!(usize, u8, u16);
329     try_from_unbounded!(usize, u32, u64, u128);
330     try_from_upper_bounded!(usize, i8, i16, i32);
331     try_from_unbounded!(usize, i64, i128);
332
333     try_from_both_bounded!(isize, u8, u16);
334     try_from_lower_bounded!(isize, u32, u64, u128);
335     try_from_both_bounded!(isize, i8, i16);
336     try_from_unbounded!(isize, i32, i64, i128);
337
338     rev!(try_from_unbounded, usize, u32);
339     rev!(try_from_upper_bounded, usize, u64, u128);
340     rev!(try_from_lower_bounded, usize, i8, i16, i32);
341     rev!(try_from_both_bounded, usize, i64, i128);
342
343     rev!(try_from_unbounded, isize, u16);
344     rev!(try_from_upper_bounded, isize, u32, u64, u128);
345     rev!(try_from_unbounded, isize, i32);
346     rev!(try_from_both_bounded, isize, i64, i128);
347 }
348
349 #[cfg(target_pointer_width = "64")]
350 mod ptr_try_from_impls {
351     use super::TryFromIntError;
352     use crate::convert::TryFrom;
353
354     try_from_upper_bounded!(usize, u8, u16, u32);
355     try_from_unbounded!(usize, u64, u128);
356     try_from_upper_bounded!(usize, i8, i16, i32, i64);
357     try_from_unbounded!(usize, i128);
358
359     try_from_both_bounded!(isize, u8, u16, u32);
360     try_from_lower_bounded!(isize, u64, u128);
361     try_from_both_bounded!(isize, i8, i16, i32);
362     try_from_unbounded!(isize, i64, i128);
363
364     rev!(try_from_unbounded, usize, u32, u64);
365     rev!(try_from_upper_bounded, usize, u128);
366     rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
367     rev!(try_from_both_bounded, usize, i128);
368
369     rev!(try_from_unbounded, isize, u16, u32);
370     rev!(try_from_upper_bounded, isize, u64, u128);
371     rev!(try_from_unbounded, isize, i32, i64);
372     rev!(try_from_both_bounded, isize, i128);
373 }
374
375 // Conversion traits for non-zero integer types
376 use crate::num::NonZeroI128;
377 use crate::num::NonZeroI16;
378 use crate::num::NonZeroI32;
379 use crate::num::NonZeroI64;
380 use crate::num::NonZeroI8;
381 use crate::num::NonZeroIsize;
382 use crate::num::NonZeroU128;
383 use crate::num::NonZeroU16;
384 use crate::num::NonZeroU32;
385 use crate::num::NonZeroU64;
386 use crate::num::NonZeroU8;
387 use crate::num::NonZeroUsize;
388
389 macro_rules! nzint_impl_from {
390     ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
391         #[$attr]
392         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
393         impl const From<$Small> for $Large {
394             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
395             // Rustdocs on functions do not.
396             #[doc = $doc]
397             #[inline]
398             fn from(small: $Small) -> Self {
399                 // SAFETY: input type guarantees the value is non-zero
400                 unsafe {
401                     Self::new_unchecked(From::from(small.get()))
402                 }
403             }
404         }
405     };
406     ($Small: ty, $Large: ty, #[$attr:meta]) => {
407         nzint_impl_from!($Small,
408                    $Large,
409                    #[$attr],
410                    concat!("Converts `",
411                            stringify!($Small),
412                            "` to `",
413                            stringify!($Large),
414                            "` losslessly."));
415     }
416 }
417
418 // Non-zero Unsigned -> Non-zero Unsigned
419 nzint_impl_from! { NonZeroU8, NonZeroU16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
420 nzint_impl_from! { NonZeroU8, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
421 nzint_impl_from! { NonZeroU8, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
422 nzint_impl_from! { NonZeroU8, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
423 nzint_impl_from! { NonZeroU8, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
424 nzint_impl_from! { NonZeroU16, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
425 nzint_impl_from! { NonZeroU16, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
426 nzint_impl_from! { NonZeroU16, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
427 nzint_impl_from! { NonZeroU16, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
428 nzint_impl_from! { NonZeroU32, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
429 nzint_impl_from! { NonZeroU32, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
430 nzint_impl_from! { NonZeroU64, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
431
432 // Non-zero Signed -> Non-zero Signed
433 nzint_impl_from! { NonZeroI8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
434 nzint_impl_from! { NonZeroI8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
435 nzint_impl_from! { NonZeroI8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
436 nzint_impl_from! { NonZeroI8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
437 nzint_impl_from! { NonZeroI8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
438 nzint_impl_from! { NonZeroI16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
439 nzint_impl_from! { NonZeroI16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
440 nzint_impl_from! { NonZeroI16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
441 nzint_impl_from! { NonZeroI16, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
442 nzint_impl_from! { NonZeroI32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
443 nzint_impl_from! { NonZeroI32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
444 nzint_impl_from! { NonZeroI64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
445
446 // NonZero UnSigned -> Non-zero Signed
447 nzint_impl_from! { NonZeroU8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
448 nzint_impl_from! { NonZeroU8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
449 nzint_impl_from! { NonZeroU8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
450 nzint_impl_from! { NonZeroU8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
451 nzint_impl_from! { NonZeroU8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
452 nzint_impl_from! { NonZeroU16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
453 nzint_impl_from! { NonZeroU16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
454 nzint_impl_from! { NonZeroU16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
455 nzint_impl_from! { NonZeroU32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
456 nzint_impl_from! { NonZeroU32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
457 nzint_impl_from! { NonZeroU64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
458
459 macro_rules! nzint_impl_try_from_int {
460     ($Int: ty, $NonZeroInt: ty, #[$attr:meta], $doc: expr) => {
461         #[$attr]
462         impl TryFrom<$Int> for $NonZeroInt {
463             type Error = TryFromIntError;
464
465             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
466             // Rustdocs on functions do not.
467             #[doc = $doc]
468             #[inline]
469             fn try_from(value: $Int) -> Result<Self, Self::Error> {
470                 Self::new(value).ok_or(TryFromIntError(()))
471             }
472         }
473     };
474     ($Int: ty, $NonZeroInt: ty, #[$attr:meta]) => {
475         nzint_impl_try_from_int!($Int,
476                                  $NonZeroInt,
477                                  #[$attr],
478                                  concat!("Attempts to convert `",
479                                          stringify!($Int),
480                                          "` to `",
481                                          stringify!($NonZeroInt),
482                                          "`."));
483     }
484 }
485
486 // Int -> Non-zero Int
487 nzint_impl_try_from_int! { u8, NonZeroU8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
488 nzint_impl_try_from_int! { u16, NonZeroU16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
489 nzint_impl_try_from_int! { u32, NonZeroU32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
490 nzint_impl_try_from_int! { u64, NonZeroU64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
491 nzint_impl_try_from_int! { u128, NonZeroU128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
492 nzint_impl_try_from_int! { usize, NonZeroUsize, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
493 nzint_impl_try_from_int! { i8, NonZeroI8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
494 nzint_impl_try_from_int! { i16, NonZeroI16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
495 nzint_impl_try_from_int! { i32, NonZeroI32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
496 nzint_impl_try_from_int! { i64, NonZeroI64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
497 nzint_impl_try_from_int! { i128, NonZeroI128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
498 nzint_impl_try_from_int! { isize, NonZeroIsize, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
499
500 macro_rules! nzint_impl_try_from_nzint {
501     ($From:ty => $To:ty, $doc: expr) => {
502         #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
503         impl TryFrom<$From> for $To {
504             type Error = TryFromIntError;
505
506             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
507             // Rustdocs on functions do not.
508             #[doc = $doc]
509             #[inline]
510             fn try_from(value: $From) -> Result<Self, Self::Error> {
511                 TryFrom::try_from(value.get()).map(|v| {
512                     // SAFETY: $From is a NonZero type, so v is not zero.
513                     unsafe { Self::new_unchecked(v) }
514                 })
515             }
516         }
517     };
518     ($To:ty: $($From: ty),*) => {$(
519         nzint_impl_try_from_nzint!(
520             $From => $To,
521             concat!(
522                 "Attempts to convert `",
523                 stringify!($From),
524                 "` to `",
525                 stringify!($To),
526                 "`.",
527             )
528         );
529     )*};
530 }
531
532 // Non-zero int -> non-zero unsigned int
533 nzint_impl_try_from_nzint! { NonZeroU8: NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
534 nzint_impl_try_from_nzint! { NonZeroU16: NonZeroI8, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
535 nzint_impl_try_from_nzint! { NonZeroU32: NonZeroI8, NonZeroI16, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
536 nzint_impl_try_from_nzint! { NonZeroU64: NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
537 nzint_impl_try_from_nzint! { NonZeroU128: NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroUsize, NonZeroIsize }
538 nzint_impl_try_from_nzint! { NonZeroUsize: NonZeroI8, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroIsize }
539
540 // Non-zero int -> non-zero signed int
541 nzint_impl_try_from_nzint! { NonZeroI8: NonZeroU8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
542 nzint_impl_try_from_nzint! { NonZeroI16: NonZeroU16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
543 nzint_impl_try_from_nzint! { NonZeroI32: NonZeroU32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
544 nzint_impl_try_from_nzint! { NonZeroI64: NonZeroU64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
545 nzint_impl_try_from_nzint! { NonZeroI128: NonZeroU128, NonZeroUsize, NonZeroIsize }
546 nzint_impl_try_from_nzint! { NonZeroIsize: NonZeroU16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize }