]> git.lizzy.rs Git - rust.git/blob - library/core/src/convert/num.rs
Auto merge of #106302 - compiler-errors:terr-coerce-w-infer, r=estebank
[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 // bool -> Float
172 #[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")]
173 #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
174 impl const From<bool> for f32 {
175     /// Converts `bool` to `f32` losslessly.
176     #[inline]
177     fn from(small: bool) -> Self {
178         small as u8 as Self
179     }
180 }
181 #[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")]
182 #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
183 impl const From<bool> for f64 {
184     /// Converts `bool` to `f64` losslessly.
185     #[inline]
186     fn from(small: bool) -> Self {
187         small as u8 as Self
188     }
189 }
190
191 // no possible bounds violation
192 macro_rules! try_from_unbounded {
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(value: $source) -> Result<Self, Self::Error> {
204                 Ok(value as Self)
205             }
206         }
207     )*}
208 }
209
210 // only negative bounds
211 macro_rules! try_from_lower_bounded {
212     ($source:ty, $($target:ty),*) => {$(
213         #[stable(feature = "try_from", since = "1.34.0")]
214         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
215         impl const TryFrom<$source> for $target {
216             type Error = TryFromIntError;
217
218             /// Try to create the target number type from a source
219             /// number type. This returns an error if the source value
220             /// is outside of the range of the target type.
221             #[inline]
222             fn try_from(u: $source) -> Result<Self, Self::Error> {
223                 if u >= 0 {
224                     Ok(u as Self)
225                 } else {
226                     Err(TryFromIntError(()))
227                 }
228             }
229         }
230     )*}
231 }
232
233 // unsigned to signed (only positive bound)
234 macro_rules! try_from_upper_bounded {
235     ($source:ty, $($target:ty),*) => {$(
236         #[stable(feature = "try_from", since = "1.34.0")]
237         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
238         impl const TryFrom<$source> for $target {
239             type Error = TryFromIntError;
240
241             /// Try to create the target number type from a source
242             /// number type. This returns an error if the source value
243             /// is outside of the range of the target type.
244             #[inline]
245             fn try_from(u: $source) -> Result<Self, Self::Error> {
246                 if u > (Self::MAX as $source) {
247                     Err(TryFromIntError(()))
248                 } else {
249                     Ok(u as Self)
250                 }
251             }
252         }
253     )*}
254 }
255
256 // all other cases
257 macro_rules! try_from_both_bounded {
258     ($source:ty, $($target:ty),*) => {$(
259         #[stable(feature = "try_from", since = "1.34.0")]
260         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
261         impl const TryFrom<$source> for $target {
262             type Error = TryFromIntError;
263
264             /// Try to create the target number type from a source
265             /// number type. This returns an error if the source value
266             /// is outside of the range of the target type.
267             #[inline]
268             fn try_from(u: $source) -> Result<Self, Self::Error> {
269                 let min = Self::MIN as $source;
270                 let max = Self::MAX as $source;
271                 if u < min || u > max {
272                     Err(TryFromIntError(()))
273                 } else {
274                     Ok(u as Self)
275                 }
276             }
277         }
278     )*}
279 }
280
281 macro_rules! rev {
282     ($mac:ident, $source:ty, $($target:ty),*) => {$(
283         $mac!($target, $source);
284     )*}
285 }
286
287 // intra-sign conversions
288 try_from_upper_bounded!(u16, u8);
289 try_from_upper_bounded!(u32, u16, u8);
290 try_from_upper_bounded!(u64, u32, u16, u8);
291 try_from_upper_bounded!(u128, u64, u32, u16, u8);
292
293 try_from_both_bounded!(i16, i8);
294 try_from_both_bounded!(i32, i16, i8);
295 try_from_both_bounded!(i64, i32, i16, i8);
296 try_from_both_bounded!(i128, i64, i32, i16, i8);
297
298 // unsigned-to-signed
299 try_from_upper_bounded!(u8, i8);
300 try_from_upper_bounded!(u16, i8, i16);
301 try_from_upper_bounded!(u32, i8, i16, i32);
302 try_from_upper_bounded!(u64, i8, i16, i32, i64);
303 try_from_upper_bounded!(u128, i8, i16, i32, i64, i128);
304
305 // signed-to-unsigned
306 try_from_lower_bounded!(i8, u8, u16, u32, u64, u128);
307 try_from_lower_bounded!(i16, u16, u32, u64, u128);
308 try_from_lower_bounded!(i32, u32, u64, u128);
309 try_from_lower_bounded!(i64, u64, u128);
310 try_from_lower_bounded!(i128, u128);
311 try_from_both_bounded!(i16, u8);
312 try_from_both_bounded!(i32, u16, u8);
313 try_from_both_bounded!(i64, u32, u16, u8);
314 try_from_both_bounded!(i128, u64, u32, u16, u8);
315
316 // usize/isize
317 try_from_upper_bounded!(usize, isize);
318 try_from_lower_bounded!(isize, usize);
319
320 #[cfg(target_pointer_width = "16")]
321 mod ptr_try_from_impls {
322     use super::TryFromIntError;
323     use crate::convert::TryFrom;
324
325     try_from_upper_bounded!(usize, u8);
326     try_from_unbounded!(usize, u16, u32, u64, u128);
327     try_from_upper_bounded!(usize, i8, i16);
328     try_from_unbounded!(usize, i32, i64, i128);
329
330     try_from_both_bounded!(isize, u8);
331     try_from_lower_bounded!(isize, u16, u32, u64, u128);
332     try_from_both_bounded!(isize, i8);
333     try_from_unbounded!(isize, i16, i32, i64, i128);
334
335     rev!(try_from_upper_bounded, usize, u32, u64, u128);
336     rev!(try_from_lower_bounded, usize, i8, i16);
337     rev!(try_from_both_bounded, usize, i32, i64, i128);
338
339     rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
340     rev!(try_from_both_bounded, isize, i32, i64, i128);
341 }
342
343 #[cfg(target_pointer_width = "32")]
344 mod ptr_try_from_impls {
345     use super::TryFromIntError;
346     use crate::convert::TryFrom;
347
348     try_from_upper_bounded!(usize, u8, u16);
349     try_from_unbounded!(usize, u32, u64, u128);
350     try_from_upper_bounded!(usize, i8, i16, i32);
351     try_from_unbounded!(usize, i64, i128);
352
353     try_from_both_bounded!(isize, u8, u16);
354     try_from_lower_bounded!(isize, u32, u64, u128);
355     try_from_both_bounded!(isize, i8, i16);
356     try_from_unbounded!(isize, i32, i64, i128);
357
358     rev!(try_from_unbounded, usize, u32);
359     rev!(try_from_upper_bounded, usize, u64, u128);
360     rev!(try_from_lower_bounded, usize, i8, i16, i32);
361     rev!(try_from_both_bounded, usize, i64, i128);
362
363     rev!(try_from_unbounded, isize, u16);
364     rev!(try_from_upper_bounded, isize, u32, u64, u128);
365     rev!(try_from_unbounded, isize, i32);
366     rev!(try_from_both_bounded, isize, i64, i128);
367 }
368
369 #[cfg(target_pointer_width = "64")]
370 mod ptr_try_from_impls {
371     use super::TryFromIntError;
372     use crate::convert::TryFrom;
373
374     try_from_upper_bounded!(usize, u8, u16, u32);
375     try_from_unbounded!(usize, u64, u128);
376     try_from_upper_bounded!(usize, i8, i16, i32, i64);
377     try_from_unbounded!(usize, i128);
378
379     try_from_both_bounded!(isize, u8, u16, u32);
380     try_from_lower_bounded!(isize, u64, u128);
381     try_from_both_bounded!(isize, i8, i16, i32);
382     try_from_unbounded!(isize, i64, i128);
383
384     rev!(try_from_unbounded, usize, u32, u64);
385     rev!(try_from_upper_bounded, usize, u128);
386     rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
387     rev!(try_from_both_bounded, usize, i128);
388
389     rev!(try_from_unbounded, isize, u16, u32);
390     rev!(try_from_upper_bounded, isize, u64, u128);
391     rev!(try_from_unbounded, isize, i32, i64);
392     rev!(try_from_both_bounded, isize, i128);
393 }
394
395 // Conversion traits for non-zero integer types
396 use crate::num::NonZeroI128;
397 use crate::num::NonZeroI16;
398 use crate::num::NonZeroI32;
399 use crate::num::NonZeroI64;
400 use crate::num::NonZeroI8;
401 use crate::num::NonZeroIsize;
402 use crate::num::NonZeroU128;
403 use crate::num::NonZeroU16;
404 use crate::num::NonZeroU32;
405 use crate::num::NonZeroU64;
406 use crate::num::NonZeroU8;
407 use crate::num::NonZeroUsize;
408
409 macro_rules! nzint_impl_from {
410     ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
411         #[$attr]
412         #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
413         impl const From<$Small> for $Large {
414             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
415             // Rustdocs on functions do not.
416             #[doc = $doc]
417             #[inline]
418             fn from(small: $Small) -> Self {
419                 // SAFETY: input type guarantees the value is non-zero
420                 unsafe {
421                     Self::new_unchecked(From::from(small.get()))
422                 }
423             }
424         }
425     };
426     ($Small: ty, $Large: ty, #[$attr:meta]) => {
427         nzint_impl_from!($Small,
428                    $Large,
429                    #[$attr],
430                    concat!("Converts `",
431                            stringify!($Small),
432                            "` to `",
433                            stringify!($Large),
434                            "` losslessly."));
435     }
436 }
437
438 // Non-zero Unsigned -> Non-zero Unsigned
439 nzint_impl_from! { NonZeroU8, NonZeroU16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
440 nzint_impl_from! { NonZeroU8, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
441 nzint_impl_from! { NonZeroU8, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
442 nzint_impl_from! { NonZeroU8, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
443 nzint_impl_from! { NonZeroU8, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
444 nzint_impl_from! { NonZeroU16, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
445 nzint_impl_from! { NonZeroU16, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
446 nzint_impl_from! { NonZeroU16, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
447 nzint_impl_from! { NonZeroU16, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
448 nzint_impl_from! { NonZeroU32, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
449 nzint_impl_from! { NonZeroU32, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
450 nzint_impl_from! { NonZeroU64, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
451
452 // Non-zero Signed -> Non-zero Signed
453 nzint_impl_from! { NonZeroI8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
454 nzint_impl_from! { NonZeroI8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
455 nzint_impl_from! { NonZeroI8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
456 nzint_impl_from! { NonZeroI8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
457 nzint_impl_from! { NonZeroI8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
458 nzint_impl_from! { NonZeroI16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
459 nzint_impl_from! { NonZeroI16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
460 nzint_impl_from! { NonZeroI16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
461 nzint_impl_from! { NonZeroI16, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
462 nzint_impl_from! { NonZeroI32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
463 nzint_impl_from! { NonZeroI32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
464 nzint_impl_from! { NonZeroI64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
465
466 // NonZero UnSigned -> Non-zero Signed
467 nzint_impl_from! { NonZeroU8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
468 nzint_impl_from! { NonZeroU8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
469 nzint_impl_from! { NonZeroU8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
470 nzint_impl_from! { NonZeroU8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
471 nzint_impl_from! { NonZeroU8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
472 nzint_impl_from! { NonZeroU16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
473 nzint_impl_from! { NonZeroU16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
474 nzint_impl_from! { NonZeroU16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
475 nzint_impl_from! { NonZeroU32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
476 nzint_impl_from! { NonZeroU32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
477 nzint_impl_from! { NonZeroU64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
478
479 macro_rules! nzint_impl_try_from_int {
480     ($Int: ty, $NonZeroInt: ty, #[$attr:meta], $doc: expr) => {
481         #[$attr]
482         impl TryFrom<$Int> for $NonZeroInt {
483             type Error = TryFromIntError;
484
485             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
486             // Rustdocs on functions do not.
487             #[doc = $doc]
488             #[inline]
489             fn try_from(value: $Int) -> Result<Self, Self::Error> {
490                 Self::new(value).ok_or(TryFromIntError(()))
491             }
492         }
493     };
494     ($Int: ty, $NonZeroInt: ty, #[$attr:meta]) => {
495         nzint_impl_try_from_int!($Int,
496                                  $NonZeroInt,
497                                  #[$attr],
498                                  concat!("Attempts to convert `",
499                                          stringify!($Int),
500                                          "` to `",
501                                          stringify!($NonZeroInt),
502                                          "`."));
503     }
504 }
505
506 // Int -> Non-zero Int
507 nzint_impl_try_from_int! { u8, NonZeroU8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
508 nzint_impl_try_from_int! { u16, NonZeroU16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
509 nzint_impl_try_from_int! { u32, NonZeroU32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
510 nzint_impl_try_from_int! { u64, NonZeroU64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
511 nzint_impl_try_from_int! { u128, NonZeroU128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
512 nzint_impl_try_from_int! { usize, NonZeroUsize, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
513 nzint_impl_try_from_int! { i8, NonZeroI8, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
514 nzint_impl_try_from_int! { i16, NonZeroI16, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
515 nzint_impl_try_from_int! { i32, NonZeroI32, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
516 nzint_impl_try_from_int! { i64, NonZeroI64, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
517 nzint_impl_try_from_int! { i128, NonZeroI128, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
518 nzint_impl_try_from_int! { isize, NonZeroIsize, #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")] }
519
520 macro_rules! nzint_impl_try_from_nzint {
521     ($From:ty => $To:ty, $doc: expr) => {
522         #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
523         impl TryFrom<$From> for $To {
524             type Error = TryFromIntError;
525
526             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
527             // Rustdocs on functions do not.
528             #[doc = $doc]
529             #[inline]
530             fn try_from(value: $From) -> Result<Self, Self::Error> {
531                 TryFrom::try_from(value.get()).map(|v| {
532                     // SAFETY: $From is a NonZero type, so v is not zero.
533                     unsafe { Self::new_unchecked(v) }
534                 })
535             }
536         }
537     };
538     ($To:ty: $($From: ty),*) => {$(
539         nzint_impl_try_from_nzint!(
540             $From => $To,
541             concat!(
542                 "Attempts to convert `",
543                 stringify!($From),
544                 "` to `",
545                 stringify!($To),
546                 "`.",
547             )
548         );
549     )*};
550 }
551
552 // Non-zero int -> non-zero unsigned int
553 nzint_impl_try_from_nzint! { NonZeroU8: NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
554 nzint_impl_try_from_nzint! { NonZeroU16: NonZeroI8, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
555 nzint_impl_try_from_nzint! { NonZeroU32: NonZeroI8, NonZeroI16, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
556 nzint_impl_try_from_nzint! { NonZeroU64: NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
557 nzint_impl_try_from_nzint! { NonZeroU128: NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroUsize, NonZeroIsize }
558 nzint_impl_try_from_nzint! { NonZeroUsize: NonZeroI8, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroIsize }
559
560 // Non-zero int -> non-zero signed int
561 nzint_impl_try_from_nzint! { NonZeroI8: NonZeroU8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
562 nzint_impl_try_from_nzint! { NonZeroI16: NonZeroU16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
563 nzint_impl_try_from_nzint! { NonZeroI32: NonZeroU32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
564 nzint_impl_try_from_nzint! { NonZeroI64: NonZeroU64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize }
565 nzint_impl_try_from_nzint! { NonZeroI128: NonZeroU128, NonZeroUsize, NonZeroIsize }
566 nzint_impl_try_from_nzint! { NonZeroIsize: NonZeroU16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize }