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 #[ferrocene::prevalidated]
51 fn from(b: bool) -> Self {
52 b as Self
53 }
54 }
55 )*}
56}
57
58impl_from_bool!(u8 u16 u32 u64 u128 usize);
60impl_from_bool!(i8 i16 i32 i64 i128 isize);
61
62macro_rules! impl_from {
64 ($small:ty => $large:ty, $(#[$attrs:meta]),+) => {
65 $(#[$attrs])+
66 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
67 impl const From<$small> for $large {
68 #[doc = concat!("Converts from [`", stringify!($small), "`] to [`", stringify!($large), "`] losslessly.")]
69 #[inline(always)]
70 #[ferrocene::prevalidated]
71 fn from(small: $small) -> Self {
72 debug_assert!(<$large>::MIN as i128 <= <$small>::MIN as i128);
73 debug_assert!(<$small>::MAX as u128 <= <$large>::MAX as u128);
74 small as Self
75 }
76 }
77 }
78}
79
80impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
82impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
83impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
84impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
85impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
86impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
87impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
88impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
89impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
90impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
91impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
92
93impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
95impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
96impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
97impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
98impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
99impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
100impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
101impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
102impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
103impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
104impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
105
106impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
108impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
109impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
110impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
111impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
112impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
113impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
114impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
115impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
116impl_from!(u64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
117
118impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
122impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
123impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
124
125impl_from!(i8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
144impl_from!(i8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
145impl_from!(i8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
146impl_from!(i8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
147impl_from!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
148impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
149impl_from!(i16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
150impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
151impl_from!(i32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
152impl_from!(i64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
153
154impl_from!(u8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
156impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158impl_from!(u8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
160impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
161impl_from!(u16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
162impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
163impl_from!(u32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
164impl_from!(u64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
165
166impl_from!(f16 => f32, #[unstable(feature = "f32_from_f16", issue = "154005")], #[unstable_feature_bound(f32_from_f16)]);
175impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
176impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
177impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
178impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
179impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
180
181macro_rules! impl_float_from_bool {
182 (
183 $float:ty $(;
184 doctest_prefix: $(#[doc = $doctest_prefix:literal])*
185 doctest_suffix: $(#[doc = $doctest_suffix:literal])*
186 )?
187 ) => {
188 #[stable(feature = "float_from_bool", since = "1.68.0")]
189 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
190 impl const From<bool> for $float {
191 #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
192 $($(#[doc = $doctest_prefix])*)?
197 #[doc = concat!("let x = ", stringify!($float), "::from(false);")]
198 #[doc = concat!("let y = ", stringify!($float), "::from(true);")]
202 $($(#[doc = $doctest_suffix])*)?
204 #[inline]
206 fn from(small: bool) -> Self {
207 small as u8 as Self
208 }
209 }
210 };
211}
212
213impl_float_from_bool!(
215 f16;
216 doctest_prefix:
217 doctest_suffix:
223 );
225impl_float_from_bool!(f32);
226impl_float_from_bool!(f64);
227impl_float_from_bool!(
228 f128;
229 doctest_prefix:
230 doctest_suffix:
235 );
237
238macro_rules! impl_try_from_unbounded {
240 ($source:ty => $($target:ty),+) => {$(
241 #[stable(feature = "try_from", since = "1.34.0")]
242 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
243 impl const TryFrom<$source> for $target {
244 type Error = TryFromIntError;
245
246 #[inline]
250 #[ferrocene::prevalidated]
251 fn try_from(value: $source) -> Result<Self, Self::Error> {
252 Ok(value as Self)
253 }
254 }
255 )*}
256}
257
258macro_rules! impl_try_from_lower_bounded {
260 ($source:ty => $($target:ty),+) => {$(
261 #[stable(feature = "try_from", since = "1.34.0")]
262 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
263 impl const TryFrom<$source> for $target {
264 type Error = TryFromIntError;
265
266 #[inline]
270 #[ferrocene::prevalidated]
271 fn try_from(u: $source) -> Result<Self, Self::Error> {
272 if u >= 0 {
273 Ok(u as Self)
274 } else {
275 Err(TryFromIntError(IntErrorKind::NegOverflow))
276 }
277 }
278 }
279 )*}
280}
281
282macro_rules! impl_try_from_upper_bounded {
284 ($source:ty => $($target:ty),+) => {$(
285 #[stable(feature = "try_from", since = "1.34.0")]
286 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
287 impl const TryFrom<$source> for $target {
288 type Error = TryFromIntError;
289
290 #[inline]
294 #[ferrocene::prevalidated]
295 fn try_from(u: $source) -> Result<Self, Self::Error> {
296 if u > (Self::MAX as $source) {
297 Err(TryFromIntError(IntErrorKind::PosOverflow))
298 } else {
299 Ok(u as Self)
300 }
301 }
302 }
303 )*}
304}
305
306macro_rules! impl_try_from_both_bounded {
308 ($source:ty => $($target:ty),+) => {$(
309 #[stable(feature = "try_from", since = "1.34.0")]
310 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
311 impl const TryFrom<$source> for $target {
312 type Error = TryFromIntError;
313
314 #[inline]
318 #[ferrocene::prevalidated]
319 fn try_from(u: $source) -> Result<Self, Self::Error> {
320 let min = Self::MIN as $source;
321 let max = Self::MAX as $source;
322 if u < min {
323 Err(TryFromIntError(IntErrorKind::NegOverflow))
324 } else if u > max {
325 Err(TryFromIntError(IntErrorKind::PosOverflow))
326 } else {
327 Ok(u as Self)
328 }
329 }
330 }
331 )*}
332}
333
334macro_rules! impl_try_from_integer_for_bool {
336 ($signedness:ident $($int:ty)+) => {$(
337 #[stable(feature = "bool_try_from_int", since = "1.95.0")]
338 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
339 impl const TryFrom<$int> for bool {
340 type Error = TryFromIntError;
341
342 #[doc = concat!("assert_eq!(bool::try_from(0_", stringify!($int), "), Ok(false));")]
349 #[doc = concat!("assert_eq!(bool::try_from(1_", stringify!($int), "), Ok(true));")]
351 #[doc = concat!("assert!(bool::try_from(2_", stringify!($int), ").is_err());")]
353 #[inline]
355 #[ferrocene::prevalidated]
356 fn try_from(i: $int) -> Result<Self, Self::Error> {
357 sign_dependent_expr!{
358 $signedness ?
359 if signed {
360 match i {
361 0 => Ok(false),
362 1 => Ok(true),
363 ..0 => Err(TryFromIntError(IntErrorKind::NegOverflow)),
364 2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
365 }
366 }
367 if unsigned {
368 match i {
369 0 => Ok(false),
370 1 => Ok(true),
371 2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
372 }
373 }
374 }
375 }
376 }
377 )*}
378}
379
380macro_rules! rev {
381 ($mac:ident, $source:ty => $($target:ty),+) => {$(
382 $mac!($target => $source);
383 )*}
384}
385
386impl_try_from_integer_for_bool!(unsigned u128 u64 u32 u16 u8);
388impl_try_from_integer_for_bool!(signed i128 i64 i32 i16 i8);
389
390impl_try_from_upper_bounded!(u16 => u8);
392impl_try_from_upper_bounded!(u32 => u8, u16);
393impl_try_from_upper_bounded!(u64 => u8, u16, u32);
394impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
395
396impl_try_from_both_bounded!(i16 => i8);
398impl_try_from_both_bounded!(i32 => i8, i16);
399impl_try_from_both_bounded!(i64 => i8, i16, i32);
400impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
401
402impl_try_from_upper_bounded!(u8 => i8);
404impl_try_from_upper_bounded!(u16 => i8, i16);
405impl_try_from_upper_bounded!(u32 => i8, i16, i32);
406impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
407impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
408
409impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
411impl_try_from_both_bounded!(i16 => u8);
412impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
413impl_try_from_both_bounded!(i32 => u8, u16);
414impl_try_from_lower_bounded!(i32 => u32, u64, u128);
415impl_try_from_both_bounded!(i64 => u8, u16, u32);
416impl_try_from_lower_bounded!(i64 => u64, u128);
417impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
418impl_try_from_lower_bounded!(i128 => u128);
419
420impl_try_from_upper_bounded!(usize => isize);
422impl_try_from_lower_bounded!(isize => usize);
423
424#[cfg(target_pointer_width = "16")]
425mod ptr_try_from_impls {
426 use super::{IntErrorKind, TryFromIntError};
427
428 impl_try_from_upper_bounded!(usize => u8);
429 impl_try_from_unbounded!(usize => u16, u32, u64, u128);
430 impl_try_from_upper_bounded!(usize => i8, i16);
431 impl_try_from_unbounded!(usize => i32, i64, i128);
432
433 impl_try_from_both_bounded!(isize => u8);
434 impl_try_from_lower_bounded!(isize => u16, u32, u64, u128);
435 impl_try_from_both_bounded!(isize => i8);
436 impl_try_from_unbounded!(isize => i16, i32, i64, i128);
437
438 rev!(impl_try_from_upper_bounded, usize => u32, u64, u128);
439 rev!(impl_try_from_lower_bounded, usize => i8, i16);
440 rev!(impl_try_from_both_bounded, usize => i32, i64, i128);
441
442 rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128);
443 rev!(impl_try_from_both_bounded, isize => i32, i64, i128);
444}
445
446#[cfg(target_pointer_width = "32")]
447mod ptr_try_from_impls {
448 use super::{IntErrorKind, TryFromIntError};
449
450 impl_try_from_upper_bounded!(usize => u8, u16);
451 impl_try_from_unbounded!(usize => u32, u64, u128);
452 impl_try_from_upper_bounded!(usize => i8, i16, i32);
453 impl_try_from_unbounded!(usize => i64, i128);
454
455 impl_try_from_both_bounded!(isize => u8, u16);
456 impl_try_from_lower_bounded!(isize => u32, u64, u128);
457 impl_try_from_both_bounded!(isize => i8, i16);
458 impl_try_from_unbounded!(isize => i32, i64, i128);
459
460 rev!(impl_try_from_unbounded, usize => u32);
461 rev!(impl_try_from_upper_bounded, usize => u64, u128);
462 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32);
463 rev!(impl_try_from_both_bounded, usize => i64, i128);
464
465 rev!(impl_try_from_unbounded, isize => u16);
466 rev!(impl_try_from_upper_bounded, isize => u32, u64, u128);
467 rev!(impl_try_from_unbounded, isize => i32);
468 rev!(impl_try_from_both_bounded, isize => i64, i128);
469}
470
471#[cfg(target_pointer_width = "64")]
472mod ptr_try_from_impls {
473 use super::{IntErrorKind, TryFromIntError};
474
475 impl_try_from_upper_bounded!(usize => u8, u16, u32);
476 impl_try_from_unbounded!(usize => u64, u128);
477 impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
478 impl_try_from_unbounded!(usize => i128);
479
480 impl_try_from_both_bounded!(isize => u8, u16, u32);
481 impl_try_from_lower_bounded!(isize => u64, u128);
482 impl_try_from_both_bounded!(isize => i8, i16, i32);
483 impl_try_from_unbounded!(isize => i64, i128);
484
485 rev!(impl_try_from_unbounded, usize => u32, u64);
486 rev!(impl_try_from_upper_bounded, usize => u128);
487 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
488 rev!(impl_try_from_both_bounded, usize => i128);
489
490 rev!(impl_try_from_unbounded, isize => u16, u32);
491 rev!(impl_try_from_upper_bounded, isize => u64, u128);
492 rev!(impl_try_from_unbounded, isize => i32, i64);
493 rev!(impl_try_from_both_bounded, isize => i128);
494}
495
496use crate::num::NonZero;
498
499macro_rules! impl_nonzero_int_from_nonzero_int {
500 ($Small:ty => $Large:ty) => {
501 #[stable(feature = "nz_int_conv", since = "1.41.0")]
502 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
503 const impl From<NonZero<$Small>> for NonZero<$Large> {
504 #[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
507 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Large), "]></code> losslessly.")]
508 #[inline]
509 fn from(small: NonZero<$Small>) -> Self {
510 unsafe { Self::new_unchecked(From::from(small.get())) }
512 }
513 }
514 };
515}
516
517impl_nonzero_int_from_nonzero_int!(u8 => u16);
519impl_nonzero_int_from_nonzero_int!(u8 => u32);
520impl_nonzero_int_from_nonzero_int!(u8 => u64);
521impl_nonzero_int_from_nonzero_int!(u8 => u128);
522impl_nonzero_int_from_nonzero_int!(u8 => usize);
523impl_nonzero_int_from_nonzero_int!(u16 => u32);
524impl_nonzero_int_from_nonzero_int!(u16 => u64);
525impl_nonzero_int_from_nonzero_int!(u16 => u128);
526impl_nonzero_int_from_nonzero_int!(u16 => usize);
527impl_nonzero_int_from_nonzero_int!(u32 => u64);
528impl_nonzero_int_from_nonzero_int!(u32 => u128);
529impl_nonzero_int_from_nonzero_int!(u64 => u128);
530
531impl_nonzero_int_from_nonzero_int!(i8 => i16);
533impl_nonzero_int_from_nonzero_int!(i8 => i32);
534impl_nonzero_int_from_nonzero_int!(i8 => i64);
535impl_nonzero_int_from_nonzero_int!(i8 => i128);
536impl_nonzero_int_from_nonzero_int!(i8 => isize);
537impl_nonzero_int_from_nonzero_int!(i16 => i32);
538impl_nonzero_int_from_nonzero_int!(i16 => i64);
539impl_nonzero_int_from_nonzero_int!(i16 => i128);
540impl_nonzero_int_from_nonzero_int!(i16 => isize);
541impl_nonzero_int_from_nonzero_int!(i32 => i64);
542impl_nonzero_int_from_nonzero_int!(i32 => i128);
543impl_nonzero_int_from_nonzero_int!(i64 => i128);
544
545impl_nonzero_int_from_nonzero_int!(u8 => i16);
547impl_nonzero_int_from_nonzero_int!(u8 => i32);
548impl_nonzero_int_from_nonzero_int!(u8 => i64);
549impl_nonzero_int_from_nonzero_int!(u8 => i128);
550impl_nonzero_int_from_nonzero_int!(u8 => isize);
551impl_nonzero_int_from_nonzero_int!(u16 => i32);
552impl_nonzero_int_from_nonzero_int!(u16 => i64);
553impl_nonzero_int_from_nonzero_int!(u16 => i128);
554impl_nonzero_int_from_nonzero_int!(u32 => i64);
555impl_nonzero_int_from_nonzero_int!(u32 => i128);
556impl_nonzero_int_from_nonzero_int!(u64 => i128);
557
558macro_rules! impl_nonzero_int_try_from_int {
559 ($Int:ty) => {
560 #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
561 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
562 const impl TryFrom<$Int> for NonZero<$Int> {
563 type Error = TryFromIntError;
564
565 #[doc = concat!("Attempts to convert [`", stringify!($Int), "`] ")]
568 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Int), "]></code>.")]
569 #[inline]
570 fn try_from(value: $Int) -> Result<Self, Self::Error> {
571 Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
572 }
573 }
574 };
575}
576
577impl_nonzero_int_try_from_int!(u8);
579impl_nonzero_int_try_from_int!(u16);
580impl_nonzero_int_try_from_int!(u32);
581impl_nonzero_int_try_from_int!(u64);
582impl_nonzero_int_try_from_int!(u128);
583impl_nonzero_int_try_from_int!(usize);
584impl_nonzero_int_try_from_int!(i8);
585impl_nonzero_int_try_from_int!(i16);
586impl_nonzero_int_try_from_int!(i32);
587impl_nonzero_int_try_from_int!(i64);
588impl_nonzero_int_try_from_int!(i128);
589impl_nonzero_int_try_from_int!(isize);
590
591macro_rules! impl_nonzero_int_try_from_nonzero_int {
592 ($source:ty => $($target:ty),+) => {$(
593 #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
594 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
595 impl const TryFrom<NonZero<$source>> for NonZero<$target> {
596 type Error = TryFromIntError;
597
598 #[doc = concat!("Attempts to convert <code>[NonZero]\\<[", stringify!($source), "]></code> ")]
601 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($target), "]></code>.")]
602 #[inline]
603 fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> {
604 Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
606 }
607 }
608 )*};
609}
610
611impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
613impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
614impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
615impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
616impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
617
618impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
620impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
621impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
622impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
623impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
624
625impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
627impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
628impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
629impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
630impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
631impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
632
633impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
635impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
636impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
637impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
638impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
639impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);