1use crate::num::{IntErrorKind, TryFromIntError};
2
3#[unstable(feature = "convert_float_to_int", issue = "67057")]
6pub impl(self) trait FloatToInt<Int>: Sized {
7 #[unstable(feature = "convert_float_to_int", issue = "67057")]
8 #[doc(hidden)]
9 unsafe fn to_int_unchecked(self) -> Int;
10}
11
12macro_rules! impl_float_to_int {
13 ($Float:ty => $($Int:ty),+) => {
14 $(
15 #[unstable(feature = "convert_float_to_int", issue = "67057")]
16 impl FloatToInt<$Int> for $Float {
17 #[inline]
18 unsafe fn to_int_unchecked(self) -> $Int {
19 unsafe { crate::intrinsics::float_to_int_unchecked(self) }
21 }
22 }
23 )+
24 }
25}
26
27impl_float_to_int!(f16 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
28impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
29impl_float_to_int!(f64 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
30impl_float_to_int!(f128 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
31
32macro_rules! impl_from_bool {
34 ($($int:ty)*) => {$(
35 #[stable(feature = "from_bool", since = "1.28.0")]
36 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
37 impl const From<bool> for $int {
38 #[doc = concat!("[`", stringify!($int), "`]")]
40 #[doc = concat!("assert_eq!(", stringify!($int), "::from(false), 0);")]
46 #[doc = concat!("assert_eq!(", stringify!($int), "::from(true), 1);")]
48 #[inline(always)]
50 fn from(b: bool) -> Self {
51 b as Self
52 }
53 }
54 )*}
55}
56
57impl_from_bool!(u8 u16 u32 u64 u128 usize);
59impl_from_bool!(i8 i16 i32 i64 i128 isize);
60
61macro_rules! impl_from {
63 ($small:ty => $large:ty, $(#[$attrs:meta]),+) => {
64 $(#[$attrs])+
65 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
66 impl const From<$small> for $large {
67 #[doc = concat!("Converts from [`", stringify!($small), "`] to [`", stringify!($large), "`] losslessly.")]
68 #[inline(always)]
69 fn from(small: $small) -> Self {
70 debug_assert!(<$large>::MIN as i128 <= <$small>::MIN as i128);
71 debug_assert!(<$small>::MAX as u128 <= <$large>::MAX as u128);
72 small as Self
73 }
74 }
75 }
76}
77
78impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
80impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
81impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
82impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
83impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
84impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
85impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
86impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
87impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
88impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
89impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
90
91impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
93impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
94impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
95impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
96impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
97impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
98impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
99impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
100impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
101impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
102impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
103
104impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
106impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
107impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
108impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
109impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
110impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
111impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
112impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
113impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
114impl_from!(u64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
115
116impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
120impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
121impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
122
123impl_from!(i8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
142impl_from!(i8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
143impl_from!(i8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
144impl_from!(i8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
145impl_from!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
146impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
147impl_from!(i16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
148impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
149impl_from!(i32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
150impl_from!(i64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
151
152impl_from!(u8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
154impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
155impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
156impl_from!(u8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159impl_from!(u16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
160impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
161impl_from!(u32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
162impl_from!(u64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
163
164impl_from!(f16 => f32, #[unstable(feature = "f32_from_f16", issue = "154005")], #[unstable_feature_bound(f32_from_f16)]);
173impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
174impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
175impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
176impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
177impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
178
179macro_rules! impl_float_from_bool {
180 (
181 $float:ty $(;
182 doctest_prefix: $(#[doc = $doctest_prefix:literal])*
183 doctest_suffix: $(#[doc = $doctest_suffix:literal])*
184 )?
185 ) => {
186 #[stable(feature = "float_from_bool", since = "1.68.0")]
187 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
188 impl const From<bool> for $float {
189 #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
190 $($(#[doc = $doctest_prefix])*)?
195 #[doc = concat!("let x = ", stringify!($float), "::from(false);")]
196 #[doc = concat!("let y = ", stringify!($float), "::from(true);")]
200 $($(#[doc = $doctest_suffix])*)?
202 #[inline]
204 fn from(small: bool) -> Self {
205 small as u8 as Self
206 }
207 }
208 };
209}
210
211impl_float_from_bool!(
213 f16;
214 doctest_prefix:
215 doctest_suffix:
221 );
223impl_float_from_bool!(f32);
224impl_float_from_bool!(f64);
225impl_float_from_bool!(
226 f128;
227 doctest_prefix:
228 doctest_suffix:
233 );
235
236macro_rules! impl_try_from_unbounded {
238 ($source:ty => $($target:ty),+) => {$(
239 #[stable(feature = "try_from", since = "1.34.0")]
240 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
241 impl const TryFrom<$source> for $target {
242 type Error = TryFromIntError;
243
244 #[inline]
248 fn try_from(value: $source) -> Result<Self, Self::Error> {
249 Ok(value as Self)
250 }
251 }
252 )*}
253}
254
255macro_rules! impl_try_from_lower_bounded {
257 ($source:ty => $($target:ty),+) => {$(
258 #[stable(feature = "try_from", since = "1.34.0")]
259 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
260 impl const TryFrom<$source> for $target {
261 type Error = TryFromIntError;
262
263 #[inline]
267 fn try_from(u: $source) -> Result<Self, Self::Error> {
268 if u >= 0 {
269 Ok(u as Self)
270 } else {
271 Err(TryFromIntError(IntErrorKind::NegOverflow))
272 }
273 }
274 }
275 )*}
276}
277
278macro_rules! impl_try_from_upper_bounded {
280 ($source:ty => $($target:ty),+) => {$(
281 #[stable(feature = "try_from", since = "1.34.0")]
282 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
283 impl const TryFrom<$source> for $target {
284 type Error = TryFromIntError;
285
286 #[inline]
290 fn try_from(u: $source) -> Result<Self, Self::Error> {
291 if u > (Self::MAX as $source) {
292 Err(TryFromIntError(IntErrorKind::PosOverflow))
293 } else {
294 Ok(u as Self)
295 }
296 }
297 }
298 )*}
299}
300
301macro_rules! impl_try_from_both_bounded {
303 ($source:ty => $($target:ty),+) => {$(
304 #[stable(feature = "try_from", since = "1.34.0")]
305 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
306 impl const TryFrom<$source> for $target {
307 type Error = TryFromIntError;
308
309 #[inline]
313 fn try_from(u: $source) -> Result<Self, Self::Error> {
314 let min = Self::MIN as $source;
315 let max = Self::MAX as $source;
316 if u < min {
317 Err(TryFromIntError(IntErrorKind::NegOverflow))
318 } else if u > max {
319 Err(TryFromIntError(IntErrorKind::PosOverflow))
320 } else {
321 Ok(u as Self)
322 }
323 }
324 }
325 )*}
326}
327
328macro_rules! impl_try_from_integer_for_bool {
330 ($signedness:ident $($int:ty)+) => {$(
331 #[stable(feature = "bool_try_from_int", since = "1.95.0")]
332 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
333 impl const TryFrom<$int> for bool {
334 type Error = TryFromIntError;
335
336 #[doc = concat!("assert_eq!(bool::try_from(0_", stringify!($int), "), Ok(false));")]
343 #[doc = concat!("assert_eq!(bool::try_from(1_", stringify!($int), "), Ok(true));")]
345 #[doc = concat!("assert!(bool::try_from(2_", stringify!($int), ").is_err());")]
347 #[inline]
349 fn try_from(i: $int) -> Result<Self, Self::Error> {
350 sign_dependent_expr!{
351 $signedness ?
352 if signed {
353 match i {
354 0 => Ok(false),
355 1 => Ok(true),
356 ..0 => Err(TryFromIntError(IntErrorKind::NegOverflow)),
357 2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
358 }
359 }
360 if unsigned {
361 match i {
362 0 => Ok(false),
363 1 => Ok(true),
364 2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
365 }
366 }
367 }
368 }
369 }
370 )*}
371}
372
373macro_rules! rev {
374 ($mac:ident, $source:ty => $($target:ty),+) => {$(
375 $mac!($target => $source);
376 )*}
377}
378
379impl_try_from_integer_for_bool!(unsigned u128 u64 u32 u16 u8);
381impl_try_from_integer_for_bool!(signed i128 i64 i32 i16 i8);
382
383impl_try_from_upper_bounded!(u16 => u8);
385impl_try_from_upper_bounded!(u32 => u8, u16);
386impl_try_from_upper_bounded!(u64 => u8, u16, u32);
387impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
388
389impl_try_from_both_bounded!(i16 => i8);
391impl_try_from_both_bounded!(i32 => i8, i16);
392impl_try_from_both_bounded!(i64 => i8, i16, i32);
393impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
394
395impl_try_from_upper_bounded!(u8 => i8);
397impl_try_from_upper_bounded!(u16 => i8, i16);
398impl_try_from_upper_bounded!(u32 => i8, i16, i32);
399impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
400impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
401
402impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
404impl_try_from_both_bounded!(i16 => u8);
405impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
406impl_try_from_both_bounded!(i32 => u8, u16);
407impl_try_from_lower_bounded!(i32 => u32, u64, u128);
408impl_try_from_both_bounded!(i64 => u8, u16, u32);
409impl_try_from_lower_bounded!(i64 => u64, u128);
410impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
411impl_try_from_lower_bounded!(i128 => u128);
412
413impl_try_from_upper_bounded!(usize => isize);
415impl_try_from_lower_bounded!(isize => usize);
416
417#[cfg(target_pointer_width = "16")]
418mod ptr_try_from_impls {
419 use super::{IntErrorKind, TryFromIntError};
420
421 impl_try_from_upper_bounded!(usize => u8);
422 impl_try_from_unbounded!(usize => u16, u32, u64, u128);
423 impl_try_from_upper_bounded!(usize => i8, i16);
424 impl_try_from_unbounded!(usize => i32, i64, i128);
425
426 impl_try_from_both_bounded!(isize => u8);
427 impl_try_from_lower_bounded!(isize => u16, u32, u64, u128);
428 impl_try_from_both_bounded!(isize => i8);
429 impl_try_from_unbounded!(isize => i16, i32, i64, i128);
430
431 rev!(impl_try_from_upper_bounded, usize => u32, u64, u128);
432 rev!(impl_try_from_lower_bounded, usize => i8, i16);
433 rev!(impl_try_from_both_bounded, usize => i32, i64, i128);
434
435 rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128);
436 rev!(impl_try_from_both_bounded, isize => i32, i64, i128);
437}
438
439#[cfg(target_pointer_width = "32")]
440mod ptr_try_from_impls {
441 use super::{IntErrorKind, TryFromIntError};
442
443 impl_try_from_upper_bounded!(usize => u8, u16);
444 impl_try_from_unbounded!(usize => u32, u64, u128);
445 impl_try_from_upper_bounded!(usize => i8, i16, i32);
446 impl_try_from_unbounded!(usize => i64, i128);
447
448 impl_try_from_both_bounded!(isize => u8, u16);
449 impl_try_from_lower_bounded!(isize => u32, u64, u128);
450 impl_try_from_both_bounded!(isize => i8, i16);
451 impl_try_from_unbounded!(isize => i32, i64, i128);
452
453 rev!(impl_try_from_unbounded, usize => u32);
454 rev!(impl_try_from_upper_bounded, usize => u64, u128);
455 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32);
456 rev!(impl_try_from_both_bounded, usize => i64, i128);
457
458 rev!(impl_try_from_unbounded, isize => u16);
459 rev!(impl_try_from_upper_bounded, isize => u32, u64, u128);
460 rev!(impl_try_from_unbounded, isize => i32);
461 rev!(impl_try_from_both_bounded, isize => i64, i128);
462}
463
464#[cfg(target_pointer_width = "64")]
465mod ptr_try_from_impls {
466 use super::{IntErrorKind, TryFromIntError};
467
468 impl_try_from_upper_bounded!(usize => u8, u16, u32);
469 impl_try_from_unbounded!(usize => u64, u128);
470 impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
471 impl_try_from_unbounded!(usize => i128);
472
473 impl_try_from_both_bounded!(isize => u8, u16, u32);
474 impl_try_from_lower_bounded!(isize => u64, u128);
475 impl_try_from_both_bounded!(isize => i8, i16, i32);
476 impl_try_from_unbounded!(isize => i64, i128);
477
478 rev!(impl_try_from_unbounded, usize => u32, u64);
479 rev!(impl_try_from_upper_bounded, usize => u128);
480 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
481 rev!(impl_try_from_both_bounded, usize => i128);
482
483 rev!(impl_try_from_unbounded, isize => u16, u32);
484 rev!(impl_try_from_upper_bounded, isize => u64, u128);
485 rev!(impl_try_from_unbounded, isize => i32, i64);
486 rev!(impl_try_from_both_bounded, isize => i128);
487}
488
489use crate::num::NonZero;
491
492macro_rules! impl_nonzero_int_from_nonzero_int {
493 ($Small:ty => $Large:ty) => {
494 #[stable(feature = "nz_int_conv", since = "1.41.0")]
495 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
496 const impl From<NonZero<$Small>> for NonZero<$Large> {
497 #[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
500 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Large), "]></code> losslessly.")]
501 #[inline]
502 fn from(small: NonZero<$Small>) -> Self {
503 unsafe { Self::new_unchecked(From::from(small.get())) }
505 }
506 }
507 };
508}
509
510impl_nonzero_int_from_nonzero_int!(u8 => u16);
512impl_nonzero_int_from_nonzero_int!(u8 => u32);
513impl_nonzero_int_from_nonzero_int!(u8 => u64);
514impl_nonzero_int_from_nonzero_int!(u8 => u128);
515impl_nonzero_int_from_nonzero_int!(u8 => usize);
516impl_nonzero_int_from_nonzero_int!(u16 => u32);
517impl_nonzero_int_from_nonzero_int!(u16 => u64);
518impl_nonzero_int_from_nonzero_int!(u16 => u128);
519impl_nonzero_int_from_nonzero_int!(u16 => usize);
520impl_nonzero_int_from_nonzero_int!(u32 => u64);
521impl_nonzero_int_from_nonzero_int!(u32 => u128);
522impl_nonzero_int_from_nonzero_int!(u64 => u128);
523
524impl_nonzero_int_from_nonzero_int!(i8 => i16);
526impl_nonzero_int_from_nonzero_int!(i8 => i32);
527impl_nonzero_int_from_nonzero_int!(i8 => i64);
528impl_nonzero_int_from_nonzero_int!(i8 => i128);
529impl_nonzero_int_from_nonzero_int!(i8 => isize);
530impl_nonzero_int_from_nonzero_int!(i16 => i32);
531impl_nonzero_int_from_nonzero_int!(i16 => i64);
532impl_nonzero_int_from_nonzero_int!(i16 => i128);
533impl_nonzero_int_from_nonzero_int!(i16 => isize);
534impl_nonzero_int_from_nonzero_int!(i32 => i64);
535impl_nonzero_int_from_nonzero_int!(i32 => i128);
536impl_nonzero_int_from_nonzero_int!(i64 => i128);
537
538impl_nonzero_int_from_nonzero_int!(u8 => i16);
540impl_nonzero_int_from_nonzero_int!(u8 => i32);
541impl_nonzero_int_from_nonzero_int!(u8 => i64);
542impl_nonzero_int_from_nonzero_int!(u8 => i128);
543impl_nonzero_int_from_nonzero_int!(u8 => isize);
544impl_nonzero_int_from_nonzero_int!(u16 => i32);
545impl_nonzero_int_from_nonzero_int!(u16 => i64);
546impl_nonzero_int_from_nonzero_int!(u16 => i128);
547impl_nonzero_int_from_nonzero_int!(u32 => i64);
548impl_nonzero_int_from_nonzero_int!(u32 => i128);
549impl_nonzero_int_from_nonzero_int!(u64 => i128);
550
551macro_rules! impl_nonzero_int_try_from_int {
552 ($Int:ty) => {
553 #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
554 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
555 const impl TryFrom<$Int> for NonZero<$Int> {
556 type Error = TryFromIntError;
557
558 #[doc = concat!("Attempts to convert [`", stringify!($Int), "`] ")]
561 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Int), "]></code>.")]
562 #[inline]
563 fn try_from(value: $Int) -> Result<Self, Self::Error> {
564 Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
565 }
566 }
567 };
568}
569
570impl_nonzero_int_try_from_int!(u8);
572impl_nonzero_int_try_from_int!(u16);
573impl_nonzero_int_try_from_int!(u32);
574impl_nonzero_int_try_from_int!(u64);
575impl_nonzero_int_try_from_int!(u128);
576impl_nonzero_int_try_from_int!(usize);
577impl_nonzero_int_try_from_int!(i8);
578impl_nonzero_int_try_from_int!(i16);
579impl_nonzero_int_try_from_int!(i32);
580impl_nonzero_int_try_from_int!(i64);
581impl_nonzero_int_try_from_int!(i128);
582impl_nonzero_int_try_from_int!(isize);
583
584macro_rules! impl_nonzero_int_try_from_nonzero_int {
585 ($source:ty => $($target:ty),+) => {$(
586 #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
587 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
588 impl const TryFrom<NonZero<$source>> for NonZero<$target> {
589 type Error = TryFromIntError;
590
591 #[doc = concat!("Attempts to convert <code>[NonZero]\\<[", stringify!($source), "]></code> ")]
594 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($target), "]></code>.")]
595 #[inline]
596 fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> {
597 Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
599 }
600 }
601 )*};
602}
603
604impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
606impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
607impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
608impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
609impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
610
611impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
613impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
614impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
615impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
616impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
617
618impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
620impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
621impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
622impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
623impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
624impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
625
626impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
628impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
629impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
630impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
631impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
632impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);