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