1use crate::num::{IntErrorKind, 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 fn from(b: bool) -> Self {
61 b as Self
62 }
63 }
64 )*}
65}
66
67impl_from_bool!(u8 u16 u32 u64 u128 usize);
69impl_from_bool!(i8 i16 i32 i64 i128 isize);
70
71macro_rules! impl_from {
73 ($small:ty => $large:ty, $(#[$attrs:meta]),+) => {
74 $(#[$attrs])+
75 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
76 impl const From<$small> for $large {
77 #[doc = concat!("Converts from [`", stringify!($small), "`] to [`", stringify!($large), "`] losslessly.")]
78 #[inline(always)]
79 fn from(small: $small) -> Self {
80 debug_assert!(<$large>::MIN as i128 <= <$small>::MIN as i128);
81 debug_assert!(<$small>::MAX as u128 <= <$large>::MAX as u128);
82 small as Self
83 }
84 }
85 }
86}
87
88impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
90impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
91impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
92impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
93impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
94impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
95impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
96impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
97impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
98impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
99impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
100
101impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
103impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
104impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
105impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
106impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
107impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
108impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
109impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
110impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
111impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
112impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
113
114impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
116impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
117impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
118impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
119impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
120impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
121impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
122impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
123impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
124impl_from!(u64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
125
126impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
130impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
131impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
132
133impl_from!(i8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
152impl_from!(i8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
153impl_from!(i8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
154impl_from!(i8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
155impl_from!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
156impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157impl_from!(i16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159impl_from!(i32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
160impl_from!(i64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
161
162impl_from!(u8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
164impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
165impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
166impl_from!(u8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
167impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
168impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
169impl_from!(u16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
170impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
171impl_from!(u32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
172impl_from!(u64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
173
174impl_from!(f16 => f32, #[unstable(feature = "f32_from_f16", issue = "154005")], #[unstable_feature_bound(f32_from_f16)]);
183impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
184impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
185impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
186impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
187impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
188
189macro_rules! impl_float_from_bool {
190 (
191 $float:ty $(;
192 doctest_prefix: $(#[doc = $doctest_prefix:literal])*
193 doctest_suffix: $(#[doc = $doctest_suffix:literal])*
194 )?
195 ) => {
196 #[stable(feature = "float_from_bool", since = "1.68.0")]
197 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
198 impl const From<bool> for $float {
199 #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
200 $($(#[doc = $doctest_prefix])*)?
205 #[doc = concat!("let x = ", stringify!($float), "::from(false);")]
206 #[doc = concat!("let y = ", stringify!($float), "::from(true);")]
210 $($(#[doc = $doctest_suffix])*)?
212 #[inline]
214 fn from(small: bool) -> Self {
215 small as u8 as Self
216 }
217 }
218 };
219}
220
221impl_float_from_bool!(
223 f16;
224 doctest_prefix:
225 doctest_suffix:
231 );
233impl_float_from_bool!(f32);
234impl_float_from_bool!(f64);
235impl_float_from_bool!(
236 f128;
237 doctest_prefix:
238 doctest_suffix:
243 );
245
246macro_rules! impl_try_from_unbounded {
248 ($source:ty => $($target:ty),+) => {$(
249 #[stable(feature = "try_from", since = "1.34.0")]
250 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
251 impl const TryFrom<$source> for $target {
252 type Error = TryFromIntError;
253
254 #[inline]
258 fn try_from(value: $source) -> Result<Self, Self::Error> {
259 Ok(value as Self)
260 }
261 }
262 )*}
263}
264
265macro_rules! impl_try_from_lower_bounded {
267 ($source:ty => $($target:ty),+) => {$(
268 #[stable(feature = "try_from", since = "1.34.0")]
269 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
270 impl const TryFrom<$source> for $target {
271 type Error = TryFromIntError;
272
273 #[inline]
277 fn try_from(u: $source) -> Result<Self, Self::Error> {
278 if u >= 0 {
279 Ok(u as Self)
280 } else {
281 Err(TryFromIntError(IntErrorKind::NegOverflow))
282 }
283 }
284 }
285 )*}
286}
287
288macro_rules! impl_try_from_upper_bounded {
290 ($source:ty => $($target:ty),+) => {$(
291 #[stable(feature = "try_from", since = "1.34.0")]
292 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
293 impl const TryFrom<$source> for $target {
294 type Error = TryFromIntError;
295
296 #[inline]
300 fn try_from(u: $source) -> Result<Self, Self::Error> {
301 if u > (Self::MAX as $source) {
302 Err(TryFromIntError(IntErrorKind::PosOverflow))
303 } else {
304 Ok(u as Self)
305 }
306 }
307 }
308 )*}
309}
310
311macro_rules! impl_try_from_both_bounded {
313 ($source:ty => $($target:ty),+) => {$(
314 #[stable(feature = "try_from", since = "1.34.0")]
315 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
316 impl const TryFrom<$source> for $target {
317 type Error = TryFromIntError;
318
319 #[inline]
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 {
327 Err(TryFromIntError(IntErrorKind::NegOverflow))
328 } else if u > max {
329 Err(TryFromIntError(IntErrorKind::PosOverflow))
330 } else {
331 Ok(u as Self)
332 }
333 }
334 }
335 )*}
336}
337
338macro_rules! impl_try_from_integer_for_bool {
340 ($signedness:ident $($int:ty)+) => {$(
341 #[stable(feature = "bool_try_from_int", since = "1.95.0")]
342 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
343 impl const TryFrom<$int> for bool {
344 type Error = TryFromIntError;
345
346 #[doc = concat!("assert_eq!(bool::try_from(0_", stringify!($int), "), Ok(false));")]
353 #[doc = concat!("assert_eq!(bool::try_from(1_", stringify!($int), "), Ok(true));")]
355 #[doc = concat!("assert!(bool::try_from(2_", stringify!($int), ").is_err());")]
357 #[inline]
359 fn try_from(i: $int) -> Result<Self, Self::Error> {
360 sign_dependent_expr!{
361 $signedness ?
362 if signed {
363 match i {
364 0 => Ok(false),
365 1 => Ok(true),
366 ..0 => Err(TryFromIntError(IntErrorKind::NegOverflow)),
367 2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
368 }
369 }
370 if unsigned {
371 match i {
372 0 => Ok(false),
373 1 => Ok(true),
374 2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
375 }
376 }
377 }
378 }
379 }
380 )*}
381}
382
383macro_rules! rev {
384 ($mac:ident, $source:ty => $($target:ty),+) => {$(
385 $mac!($target => $source);
386 )*}
387}
388
389impl_try_from_integer_for_bool!(unsigned u128 u64 u32 u16 u8);
391impl_try_from_integer_for_bool!(signed i128 i64 i32 i16 i8);
392
393impl_try_from_upper_bounded!(u16 => u8);
395impl_try_from_upper_bounded!(u32 => u8, u16);
396impl_try_from_upper_bounded!(u64 => u8, u16, u32);
397impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
398
399impl_try_from_both_bounded!(i16 => i8);
401impl_try_from_both_bounded!(i32 => i8, i16);
402impl_try_from_both_bounded!(i64 => i8, i16, i32);
403impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
404
405impl_try_from_upper_bounded!(u8 => i8);
407impl_try_from_upper_bounded!(u16 => i8, i16);
408impl_try_from_upper_bounded!(u32 => i8, i16, i32);
409impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
410impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
411
412impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
414impl_try_from_both_bounded!(i16 => u8);
415impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
416impl_try_from_both_bounded!(i32 => u8, u16);
417impl_try_from_lower_bounded!(i32 => u32, u64, u128);
418impl_try_from_both_bounded!(i64 => u8, u16, u32);
419impl_try_from_lower_bounded!(i64 => u64, u128);
420impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
421impl_try_from_lower_bounded!(i128 => u128);
422
423impl_try_from_upper_bounded!(usize => isize);
425impl_try_from_lower_bounded!(isize => usize);
426
427#[cfg(target_pointer_width = "16")]
428mod ptr_try_from_impls {
429 use super::{IntErrorKind, TryFromIntError};
430
431 impl_try_from_upper_bounded!(usize => u8);
432 impl_try_from_unbounded!(usize => u16, u32, u64, u128);
433 impl_try_from_upper_bounded!(usize => i8, i16);
434 impl_try_from_unbounded!(usize => i32, i64, i128);
435
436 impl_try_from_both_bounded!(isize => u8);
437 impl_try_from_lower_bounded!(isize => u16, u32, u64, u128);
438 impl_try_from_both_bounded!(isize => i8);
439 impl_try_from_unbounded!(isize => i16, i32, i64, i128);
440
441 rev!(impl_try_from_upper_bounded, usize => u32, u64, u128);
442 rev!(impl_try_from_lower_bounded, usize => i8, i16);
443 rev!(impl_try_from_both_bounded, usize => i32, i64, i128);
444
445 rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128);
446 rev!(impl_try_from_both_bounded, isize => i32, i64, i128);
447}
448
449#[cfg(target_pointer_width = "32")]
450mod ptr_try_from_impls {
451 use super::{IntErrorKind, TryFromIntError};
452
453 impl_try_from_upper_bounded!(usize => u8, u16);
454 impl_try_from_unbounded!(usize => u32, u64, u128);
455 impl_try_from_upper_bounded!(usize => i8, i16, i32);
456 impl_try_from_unbounded!(usize => i64, i128);
457
458 impl_try_from_both_bounded!(isize => u8, u16);
459 impl_try_from_lower_bounded!(isize => u32, u64, u128);
460 impl_try_from_both_bounded!(isize => i8, i16);
461 impl_try_from_unbounded!(isize => i32, i64, i128);
462
463 rev!(impl_try_from_unbounded, usize => u32);
464 rev!(impl_try_from_upper_bounded, usize => u64, u128);
465 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32);
466 rev!(impl_try_from_both_bounded, usize => i64, i128);
467
468 rev!(impl_try_from_unbounded, isize => u16);
469 rev!(impl_try_from_upper_bounded, isize => u32, u64, u128);
470 rev!(impl_try_from_unbounded, isize => i32);
471 rev!(impl_try_from_both_bounded, isize => i64, i128);
472}
473
474#[cfg(target_pointer_width = "64")]
475mod ptr_try_from_impls {
476 use super::{IntErrorKind, TryFromIntError};
477
478 impl_try_from_upper_bounded!(usize => u8, u16, u32);
479 impl_try_from_unbounded!(usize => u64, u128);
480 impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
481 impl_try_from_unbounded!(usize => i128);
482
483 impl_try_from_both_bounded!(isize => u8, u16, u32);
484 impl_try_from_lower_bounded!(isize => u64, u128);
485 impl_try_from_both_bounded!(isize => i8, i16, i32);
486 impl_try_from_unbounded!(isize => i64, i128);
487
488 rev!(impl_try_from_unbounded, usize => u32, u64);
489 rev!(impl_try_from_upper_bounded, usize => u128);
490 rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
491 rev!(impl_try_from_both_bounded, usize => i128);
492
493 rev!(impl_try_from_unbounded, isize => u16, u32);
494 rev!(impl_try_from_upper_bounded, isize => u64, u128);
495 rev!(impl_try_from_unbounded, isize => i32, i64);
496 rev!(impl_try_from_both_bounded, isize => i128);
497}
498
499use crate::num::NonZero;
501
502macro_rules! impl_nonzero_int_from_nonzero_int {
503 ($Small:ty => $Large:ty) => {
504 #[stable(feature = "nz_int_conv", since = "1.41.0")]
505 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
506 impl const From<NonZero<$Small>> for NonZero<$Large> {
507 #[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
510 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Large), "]></code> losslessly.")]
511 #[inline]
512 fn from(small: NonZero<$Small>) -> Self {
513 unsafe { Self::new_unchecked(From::from(small.get())) }
515 }
516 }
517 };
518}
519
520impl_nonzero_int_from_nonzero_int!(u8 => u16);
522impl_nonzero_int_from_nonzero_int!(u8 => u32);
523impl_nonzero_int_from_nonzero_int!(u8 => u64);
524impl_nonzero_int_from_nonzero_int!(u8 => u128);
525impl_nonzero_int_from_nonzero_int!(u8 => usize);
526impl_nonzero_int_from_nonzero_int!(u16 => u32);
527impl_nonzero_int_from_nonzero_int!(u16 => u64);
528impl_nonzero_int_from_nonzero_int!(u16 => u128);
529impl_nonzero_int_from_nonzero_int!(u16 => usize);
530impl_nonzero_int_from_nonzero_int!(u32 => u64);
531impl_nonzero_int_from_nonzero_int!(u32 => u128);
532impl_nonzero_int_from_nonzero_int!(u64 => u128);
533
534impl_nonzero_int_from_nonzero_int!(i8 => i16);
536impl_nonzero_int_from_nonzero_int!(i8 => i32);
537impl_nonzero_int_from_nonzero_int!(i8 => i64);
538impl_nonzero_int_from_nonzero_int!(i8 => i128);
539impl_nonzero_int_from_nonzero_int!(i8 => isize);
540impl_nonzero_int_from_nonzero_int!(i16 => i32);
541impl_nonzero_int_from_nonzero_int!(i16 => i64);
542impl_nonzero_int_from_nonzero_int!(i16 => i128);
543impl_nonzero_int_from_nonzero_int!(i16 => isize);
544impl_nonzero_int_from_nonzero_int!(i32 => i64);
545impl_nonzero_int_from_nonzero_int!(i32 => i128);
546impl_nonzero_int_from_nonzero_int!(i64 => i128);
547
548impl_nonzero_int_from_nonzero_int!(u8 => i16);
550impl_nonzero_int_from_nonzero_int!(u8 => i32);
551impl_nonzero_int_from_nonzero_int!(u8 => i64);
552impl_nonzero_int_from_nonzero_int!(u8 => i128);
553impl_nonzero_int_from_nonzero_int!(u8 => isize);
554impl_nonzero_int_from_nonzero_int!(u16 => i32);
555impl_nonzero_int_from_nonzero_int!(u16 => i64);
556impl_nonzero_int_from_nonzero_int!(u16 => i128);
557impl_nonzero_int_from_nonzero_int!(u32 => i64);
558impl_nonzero_int_from_nonzero_int!(u32 => i128);
559impl_nonzero_int_from_nonzero_int!(u64 => i128);
560
561macro_rules! impl_nonzero_int_try_from_int {
562 ($Int:ty) => {
563 #[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
564 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
565 impl const TryFrom<$Int> for NonZero<$Int> {
566 type Error = TryFromIntError;
567
568 #[doc = concat!("Attempts to convert [`", stringify!($Int), "`] ")]
571 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($Int), "]></code>.")]
572 #[inline]
573 fn try_from(value: $Int) -> Result<Self, Self::Error> {
574 Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
575 }
576 }
577 };
578}
579
580impl_nonzero_int_try_from_int!(u8);
582impl_nonzero_int_try_from_int!(u16);
583impl_nonzero_int_try_from_int!(u32);
584impl_nonzero_int_try_from_int!(u64);
585impl_nonzero_int_try_from_int!(u128);
586impl_nonzero_int_try_from_int!(usize);
587impl_nonzero_int_try_from_int!(i8);
588impl_nonzero_int_try_from_int!(i16);
589impl_nonzero_int_try_from_int!(i32);
590impl_nonzero_int_try_from_int!(i64);
591impl_nonzero_int_try_from_int!(i128);
592impl_nonzero_int_try_from_int!(isize);
593
594macro_rules! impl_nonzero_int_try_from_nonzero_int {
595 ($source:ty => $($target:ty),+) => {$(
596 #[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
597 #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
598 impl const TryFrom<NonZero<$source>> for NonZero<$target> {
599 type Error = TryFromIntError;
600
601 #[doc = concat!("Attempts to convert <code>[NonZero]\\<[", stringify!($source), "]></code> ")]
604 #[doc = concat!("to <code>[NonZero]\\<[", stringify!($target), "]></code>.")]
605 #[inline]
606 fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> {
607 Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
609 }
610 }
611 )*};
612}
613
614impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
616impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
617impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
618impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
619impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
620
621impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
623impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
624impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
625impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
626impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
627
628impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
630impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
631impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
632impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
633impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
634impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
635
636impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
638impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
639impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
640impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
641impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
642impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);