1use crate::num::TryFromIntError;
2
3mod private {
4 #[unstable(feature = "convert_float_to_int", issue = "67057")]
8 pub trait Sealed {}
9}
10
11#[unstable(feature = "convert_float_to_int", issue = "67057")]
14pub trait FloatToInt<Int>: private::Sealed + Sized {
15 #[unstable(feature = "convert_float_to_int", issue = "67057")]
16 #[doc(hidden)]
17 unsafe fn to_int_unchecked(self) -> Int;
18}
19
20macro_rules! impl_float_to_int {
21 ($Float:ty => $($Int:ty),+) => {
22 #[unstable(feature = "convert_float_to_int", issue = "67057")]
23 impl private::Sealed for $Float {}
24 $(
25 #[unstable(feature = "convert_float_to_int", issue = "67057")]
26 impl FloatToInt<$Int> for $Float {
27 #[inline]
28 unsafe fn to_int_unchecked(self) -> $Int {
29 unsafe { crate::intrinsics::float_to_int_unchecked(self) }
31 }
32 }
33 )+
34 }
35}
36
37impl_float_to_int!(f16 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
38impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
39impl_float_to_int!(f64 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
40impl_float_to_int!(f128 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
41
42macro_rules! impl_from_bool {
44 ($($int:ty)*) => {$(
45 #[stable(feature = "from_bool", since = "1.28.0")]
46 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
47 impl const From<bool> for $int {
48 #[doc = concat!("[`", stringify!($int), "`]")]
50 #[doc = concat!("assert_eq!(", stringify!($int), "::from(false), 0);")]
56 #[doc = concat!("assert_eq!(", stringify!($int), "::from(true), 1);")]
58 #[inline(always)]
60 #[ferrocene::prevalidated]
61 fn from(b: bool) -> Self {
62 b as Self
63 }
64 }
65 )*}
66}
67
68impl_from_bool!(u8 u16 u32 u64 u128 usize);
70impl_from_bool!(i8 i16 i32 i64 i128 isize);
71
72macro_rules! impl_from {
74 ($small:ty => $large:ty, $(#[$attrs:meta]),+) => {
75 $(#[$attrs])+
76 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
77 impl const From<$small> for $large {
78 #[doc = concat!("Converts from [`", stringify!($small), "`] to [`", stringify!($large), "`] losslessly.")]
79 #[inline(always)]
80 #[ferrocene::prevalidated]
81 fn from(small: $small) -> Self {
82 debug_assert!(<$large>::MIN as i128 <= <$small>::MIN as i128);
83 debug_assert!(<$small>::MAX as u128 <= <$large>::MAX as u128);
84 small as Self
85 }
86 }
87 }
88}
89
90impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
92impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
93impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
94impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
95impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
96impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
97impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
98impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
99impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
100impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
101impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
102
103impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
105impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
106impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
107impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
108impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
109impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
110impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
111impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
112impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
113impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
114impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
115
116impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
118impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
119impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
120impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
121impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
122impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
123impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
124impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
125impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
126impl_from!(u64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
127
128impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
132impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
133impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
134
135impl_from!(i8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
154impl_from!(i8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
155impl_from!(i8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
156impl_from!(i8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157impl_from!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159impl_from!(i16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
160impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
161impl_from!(i32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
162impl_from!(i64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
163
164impl_from!(u8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
166impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
167impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
168impl_from!(u8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
169impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
170impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
171impl_from!(u16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
172impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
173impl_from!(u32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
174impl_from!(u64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
175
176impl_from!(f16 => f32, #[unstable(feature = "f32_from_f16", issue = "154005")], #[unstable_feature_bound(f32_from_f16)]);
185impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
186impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
187impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
188impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
189impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
190
191macro_rules! impl_float_from_bool {
192 (
193 $float:ty $(;
194 doctest_prefix: $(#[doc = $doctest_prefix:literal])*
195 doctest_suffix: $(#[doc = $doctest_suffix:literal])*
196 )?
197 ) => {
198 #[stable(feature = "float_from_bool", since = "1.68.0")]
199 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
200 impl const From<bool> for $float {
201 #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
202 $($(#[doc = $doctest_prefix])*)?
207 #[doc = concat!("let x = ", stringify!($float), "::from(false);")]
208 #[doc = concat!("let y = ", stringify!($float), "::from(true);")]
212 $($(#[doc = $doctest_suffix])*)?
214 #[inline]
216 fn from(small: bool) -> Self {
217 small as u8 as Self
218 }
219 }
220 };
221}
222
223impl_float_from_bool!(
225 f16;
226 doctest_prefix:
227 doctest_suffix:
233 );
235impl_float_from_bool!(f32);
236impl_float_from_bool!(f64);
237impl_float_from_bool!(
238 f128;
239 doctest_prefix:
240 doctest_suffix:
245 );
247
248macro_rules! impl_try_from_unbounded {
250 ($source:ty => $($target:ty),+) => {$(
251 #[stable(feature = "try_from", since = "1.34.0")]
252 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
253 impl const TryFrom<$source> for $target {
254 type Error = TryFromIntError;
255
256 #[inline]
260 #[ferrocene::prevalidated]
261 fn try_from(value: $source) -> Result<Self, Self::Error> {
262 Ok(value as Self)
263 }
264 }
265 )*}
266}
267
268macro_rules! impl_try_from_lower_bounded {
270 ($source:ty => $($target:ty),+) => {$(
271 #[stable(feature = "try_from", since = "1.34.0")]
272 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
273 impl const TryFrom<$source> for $target {
274 type Error = TryFromIntError;
275
276 #[inline]
280 #[ferrocene::prevalidated]
281 fn try_from(u: $source) -> Result<Self, Self::Error> {
282 if u >= 0 {
283 Ok(u as Self)
284 } else {
285 Err(TryFromIntError(()))
286 }
287 }
288 }
289 )*}
290}
291
292macro_rules! impl_try_from_upper_bounded {
294 ($source:ty => $($target:ty),+) => {$(
295 #[stable(feature = "try_from", since = "1.34.0")]
296 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
297 impl const TryFrom<$source> for $target {
298 type Error = TryFromIntError;
299
300 #[inline]
304 #[ferrocene::prevalidated]
305 fn try_from(u: $source) -> Result<Self, Self::Error> {
306 if u > (Self::MAX as $source) {
307 Err(TryFromIntError(()))
308 } else {
309 Ok(u as Self)
310 }
311 }
312 }
313 )*}
314}
315
316macro_rules! impl_try_from_both_bounded {
318 ($source:ty => $($target:ty),+) => {$(
319 #[stable(feature = "try_from", since = "1.34.0")]
320 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
321 impl const TryFrom<$source> for $target {
322 type Error = TryFromIntError;
323
324 #[inline]
328 #[ferrocene::prevalidated]
329 fn try_from(u: $source) -> Result<Self, Self::Error> {
330 let min = Self::MIN as $source;
331 let max = Self::MAX as $source;
332 if u < min || u > max {
333 Err(TryFromIntError(()))
334 } else {
335 Ok(u as Self)
336 }
337 }
338 }
339 )*}
340}
341
342macro_rules! impl_try_from_integer_for_bool {
344 ($($int:ty)+) => {$(
345 #[stable(feature = "bool_try_from_int", since = "1.95.0")]
346 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
347 impl const TryFrom<$int> for bool {
348 type Error = TryFromIntError;
349
350 #[doc = concat!("assert_eq!(bool::try_from(0_", stringify!($int), "), Ok(false));")]
357 #[doc = concat!("assert_eq!(bool::try_from(1_", stringify!($int), "), Ok(true));")]
359 #[doc = concat!("assert!(bool::try_from(2_", stringify!($int), ").is_err());")]
361 #[inline]
363 #[ferrocene::prevalidated]
364 fn try_from(i: $int) -> Result<Self, Self::Error> {
365 match i {
366 0 => Ok(false),
367 1 => Ok(true),
368 _ => Err(TryFromIntError(())),
369 }
370 }
371 }
372 )*}
373}
374
375macro_rules! rev {
376 ($mac:ident, $source:ty => $($target:ty),+) => {$(
377 $mac!($target => $source);
378 )*}
379}
380
381impl_try_from_integer_for_bool!(u128 u64 u32 u16 u8);
383impl_try_from_integer_for_bool!(i128 i64 i32 i16 i8);
384
385impl_try_from_upper_bounded!(u16 => u8);
387impl_try_from_upper_bounded!(u32 => u8, u16);
388impl_try_from_upper_bounded!(u64 => u8, u16, u32);
389impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
390
391impl_try_from_both_bounded!(i16 => i8);
393impl_try_from_both_bounded!(i32 => i8, i16);
394impl_try_from_both_bounded!(i64 => i8, i16, i32);
395impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
396
397impl_try_from_upper_bounded!(u8 => i8);
399impl_try_from_upper_bounded!(u16 => i8, i16);
400impl_try_from_upper_bounded!(u32 => i8, i16, i32);
401impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
402impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
403
404impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
406impl_try_from_both_bounded!(i16 => u8);
407impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
408impl_try_from_both_bounded!(i32 => u8, u16);
409impl_try_from_lower_bounded!(i32 => u32, u64, u128);
410impl_try_from_both_bounded!(i64 => u8, u16, u32);
411impl_try_from_lower_bounded!(i64 => u64, u128);
412impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
413impl_try_from_lower_bounded!(i128 => u128);
414
415impl_try_from_upper_bounded!(usize => isize);
417impl_try_from_lower_bounded!(isize => usize);
418
419#[cfg(target_pointer_width = "16")]
420mod ptr_try_from_impls {
421 use super::TryFromIntError;
422
423 impl_try_from_upper_bounded!(usize => u8);
424 impl_try_from_unbounded!(usize => u16, u32, u64, u128);
425 impl_try_from_upper_bounded!(usize => i8, i16);
426 impl_try_from_unbounded!(usize => i32, i64, i128);
427
428 impl_try_from_both_bounded!(isize => u8);
429 impl_try_from_lower_bounded!(isize => u16, u32, u64, u128);
430 impl_try_from_both_bounded!(isize => i8);
431 impl_try_from_unbounded!(isize => i16, i32, i64, i128);
432
433 rev!(impl_try_from_upper_bounded, usize => u32, u64, u128);
434 rev!(impl_try_from_lower_bounded, usize => i8, i16);
435 rev!(impl_try_from_both_bounded, usize => i32, i64, i128);
436
437 rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128);
438 rev!(impl_try_from_both_bounded, isize => i32, i64, i128);
439}
440
441#[cfg(target_pointer_width = "32")]
442mod ptr_try_from_impls {
443 use super::TryFromIntError;
444
445 impl_try_from_upper_bounded!(usize => u8, u16);
446 impl_try_from_unbounded!(usize => u32, u64, u128);
447 impl_try_from_upper_bounded!(usize => i8, i16, i32);
448 impl_try_from_unbounded!(usize => i64, i128);
449
450 impl_try_from_both_bounded!(isize => u8, u16);
451 impl_try_from_lower_bounded!(isize => u32, u64, u128);
452 impl_try_from_both_bounded!(isize => i8, i16);
453 impl_try_from_unbounded!(isize => i32, i64, i128);
454
455 rev!(impl_try_from_unbounded, usize => u32);
456 rev!(impl_try_from_upper_bounded, usize => u64, u128);
457 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32);
458 rev!(impl_try_from_both_bounded, usize => i64, i128);
459
460 rev!(impl_try_from_unbounded, isize => u16);
461 rev!(impl_try_from_upper_bounded, isize => u32, u64, u128);
462 rev!(impl_try_from_unbounded, isize => i32);
463 rev!(impl_try_from_both_bounded, isize => i64, i128);
464}
465
466#[cfg(target_pointer_width = "64")]
467mod ptr_try_from_impls {
468 use super::TryFromIntError;
469
470 impl_try_from_upper_bounded!(usize => u8, u16, u32);
471 impl_try_from_unbounded!(usize => u64, u128);
472 impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
473 impl_try_from_unbounded!(usize => i128);
474
475 impl_try_from_both_bounded!(isize => u8, u16, u32);
476 impl_try_from_lower_bounded!(isize => u64, u128);
477 impl_try_from_both_bounded!(isize => i8, i16, i32);
478 impl_try_from_unbounded!(isize => i64, i128);
479
480 rev!(impl_try_from_unbounded, usize => u32, u64);
481 rev!(impl_try_from_upper_bounded, usize => u128);
482 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
483 rev!(impl_try_from_both_bounded, usize => i128);
484
485 rev!(impl_try_from_unbounded, isize => u16, u32);
486 rev!(impl_try_from_upper_bounded, isize => u64, u128);
487 rev!(impl_try_from_unbounded, isize => i32, i64);
488 rev!(impl_try_from_both_bounded, isize => i128);
489}
490
491use crate::num::NonZero;
493
494macro_rules! impl_nonzero_int_from_nonzero_int {
495 ($Small:ty => $Large:ty) => {
496 #[stable(feature = "nz_int_conv", since = "1.41.0")]
497 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
498 impl const From<NonZero<$Small>> for NonZero<$Large> {
499 #[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
502 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Large), "]></code> losslessly.")]
503 #[inline]
504 fn from(small: NonZero<$Small>) -> Self {
505 unsafe { Self::new_unchecked(From::from(small.get())) }
507 }
508 }
509 };
510}
511
512impl_nonzero_int_from_nonzero_int!(u8 => u16);
514impl_nonzero_int_from_nonzero_int!(u8 => u32);
515impl_nonzero_int_from_nonzero_int!(u8 => u64);
516impl_nonzero_int_from_nonzero_int!(u8 => u128);
517impl_nonzero_int_from_nonzero_int!(u8 => usize);
518impl_nonzero_int_from_nonzero_int!(u16 => u32);
519impl_nonzero_int_from_nonzero_int!(u16 => u64);
520impl_nonzero_int_from_nonzero_int!(u16 => u128);
521impl_nonzero_int_from_nonzero_int!(u16 => usize);
522impl_nonzero_int_from_nonzero_int!(u32 => u64);
523impl_nonzero_int_from_nonzero_int!(u32 => u128);
524impl_nonzero_int_from_nonzero_int!(u64 => u128);
525
526impl_nonzero_int_from_nonzero_int!(i8 => i16);
528impl_nonzero_int_from_nonzero_int!(i8 => i32);
529impl_nonzero_int_from_nonzero_int!(i8 => i64);
530impl_nonzero_int_from_nonzero_int!(i8 => i128);
531impl_nonzero_int_from_nonzero_int!(i8 => isize);
532impl_nonzero_int_from_nonzero_int!(i16 => i32);
533impl_nonzero_int_from_nonzero_int!(i16 => i64);
534impl_nonzero_int_from_nonzero_int!(i16 => i128);
535impl_nonzero_int_from_nonzero_int!(i16 => isize);
536impl_nonzero_int_from_nonzero_int!(i32 => i64);
537impl_nonzero_int_from_nonzero_int!(i32 => i128);
538impl_nonzero_int_from_nonzero_int!(i64 => i128);
539
540impl_nonzero_int_from_nonzero_int!(u8 => i16);
542impl_nonzero_int_from_nonzero_int!(u8 => i32);
543impl_nonzero_int_from_nonzero_int!(u8 => i64);
544impl_nonzero_int_from_nonzero_int!(u8 => i128);
545impl_nonzero_int_from_nonzero_int!(u8 => isize);
546impl_nonzero_int_from_nonzero_int!(u16 => i32);
547impl_nonzero_int_from_nonzero_int!(u16 => i64);
548impl_nonzero_int_from_nonzero_int!(u16 => i128);
549impl_nonzero_int_from_nonzero_int!(u32 => i64);
550impl_nonzero_int_from_nonzero_int!(u32 => i128);
551impl_nonzero_int_from_nonzero_int!(u64 => i128);
552
553macro_rules! impl_nonzero_int_try_from_int {
554 ($Int:ty) => {
555 #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
556 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
557 impl const TryFrom<$Int> for NonZero<$Int> {
558 type Error = TryFromIntError;
559
560 #[doc = concat!("Attempts to convert [`", stringify!($Int), "`] ")]
563 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Int), "]></code>.")]
564 #[inline]
565 fn try_from(value: $Int) -> Result<Self, Self::Error> {
566 Self::new(value).ok_or(TryFromIntError(()))
567 }
568 }
569 };
570}
571
572impl_nonzero_int_try_from_int!(u8);
574impl_nonzero_int_try_from_int!(u16);
575impl_nonzero_int_try_from_int!(u32);
576impl_nonzero_int_try_from_int!(u64);
577impl_nonzero_int_try_from_int!(u128);
578impl_nonzero_int_try_from_int!(usize);
579impl_nonzero_int_try_from_int!(i8);
580impl_nonzero_int_try_from_int!(i16);
581impl_nonzero_int_try_from_int!(i32);
582impl_nonzero_int_try_from_int!(i64);
583impl_nonzero_int_try_from_int!(i128);
584impl_nonzero_int_try_from_int!(isize);
585
586macro_rules! impl_nonzero_int_try_from_nonzero_int {
587 ($source:ty => $($target:ty),+) => {$(
588 #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
589 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
590 impl const TryFrom<NonZero<$source>> for NonZero<$target> {
591 type Error = TryFromIntError;
592
593 #[doc = concat!("Attempts to convert <code>[NonZero]\\<[", stringify!($source), "]></code> ")]
596 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($target), "]></code>.")]
597 #[inline]
598 fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> {
599 Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
601 }
602 }
603 )*};
604}
605
606impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
608impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
609impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
610impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
611impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
612
613impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
615impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
616impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
617impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
618impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
619
620impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
622impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
623impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
624impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
625impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
626impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
627
628impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
630impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
631impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
632impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
633impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
634impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);