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 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
180impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
181impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
182impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
183impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
184
185macro_rules! impl_float_from_bool {
186 (
187 $float:ty $(;
188 doctest_prefix: $(#[doc = $doctest_prefix:literal])*
189 doctest_suffix: $(#[doc = $doctest_suffix:literal])*
190 )?
191 ) => {
192 #[stable(feature = "float_from_bool", since = "1.68.0")]
193 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
194 impl const From<bool> for $float {
195 #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
196 $($(#[doc = $doctest_prefix])*)?
201 #[doc = concat!("let x = ", stringify!($float), "::from(false);")]
202 #[doc = concat!("let y = ", stringify!($float), "::from(true);")]
206 $($(#[doc = $doctest_suffix])*)?
208 #[inline]
210 fn from(small: bool) -> Self {
211 small as u8 as Self
212 }
213 }
214 };
215}
216
217impl_float_from_bool!(
219 f16;
220 doctest_prefix:
221 doctest_suffix:
227 );
229impl_float_from_bool!(f32);
230impl_float_from_bool!(f64);
231impl_float_from_bool!(
232 f128;
233 doctest_prefix:
234 doctest_suffix:
239 );
241
242macro_rules! impl_try_from_unbounded {
244 ($source:ty => $($target:ty),+) => {$(
245 #[stable(feature = "try_from", since = "1.34.0")]
246 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
247 impl const TryFrom<$source> for $target {
248 type Error = TryFromIntError;
249
250 #[inline]
254 #[ferrocene::prevalidated]
255 fn try_from(value: $source) -> Result<Self, Self::Error> {
256 Ok(value as Self)
257 }
258 }
259 )*}
260}
261
262macro_rules! impl_try_from_lower_bounded {
264 ($source:ty => $($target:ty),+) => {$(
265 #[stable(feature = "try_from", since = "1.34.0")]
266 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
267 impl const TryFrom<$source> for $target {
268 type Error = TryFromIntError;
269
270 #[inline]
274 #[ferrocene::prevalidated]
275 fn try_from(u: $source) -> Result<Self, Self::Error> {
276 if u >= 0 {
277 Ok(u as Self)
278 } else {
279 Err(TryFromIntError(()))
280 }
281 }
282 }
283 )*}
284}
285
286macro_rules! impl_try_from_upper_bounded {
288 ($source:ty => $($target:ty),+) => {$(
289 #[stable(feature = "try_from", since = "1.34.0")]
290 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
291 impl const TryFrom<$source> for $target {
292 type Error = TryFromIntError;
293
294 #[inline]
298 #[ferrocene::prevalidated]
299 fn try_from(u: $source) -> Result<Self, Self::Error> {
300 if u > (Self::MAX as $source) {
301 Err(TryFromIntError(()))
302 } else {
303 Ok(u as Self)
304 }
305 }
306 }
307 )*}
308}
309
310macro_rules! impl_try_from_both_bounded {
312 ($source:ty => $($target:ty),+) => {$(
313 #[stable(feature = "try_from", since = "1.34.0")]
314 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
315 impl const TryFrom<$source> for $target {
316 type Error = TryFromIntError;
317
318 #[inline]
322 #[ferrocene::prevalidated]
323 fn try_from(u: $source) -> Result<Self, Self::Error> {
324 let min = Self::MIN as $source;
325 let max = Self::MAX as $source;
326 if u < min || u > max {
327 Err(TryFromIntError(()))
328 } else {
329 Ok(u as Self)
330 }
331 }
332 }
333 )*}
334}
335
336macro_rules! impl_try_from_integer_for_bool {
338 ($($int:ty)+) => {$(
339 #[stable(feature = "try_from", since = "1.34.0")]
340 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
341 impl const TryFrom<$int> for bool {
342 type Error = TryFromIntError;
343
344 #[doc = concat!("assert_eq!(bool::try_from(0_", stringify!($int), "), Ok(false));")]
351 #[doc = concat!("assert_eq!(bool::try_from(1_", stringify!($int), "), Ok(true));")]
353 #[doc = concat!("assert!(bool::try_from(2_", stringify!($int), ").is_err());")]
355 #[inline]
357 #[ferrocene::prevalidated]
358 fn try_from(i: $int) -> Result<Self, Self::Error> {
359 match i {
360 0 => Ok(false),
361 1 => Ok(true),
362 _ => Err(TryFromIntError(())),
363 }
364 }
365 }
366 )*}
367}
368
369macro_rules! rev {
370 ($mac:ident, $source:ty => $($target:ty),+) => {$(
371 $mac!($target => $source);
372 )*}
373}
374
375impl_try_from_integer_for_bool!(u128 u64 u32 u16 u8);
377impl_try_from_integer_for_bool!(i128 i64 i32 i16 i8);
378
379impl_try_from_upper_bounded!(u16 => u8);
381impl_try_from_upper_bounded!(u32 => u8, u16);
382impl_try_from_upper_bounded!(u64 => u8, u16, u32);
383impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
384
385impl_try_from_both_bounded!(i16 => i8);
387impl_try_from_both_bounded!(i32 => i8, i16);
388impl_try_from_both_bounded!(i64 => i8, i16, i32);
389impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
390
391impl_try_from_upper_bounded!(u8 => i8);
393impl_try_from_upper_bounded!(u16 => i8, i16);
394impl_try_from_upper_bounded!(u32 => i8, i16, i32);
395impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
396impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
397
398impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
400impl_try_from_both_bounded!(i16 => u8);
401impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
402impl_try_from_both_bounded!(i32 => u8, u16);
403impl_try_from_lower_bounded!(i32 => u32, u64, u128);
404impl_try_from_both_bounded!(i64 => u8, u16, u32);
405impl_try_from_lower_bounded!(i64 => u64, u128);
406impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
407impl_try_from_lower_bounded!(i128 => u128);
408
409impl_try_from_upper_bounded!(usize => isize);
411impl_try_from_lower_bounded!(isize => usize);
412
413#[cfg(target_pointer_width = "16")]
414mod ptr_try_from_impls {
415 use super::TryFromIntError;
416
417 impl_try_from_upper_bounded!(usize => u8);
418 impl_try_from_unbounded!(usize => u16, u32, u64, u128);
419 impl_try_from_upper_bounded!(usize => i8, i16);
420 impl_try_from_unbounded!(usize => i32, i64, i128);
421
422 impl_try_from_both_bounded!(isize => u8);
423 impl_try_from_lower_bounded!(isize => u16, u32, u64, u128);
424 impl_try_from_both_bounded!(isize => i8);
425 impl_try_from_unbounded!(isize => i16, i32, i64, i128);
426
427 rev!(impl_try_from_upper_bounded, usize => u32, u64, u128);
428 rev!(impl_try_from_lower_bounded, usize => i8, i16);
429 rev!(impl_try_from_both_bounded, usize => i32, i64, i128);
430
431 rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128);
432 rev!(impl_try_from_both_bounded, isize => i32, i64, i128);
433}
434
435#[cfg(target_pointer_width = "32")]
436mod ptr_try_from_impls {
437 use super::TryFromIntError;
438
439 impl_try_from_upper_bounded!(usize => u8, u16);
440 impl_try_from_unbounded!(usize => u32, u64, u128);
441 impl_try_from_upper_bounded!(usize => i8, i16, i32);
442 impl_try_from_unbounded!(usize => i64, i128);
443
444 impl_try_from_both_bounded!(isize => u8, u16);
445 impl_try_from_lower_bounded!(isize => u32, u64, u128);
446 impl_try_from_both_bounded!(isize => i8, i16);
447 impl_try_from_unbounded!(isize => i32, i64, i128);
448
449 rev!(impl_try_from_unbounded, usize => u32);
450 rev!(impl_try_from_upper_bounded, usize => u64, u128);
451 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32);
452 rev!(impl_try_from_both_bounded, usize => i64, i128);
453
454 rev!(impl_try_from_unbounded, isize => u16);
455 rev!(impl_try_from_upper_bounded, isize => u32, u64, u128);
456 rev!(impl_try_from_unbounded, isize => i32);
457 rev!(impl_try_from_both_bounded, isize => i64, i128);
458}
459
460#[cfg(target_pointer_width = "64")]
461mod ptr_try_from_impls {
462 use super::TryFromIntError;
463
464 impl_try_from_upper_bounded!(usize => u8, u16, u32);
465 impl_try_from_unbounded!(usize => u64, u128);
466 impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
467 impl_try_from_unbounded!(usize => i128);
468
469 impl_try_from_both_bounded!(isize => u8, u16, u32);
470 impl_try_from_lower_bounded!(isize => u64, u128);
471 impl_try_from_both_bounded!(isize => i8, i16, i32);
472 impl_try_from_unbounded!(isize => i64, i128);
473
474 rev!(impl_try_from_unbounded, usize => u32, u64);
475 rev!(impl_try_from_upper_bounded, usize => u128);
476 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
477 rev!(impl_try_from_both_bounded, usize => i128);
478
479 rev!(impl_try_from_unbounded, isize => u16, u32);
480 rev!(impl_try_from_upper_bounded, isize => u64, u128);
481 rev!(impl_try_from_unbounded, isize => i32, i64);
482 rev!(impl_try_from_both_bounded, isize => i128);
483}
484
485use crate::num::NonZero;
487
488macro_rules! impl_nonzero_int_from_nonzero_int {
489 ($Small:ty => $Large:ty) => {
490 #[stable(feature = "nz_int_conv", since = "1.41.0")]
491 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
492 impl const From<NonZero<$Small>> for NonZero<$Large> {
493 #[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
496 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Large), "]></code> losslessly.")]
497 #[inline]
498 fn from(small: NonZero<$Small>) -> Self {
499 unsafe { Self::new_unchecked(From::from(small.get())) }
501 }
502 }
503 };
504}
505
506impl_nonzero_int_from_nonzero_int!(u8 => u16);
508impl_nonzero_int_from_nonzero_int!(u8 => u32);
509impl_nonzero_int_from_nonzero_int!(u8 => u64);
510impl_nonzero_int_from_nonzero_int!(u8 => u128);
511impl_nonzero_int_from_nonzero_int!(u8 => usize);
512impl_nonzero_int_from_nonzero_int!(u16 => u32);
513impl_nonzero_int_from_nonzero_int!(u16 => u64);
514impl_nonzero_int_from_nonzero_int!(u16 => u128);
515impl_nonzero_int_from_nonzero_int!(u16 => usize);
516impl_nonzero_int_from_nonzero_int!(u32 => u64);
517impl_nonzero_int_from_nonzero_int!(u32 => u128);
518impl_nonzero_int_from_nonzero_int!(u64 => u128);
519
520impl_nonzero_int_from_nonzero_int!(i8 => i16);
522impl_nonzero_int_from_nonzero_int!(i8 => i32);
523impl_nonzero_int_from_nonzero_int!(i8 => i64);
524impl_nonzero_int_from_nonzero_int!(i8 => i128);
525impl_nonzero_int_from_nonzero_int!(i8 => isize);
526impl_nonzero_int_from_nonzero_int!(i16 => i32);
527impl_nonzero_int_from_nonzero_int!(i16 => i64);
528impl_nonzero_int_from_nonzero_int!(i16 => i128);
529impl_nonzero_int_from_nonzero_int!(i16 => isize);
530impl_nonzero_int_from_nonzero_int!(i32 => i64);
531impl_nonzero_int_from_nonzero_int!(i32 => i128);
532impl_nonzero_int_from_nonzero_int!(i64 => i128);
533
534impl_nonzero_int_from_nonzero_int!(u8 => i16);
536impl_nonzero_int_from_nonzero_int!(u8 => i32);
537impl_nonzero_int_from_nonzero_int!(u8 => i64);
538impl_nonzero_int_from_nonzero_int!(u8 => i128);
539impl_nonzero_int_from_nonzero_int!(u8 => isize);
540impl_nonzero_int_from_nonzero_int!(u16 => i32);
541impl_nonzero_int_from_nonzero_int!(u16 => i64);
542impl_nonzero_int_from_nonzero_int!(u16 => i128);
543impl_nonzero_int_from_nonzero_int!(u32 => i64);
544impl_nonzero_int_from_nonzero_int!(u32 => i128);
545impl_nonzero_int_from_nonzero_int!(u64 => i128);
546
547macro_rules! impl_nonzero_int_try_from_int {
548 ($Int:ty) => {
549 #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
550 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
551 impl const TryFrom<$Int> for NonZero<$Int> {
552 type Error = TryFromIntError;
553
554 #[doc = concat!("Attempts to convert [`", stringify!($Int), "`] ")]
557 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Int), "]></code>.")]
558 #[inline]
559 fn try_from(value: $Int) -> Result<Self, Self::Error> {
560 Self::new(value).ok_or(TryFromIntError(()))
561 }
562 }
563 };
564}
565
566impl_nonzero_int_try_from_int!(u8);
568impl_nonzero_int_try_from_int!(u16);
569impl_nonzero_int_try_from_int!(u32);
570impl_nonzero_int_try_from_int!(u64);
571impl_nonzero_int_try_from_int!(u128);
572impl_nonzero_int_try_from_int!(usize);
573impl_nonzero_int_try_from_int!(i8);
574impl_nonzero_int_try_from_int!(i16);
575impl_nonzero_int_try_from_int!(i32);
576impl_nonzero_int_try_from_int!(i64);
577impl_nonzero_int_try_from_int!(i128);
578impl_nonzero_int_try_from_int!(isize);
579
580macro_rules! impl_nonzero_int_try_from_nonzero_int {
581 ($source:ty => $($target:ty),+) => {$(
582 #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
583 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
584 impl const TryFrom<NonZero<$source>> for NonZero<$target> {
585 type Error = TryFromIntError;
586
587 #[doc = concat!("Attempts to convert <code>[NonZero]\\<[", stringify!($source), "]></code> ")]
590 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($target), "]></code>.")]
591 #[inline]
592 fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> {
593 Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
595 }
596 }
597 )*};
598}
599
600impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
602impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
603impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
604impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
605impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
606
607impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
609impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
610impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
611impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
612impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
613
614impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
616impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
617impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
618impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
619impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
620impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
621
622impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
624impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
625impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
626impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
627impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
628impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);