Skip to main content

core/convert/
num.rs

1use crate::num::{IntErrorKind, TryFromIntError};
2
3/// Supporting trait for inherent methods of `f32` and `f64` such as `to_int_unchecked`.
4/// Typically doesn’t need to be used directly.
5#[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                    // SAFETY: the safety contract must be upheld by the caller.
20                    unsafe { crate::intrinsics::float_to_int_unchecked(self) }
21                }
22            }
23        )+
24    }
25}
26
27#[unstable(feature = "convert_float_to_int", issue = "67057")]
impl FloatToInt<isize> for f16 {
    #[inline]
    unsafe fn to_int_unchecked(self) -> isize {
        unsafe { crate::intrinsics::float_to_int_unchecked(self) }
    }
}impl_float_to_int!(f16 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
28#[unstable(feature = "convert_float_to_int", issue = "67057")]
impl FloatToInt<isize> for f32 {
    #[inline]
    unsafe fn to_int_unchecked(self) -> isize {
        unsafe { crate::intrinsics::float_to_int_unchecked(self) }
    }
}impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
29#[unstable(feature = "convert_float_to_int", issue = "67057")]
impl FloatToInt<isize> for f64 {
    #[inline]
    unsafe fn to_int_unchecked(self) -> isize {
        unsafe { crate::intrinsics::float_to_int_unchecked(self) }
    }
}impl_float_to_int!(f64 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
30#[unstable(feature = "convert_float_to_int", issue = "67057")]
impl FloatToInt<isize> for f128 {
    #[inline]
    unsafe fn to_int_unchecked(self) -> isize {
        unsafe { crate::intrinsics::float_to_int_unchecked(self) }
    }
}impl_float_to_int!(f128 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
31
32/// Implement `From<bool>` for integers
33macro_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        const impl From<bool> for $int {
38            /// Converts from [`bool`] to
39            #[doc = concat!("[`", stringify!($int), "`]")]
40            /// , by turning `false` into `0` and `true` into `1`.
41            ///
42            /// # Examples
43            ///
44            /// ```
45            #[doc = concat!("assert_eq!(", stringify!($int), "::from(false), 0);")]
46            ///
47            #[doc = concat!("assert_eq!(", stringify!($int), "::from(true), 1);")]
48            /// ```
49            #[inline(always)]
50            fn from(b: bool) -> Self {
51                b as Self
52            }
53        }
54    )*}
55}
56
57// boolean -> integer
58#[stable(feature = "from_bool", since = "1.28.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<bool> for usize {
    /// Converts from [`bool`] to
    #[doc = "[`usize`]"]
    /// , by turning `false` into `0` and `true` into `1`.
    ///
    /// # Examples
    ///
    /// ```
    #[doc = "assert_eq!(usize::from(false), 0);"]
    ///
    #[doc = "assert_eq!(usize::from(true), 1);"]
    /// ```
    #[inline(always)]
    fn from(b: bool) -> Self { b as Self }
}impl_from_bool!(u8 u16 u32 u64 u128 usize);
59#[stable(feature = "from_bool", since = "1.28.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<bool> for isize {
    /// Converts from [`bool`] to
    #[doc = "[`isize`]"]
    /// , by turning `false` into `0` and `true` into `1`.
    ///
    /// # Examples
    ///
    /// ```
    #[doc = "assert_eq!(isize::from(false), 0);"]
    ///
    #[doc = "assert_eq!(isize::from(true), 1);"]
    /// ```
    #[inline(always)]
    fn from(b: bool) -> Self { b as Self }
}impl_from_bool!(i8 i16 i32 i64 i128 isize);
60
61/// Implement `From<$small>` for `$large`
62macro_rules! impl_from {
63    ($small:ty => $large:ty, $(#[$attrs:meta]),+) => {
64        $(#[$attrs])+
65        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
66        const impl 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
78// unsigned integer -> unsigned integer
79#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for u16 {
    #[doc = "Converts from [`u8`] to [`u16`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<u16>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u16>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <u16>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <u16>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
80#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for u32 {
    #[doc = "Converts from [`u8`] to [`u32`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<u32>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u32>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <u32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <u32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
81#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for u64 {
    #[doc = "Converts from [`u8`] to [`u64`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<u64>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u64>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <u64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <u64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
82#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for u128 {
    #[doc = "Converts from [`u8`] to [`u128`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<u128>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u128>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <u128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <u128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
83#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for usize {
    #[doc = "Converts from [`u8`] to [`usize`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<usize>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <usize>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <usize>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <usize>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
84#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for u32 {
    #[doc = "Converts from [`u16`] to [`u32`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<u32>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u32>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <u32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <u32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
85#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for u64 {
    #[doc = "Converts from [`u16`] to [`u64`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<u64>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u64>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <u64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <u64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
86#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for u128 {
    #[doc = "Converts from [`u16`] to [`u128`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<u128>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u128>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <u128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <u128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
87#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u32> for u64 {
    #[doc = "Converts from [`u32`] to [`u64`] losslessly."]
    #[inline(always)]
    fn from(small: u32) -> Self {
        if true {
            if !(<u64>::MIN as i128 <= <u32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u64>::MIN as i128 <= <u32>::MIN as i128")
            };
        };
        if true {
            if !(<u32>::MAX as u128 <= <u64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u32>::MAX as u128 <= <u64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
88#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u32> for u128 {
    #[doc = "Converts from [`u32`] to [`u128`] losslessly."]
    #[inline(always)]
    fn from(small: u32) -> Self {
        if true {
            if !(<u128>::MIN as i128 <= <u32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u128>::MIN as i128 <= <u32>::MIN as i128")
            };
        };
        if true {
            if !(<u32>::MAX as u128 <= <u128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u32>::MAX as u128 <= <u128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
89#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u64> for u128 {
    #[doc = "Converts from [`u64`] to [`u128`] losslessly."]
    #[inline(always)]
    fn from(small: u64) -> Self {
        if true {
            if !(<u128>::MIN as i128 <= <u64>::MIN as i128) {
                crate::panicking::panic("assertion failed: <u128>::MIN as i128 <= <u64>::MIN as i128")
            };
        };
        if true {
            if !(<u64>::MAX as u128 <= <u128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u64>::MAX as u128 <= <u128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
90
91// signed integer -> signed integer
92#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for i16 {
    #[doc = "Converts from [`i8`] to [`i16`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<i16>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i16>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <i16>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <i16>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
93#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for i32 {
    #[doc = "Converts from [`i8`] to [`i32`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<i32>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i32>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <i32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <i32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
94#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for i64 {
    #[doc = "Converts from [`i8`] to [`i64`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<i64>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i64>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <i64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <i64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
95#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for i128 {
    #[doc = "Converts from [`i8`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
96#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for isize {
    #[doc = "Converts from [`i8`] to [`isize`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<isize>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <isize>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <isize>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <isize>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
97#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i16> for i32 {
    #[doc = "Converts from [`i16`] to [`i32`] losslessly."]
    #[inline(always)]
    fn from(small: i16) -> Self {
        if true {
            if !(<i32>::MIN as i128 <= <i16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i32>::MIN as i128 <= <i16>::MIN as i128")
            };
        };
        if true {
            if !(<i16>::MAX as u128 <= <i32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i16>::MAX as u128 <= <i32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
98#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i16> for i64 {
    #[doc = "Converts from [`i16`] to [`i64`] losslessly."]
    #[inline(always)]
    fn from(small: i16) -> Self {
        if true {
            if !(<i64>::MIN as i128 <= <i16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i64>::MIN as i128 <= <i16>::MIN as i128")
            };
        };
        if true {
            if !(<i16>::MAX as u128 <= <i64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i16>::MAX as u128 <= <i64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
99#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i16> for i128 {
    #[doc = "Converts from [`i16`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: i16) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <i16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <i16>::MIN as i128")
            };
        };
        if true {
            if !(<i16>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i16>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
100#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i32> for i64 {
    #[doc = "Converts from [`i32`] to [`i64`] losslessly."]
    #[inline(always)]
    fn from(small: i32) -> Self {
        if true {
            if !(<i64>::MIN as i128 <= <i32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i64>::MIN as i128 <= <i32>::MIN as i128")
            };
        };
        if true {
            if !(<i32>::MAX as u128 <= <i64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i32>::MAX as u128 <= <i64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
101#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i32> for i128 {
    #[doc = "Converts from [`i32`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: i32) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <i32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <i32>::MIN as i128")
            };
        };
        if true {
            if !(<i32>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i32>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
102#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i64> for i128 {
    #[doc = "Converts from [`i64`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: i64) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <i64>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <i64>::MIN as i128")
            };
        };
        if true {
            if !(<i64>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i64>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
103
104// unsigned integer -> signed integer
105#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for i16 {
    #[doc = "Converts from [`u8`] to [`i16`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<i16>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i16>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <i16>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <i16>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
106#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for i32 {
    #[doc = "Converts from [`u8`] to [`i32`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<i32>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i32>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <i32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <i32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
107#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for i64 {
    #[doc = "Converts from [`u8`] to [`i64`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<i64>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i64>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <i64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <i64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
108#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for i128 {
    #[doc = "Converts from [`u8`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
109#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for i32 {
    #[doc = "Converts from [`u16`] to [`i32`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<i32>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i32>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <i32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <i32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
110#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for i64 {
    #[doc = "Converts from [`u16`] to [`i64`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<i64>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i64>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <i64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <i64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
111#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for i128 {
    #[doc = "Converts from [`u16`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
112#[stable(feature = "lossless_int_conv", since = "1.5.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u32> for i64 {
    #[doc = "Converts from [`u32`] to [`i64`] losslessly."]
    #[inline(always)]
    fn from(small: u32) -> Self {
        if true {
            if !(<i64>::MIN as i128 <= <u32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i64>::MIN as i128 <= <u32>::MIN as i128")
            };
        };
        if true {
            if !(<u32>::MAX as u128 <= <i64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u32>::MAX as u128 <= <i64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
113#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u32> for i128 {
    #[doc = "Converts from [`u32`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: u32) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <u32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <u32>::MIN as i128")
            };
        };
        if true {
            if !(<u32>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u32>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
114#[stable(feature = "i128", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u64> for i128 {
    #[doc = "Converts from [`u64`] to [`i128`] losslessly."]
    #[inline(always)]
    fn from(small: u64) -> Self {
        if true {
            if !(<i128>::MIN as i128 <= <u64>::MIN as i128) {
                crate::panicking::panic("assertion failed: <i128>::MIN as i128 <= <u64>::MIN as i128")
            };
        };
        if true {
            if !(<u64>::MAX as u128 <= <i128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u64>::MAX as u128 <= <i128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
115
116// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
117// which imply that pointer-sized integers must be at least 16 bits:
118// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
119#[stable(feature = "lossless_iusize_conv", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for usize {
    #[doc = "Converts from [`u16`] to [`usize`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<usize>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <usize>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <usize>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <usize>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
120#[stable(feature = "lossless_iusize_conv", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for isize {
    #[doc = "Converts from [`u8`] to [`isize`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<isize>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <isize>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <isize>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <isize>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
121#[stable(feature = "lossless_iusize_conv", since = "1.26.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i16> for isize {
    #[doc = "Converts from [`i16`] to [`isize`] losslessly."]
    #[inline(always)]
    fn from(small: i16) -> Self {
        if true {
            if !(<isize>::MIN as i128 <= <i16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <isize>::MIN as i128 <= <i16>::MIN as i128")
            };
        };
        if true {
            if !(<i16>::MAX as u128 <= <isize>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i16>::MAX as u128 <= <isize>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
122
123// RISC-V defines the possibility of a 128-bit address space (RV128).
124
125// CHERI proposes 128-bit “capabilities”. Unclear if this would be relevant to usize/isize.
126// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
127// https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf
128
129// Note: integers can only be represented with full precision in a float if
130// they fit in the significand, which is:
131// * 11 bits in f16
132// * 24 bits in f32
133// * 53 bits in f64
134// * 113 bits in f128
135// Lossy float conversions are not implemented at this time.
136// FIXME(f16,f128): The `f16`/`f128` impls `#[stable]` attributes should be changed to reference
137// `f16`/`f128` when they are stabilised (trait impls have to have a `#[stable]` attribute, but none
138// of the `f16`/`f128` impls can be used on stable as the `f16` and `f128` types are unstable).
139
140// signed integer -> float
141#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for f16 {
    #[doc = "Converts from [`i8`] to [`f16`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<f16>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f16>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <f16>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <f16>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
142#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for f32 {
    #[doc = "Converts from [`i8`] to [`f32`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<f32>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f32>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <f32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <f32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
143#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for f64 {
    #[doc = "Converts from [`i8`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
144#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i8> for f128 {
    #[doc = "Converts from [`i8`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: i8) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <i8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <i8>::MIN as i128")
            };
        };
        if true {
            if !(<i8>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i8>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
145#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i16> for f32 {
    #[doc = "Converts from [`i16`] to [`f32`] losslessly."]
    #[inline(always)]
    fn from(small: i16) -> Self {
        if true {
            if !(<f32>::MIN as i128 <= <i16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f32>::MIN as i128 <= <i16>::MIN as i128")
            };
        };
        if true {
            if !(<i16>::MAX as u128 <= <f32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i16>::MAX as u128 <= <f32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
146#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i16> for f64 {
    #[doc = "Converts from [`i16`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: i16) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <i16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <i16>::MIN as i128")
            };
        };
        if true {
            if !(<i16>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i16>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
147#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i16> for f128 {
    #[doc = "Converts from [`i16`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: i16) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <i16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <i16>::MIN as i128")
            };
        };
        if true {
            if !(<i16>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i16>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
148#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i32> for f64 {
    #[doc = "Converts from [`i32`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: i32) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <i32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <i32>::MIN as i128")
            };
        };
        if true {
            if !(<i32>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i32>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
149#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i32> for f128 {
    #[doc = "Converts from [`i32`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: i32) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <i32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <i32>::MIN as i128")
            };
        };
        if true {
            if !(<i32>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i32>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
150#[unstable(feature = "f128", issue = "116909")]
#[unstable_feature_bound(f128)]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<i64> for f128 {
    #[doc = "Converts from [`i64`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: i64) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <i64>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <i64>::MIN as i128")
            };
        };
        if true {
            if !(<i64>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <i64>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(i64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
151
152// unsigned integer -> float
153#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for f16 {
    #[doc = "Converts from [`u8`] to [`f16`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<f16>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f16>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <f16>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <f16>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => f16, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
154#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for f32 {
    #[doc = "Converts from [`u8`] to [`f32`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<f32>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f32>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <f32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <f32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
155#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for f64 {
    #[doc = "Converts from [`u8`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
156#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u8> for f128 {
    #[doc = "Converts from [`u8`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: u8) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <u8>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <u8>::MIN as i128")
            };
        };
        if true {
            if !(<u8>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u8>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u8 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for f32 {
    #[doc = "Converts from [`u16`] to [`f32`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<f32>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f32>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <f32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <f32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for f64 {
    #[doc = "Converts from [`u16`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u16> for f128 {
    #[doc = "Converts from [`u16`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: u16) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <u16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <u16>::MIN as i128")
            };
        };
        if true {
            if !(<u16>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u16>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
160#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u32> for f64 {
    #[doc = "Converts from [`u32`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: u32) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <u32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <u32>::MIN as i128")
            };
        };
        if true {
            if !(<u32>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u32>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
161#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u32> for f128 {
    #[doc = "Converts from [`u32`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: u32) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <u32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <u32>::MIN as i128")
            };
        };
        if true {
            if !(<u32>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u32>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
162#[unstable(feature = "f128", issue = "116909")]
#[unstable_feature_bound(f128)]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<u64> for f128 {
    #[doc = "Converts from [`u64`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: u64) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <u64>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <u64>::MIN as i128")
            };
        };
        if true {
            if !(<u64>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <u64>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(u64 => f128, #[unstable(feature = "f128", issue = "116909")], #[unstable_feature_bound(f128)]);
163
164// float -> float
165
166// FIXME(f16): adding the additional `From<{float}>` impl to `f32` would break inference in cases
167// like `f32::from(1.0)`. The type checker has a custom workaround to keep that and similar code
168// compiling even with the second `From<16> for f32` instance. We keep this instance unstable for
169// now so that we can later remove the workaround.
170//
171// See also <https://github.com/rust-lang/rust/issues/123831>.
172#[unstable(feature = "f32_from_f16", issue = "154005")]
#[unstable_feature_bound(f32_from_f16)]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<f16> for f32 {
    #[doc = "Converts from [`f16`] to [`f32`] losslessly."]
    #[inline(always)]
    fn from(small: f16) -> Self {
        if true {
            if !(<f32>::MIN as i128 <= <f16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f32>::MIN as i128 <= <f16>::MIN as i128")
            };
        };
        if true {
            if !(<f16>::MAX as u128 <= <f32>::MAX as u128) {
                crate::panicking::panic("assertion failed: <f16>::MAX as u128 <= <f32>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(f16 => f32, #[unstable(feature = "f32_from_f16", issue = "154005")], #[unstable_feature_bound(f32_from_f16)]);
173#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<f16> for f64 {
    #[doc = "Converts from [`f16`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: f16) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <f16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <f16>::MIN as i128")
            };
        };
        if true {
            if !(<f16>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <f16>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(f16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
174#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<f16> for f128 {
    #[doc = "Converts from [`f16`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: f16) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <f16>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <f16>::MIN as i128")
            };
        };
        if true {
            if !(<f16>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <f16>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
175#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<f32> for f64 {
    #[doc = "Converts from [`f32`] to [`f64`] losslessly."]
    #[inline(always)]
    fn from(small: f32) -> Self {
        if true {
            if !(<f64>::MIN as i128 <= <f32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f64>::MIN as i128 <= <f32>::MIN as i128")
            };
        };
        if true {
            if !(<f32>::MAX as u128 <= <f64>::MAX as u128) {
                crate::panicking::panic("assertion failed: <f32>::MAX as u128 <= <f64>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
176#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<f32> for f128 {
    #[doc = "Converts from [`f32`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: f32) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <f32>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <f32>::MIN as i128")
            };
        };
        if true {
            if !(<f32>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <f32>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
177#[stable(feature = "lossless_float_conv", since = "1.6.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<f64> for f128 {
    #[doc = "Converts from [`f64`] to [`f128`] losslessly."]
    #[inline(always)]
    fn from(small: f64) -> Self {
        if true {
            if !(<f128>::MIN as i128 <= <f64>::MIN as i128) {
                crate::panicking::panic("assertion failed: <f128>::MIN as i128 <= <f64>::MIN as i128")
            };
        };
        if true {
            if !(<f64>::MAX as u128 <= <f128>::MAX as u128) {
                crate::panicking::panic("assertion failed: <f64>::MAX as u128 <= <f128>::MAX as u128")
            };
        };
        small as Self
    }
}impl_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            const impl From<bool> for $float {
189            #[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
190            /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
191            ///
192            /// # Examples
193            /// ```
194            $($(#[doc = $doctest_prefix])*)?
195            #[doc = concat!("let x = ", stringify!($float), "::from(false);")]
196            /// assert_eq!(x, 0.0);
197            /// assert!(x.is_sign_positive());
198            ///
199            #[doc = concat!("let y = ", stringify!($float), "::from(true);")]
200            /// assert_eq!(y, 1.0);
201            $($(#[doc = $doctest_suffix])*)?
202            /// ```
203            #[inline]
204            fn from(small: bool) -> Self {
205                small as u8 as Self
206            }
207        }
208    };
209}
210
211// boolean -> float
212#[stable(feature = "float_from_bool", since = "1.68.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<bool> for f16 {
    #[doc = "Converts a [`bool`] to [`f16`] losslessly."]
    /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
    ///
    /// # Examples
    /// ```
    #[doc = r"# #![allow(unused_features)]"]
    #[doc = r"#![feature(f16)]"]
    #[doc = r#"# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {"#]
    #[doc = r""]
    #[doc = "let x = f16::from(false);"]
    /// assert_eq!(x, 0.0);
    /// assert!(x.is_sign_positive());
    ///
    #[doc = "let y = f16::from(true);"]
    /// assert_eq!(y, 1.0);
    #[doc = r"# }"]
    /// ```
    #[inline]
    fn from(small: bool) -> Self { small as u8 as Self }
}impl_float_from_bool!(
213    f16;
214    doctest_prefix:
215    // rustdoc doesn't remove the conventional space after the `///`
216    ///# #![allow(unused_features)]
217    ///#![feature(f16)]
218    ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
219    ///
220    doctest_suffix:
221    ///# }
222);
223#[stable(feature = "float_from_bool", since = "1.68.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<bool> for f32 {
    #[doc = "Converts a [`bool`] to [`f32`] losslessly."]
    /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
    ///
    /// # Examples
    /// ```
    #[doc = "let x = f32::from(false);"]
    /// assert_eq!(x, 0.0);
    /// assert!(x.is_sign_positive());
    ///
    #[doc = "let y = f32::from(true);"]
    /// assert_eq!(y, 1.0);
    /// ```
    #[inline]
    fn from(small: bool) -> Self { small as u8 as Self }
}impl_float_from_bool!(f32);
224#[stable(feature = "float_from_bool", since = "1.68.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<bool> for f64 {
    #[doc = "Converts a [`bool`] to [`f64`] losslessly."]
    /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
    ///
    /// # Examples
    /// ```
    #[doc = "let x = f64::from(false);"]
    /// assert_eq!(x, 0.0);
    /// assert!(x.is_sign_positive());
    ///
    #[doc = "let y = f64::from(true);"]
    /// assert_eq!(y, 1.0);
    /// ```
    #[inline]
    fn from(small: bool) -> Self { small as u8 as Self }
}impl_float_from_bool!(f64);
225#[stable(feature = "float_from_bool", since = "1.68.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<bool> for f128 {
    #[doc = "Converts a [`bool`] to [`f128`] losslessly."]
    /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
    ///
    /// # Examples
    /// ```
    #[doc = r"# #![allow(unused_features)]"]
    #[doc = r"#![feature(f128)]"]
    #[doc = r#"# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {"#]
    #[doc = r""]
    #[doc = "let x = f128::from(false);"]
    /// assert_eq!(x, 0.0);
    /// assert!(x.is_sign_positive());
    ///
    #[doc = "let y = f128::from(true);"]
    /// assert_eq!(y, 1.0);
    #[doc = r"# }"]
    /// ```
    #[inline]
    fn from(small: bool) -> Self { small as u8 as Self }
}impl_float_from_bool!(
226    f128;
227    doctest_prefix:
228    ///# #![allow(unused_features)]
229    ///#![feature(f128)]
230    ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
231    ///
232    doctest_suffix:
233    ///# }
234);
235
236// no possible bounds violation
237macro_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        const impl TryFrom<$source> for $target {
242            type Error = TryFromIntError;
243
244            /// Tries to create the target number type from a source
245            /// number type. This returns an error if the source value
246            /// is outside of the range of the target type.
247            #[inline]
248            fn try_from(value: $source) -> Result<Self, Self::Error> {
249                Ok(value as Self)
250            }
251        }
252    )*}
253}
254
255// only negative bounds
256macro_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        const impl TryFrom<$source> for $target {
261            type Error = TryFromIntError;
262
263            /// Tries to create the target number type from a source
264            /// number type. This returns an error if the source value
265            /// is outside of the range of the target type.
266            #[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
278// unsigned to signed (only positive bound)
279macro_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        const impl TryFrom<$source> for $target {
284            type Error = TryFromIntError;
285
286            /// Tries to create the target number type from a source
287            /// number type. This returns an error if the source value
288            /// is outside of the range of the target type.
289            #[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
301// all other cases
302macro_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        const impl TryFrom<$source> for $target {
307            type Error = TryFromIntError;
308
309            /// Tries to create the target number type from a source
310            /// number type. This returns an error if the source value
311            /// is outside of the range of the target type.
312            #[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
328/// Implement `TryFrom<integer>` for `bool`
329macro_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        const impl TryFrom<$int> for bool {
334            type Error = TryFromIntError;
335
336            /// Tries to create a bool from an integer type.
337            /// Returns an error if the integer is not 0 or 1.
338            ///
339            /// # Examples
340            ///
341            /// ```
342            #[doc = concat!("assert_eq!(bool::try_from(0_", stringify!($int), "), Ok(false));")]
343            ///
344            #[doc = concat!("assert_eq!(bool::try_from(1_", stringify!($int), "), Ok(true));")]
345            ///
346            #[doc = concat!("assert!(bool::try_from(2_", stringify!($int), ").is_err());")]
347            /// ```
348            #[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
379// integer -> bool
380#[stable(feature = "bool_try_from_int", since = "1.95.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u8> for bool {
    type Error = TryFromIntError;
    /// Tries to create a bool from an integer type.
    /// Returns an error if the integer is not 0 or 1.
    ///
    /// # Examples
    ///
    /// ```
    #[doc = "assert_eq!(bool::try_from(0_u8), Ok(false));"]
    ///
    #[doc = "assert_eq!(bool::try_from(1_u8), Ok(true));"]
    ///
    #[doc = "assert!(bool::try_from(2_u8).is_err());"]
    /// ```
    #[inline]
    fn try_from(i: u8) -> Result<Self, Self::Error> {
        match i {
            0 => Ok(false),
            1 => Ok(true),
            2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
        }
    }
}impl_try_from_integer_for_bool!(unsigned u128 u64 u32 u16 u8);
381#[stable(feature = "bool_try_from_int", since = "1.95.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i8> for bool {
    type Error = TryFromIntError;
    /// Tries to create a bool from an integer type.
    /// Returns an error if the integer is not 0 or 1.
    ///
    /// # Examples
    ///
    /// ```
    #[doc = "assert_eq!(bool::try_from(0_i8), Ok(false));"]
    ///
    #[doc = "assert_eq!(bool::try_from(1_i8), Ok(true));"]
    ///
    #[doc = "assert!(bool::try_from(2_i8).is_err());"]
    /// ```
    #[inline]
    fn try_from(i: i8) -> Result<Self, Self::Error> {
        match i {
            0 => Ok(false),
            1 => Ok(true),
            ..0 => Err(TryFromIntError(IntErrorKind::NegOverflow)),
            2.. => Err(TryFromIntError(IntErrorKind::PosOverflow)),
        }
    }
}impl_try_from_integer_for_bool!(signed i128 i64 i32 i16 i8);
382
383// unsigned integer -> unsigned integer
384#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u16> for u8 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u16) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u16) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u16 => u8);
385#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u32> for u16 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u32) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u32) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u32 => u8, u16);
386#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u64> for u32 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u64) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u64) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u64 => u8, u16, u32);
387#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u128> for u64 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u128) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u128) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
388
389// signed integer -> signed integer
390#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i16> for i8 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i16) -> Result<Self, Self::Error> {
        let min = Self::MIN as i16;
        let max = Self::MAX as i16;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i16 => i8);
391#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i32> for i16 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i32) -> Result<Self, Self::Error> {
        let min = Self::MIN as i32;
        let max = Self::MAX as i32;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i32 => i8, i16);
392#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i64> for i32 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i64) -> Result<Self, Self::Error> {
        let min = Self::MIN as i64;
        let max = Self::MAX as i64;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i64 => i8, i16, i32);
393#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i128> for i64 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i128) -> Result<Self, Self::Error> {
        let min = Self::MIN as i128;
        let max = Self::MAX as i128;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
394
395// unsigned integer -> signed integer
396#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u8> for i8 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u8) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u8) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u8 => i8);
397#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u16> for i16 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u16) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u16) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u16 => i8, i16);
398#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u32> for i32 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u32) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u32) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u32 => i8, i16, i32);
399#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u64> for i64 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u64) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u64) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
400#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u128> for i128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u128) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u128) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
401
402// signed integer -> unsigned integer
403#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i8> for u128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i8) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
404#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i16> for u8 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i16) -> Result<Self, Self::Error> {
        let min = Self::MIN as i16;
        let max = Self::MAX as i16;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i16 => u8);
405#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i16> for u128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i16) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
406#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i32> for u16 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i32) -> Result<Self, Self::Error> {
        let min = Self::MIN as i32;
        let max = Self::MAX as i32;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i32 => u8, u16);
407#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i32> for u128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i32) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}impl_try_from_lower_bounded!(i32 => u32, u64, u128);
408#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i64> for u32 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i64) -> Result<Self, Self::Error> {
        let min = Self::MIN as i64;
        let max = Self::MAX as i64;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i64 => u8, u16, u32);
409#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i64> for u128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i64) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}impl_try_from_lower_bounded!(i64 => u64, u128);
410#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i128> for u64 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i128) -> Result<Self, Self::Error> {
        let min = Self::MIN as i128;
        let max = Self::MAX as i128;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
411#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i128> for u128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i128) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}impl_try_from_lower_bounded!(i128 => u128);
412
413// usize/isize
414#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<usize> for isize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: usize) -> Result<Self, Self::Error> {
        if u > (Self::MAX as usize) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(usize => isize);
415#[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<isize> for usize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: isize) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}impl_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    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<usize> for u32 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: usize) -> Result<Self, Self::Error> {
        if u > (Self::MAX as usize) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(usize => u8, u16, u32);
469    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<usize> for u128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(value: usize) -> Result<Self, Self::Error> {
        Ok(value as Self)
    }
}impl_try_from_unbounded!(usize => u64, u128);
470    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<usize> for i64 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: usize) -> Result<Self, Self::Error> {
        if u > (Self::MAX as usize) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_upper_bounded!(usize => i8, i16, i32, i64);
471    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<usize> for i128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(value: usize) -> Result<Self, Self::Error> {
        Ok(value as Self)
    }
}impl_try_from_unbounded!(usize => i128);
472
473    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<isize> for u32 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: isize) -> Result<Self, Self::Error> {
        let min = Self::MIN as isize;
        let max = Self::MAX as isize;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(isize => u8, u16, u32);
474    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<isize> for u128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: isize) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}impl_try_from_lower_bounded!(isize => u64, u128);
475    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<isize> for i32 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: isize) -> Result<Self, Self::Error> {
        let min = Self::MIN as isize;
        let max = Self::MAX as isize;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}impl_try_from_both_bounded!(isize => i8, i16, i32);
476    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<isize> for i128 {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(value: isize) -> Result<Self, Self::Error> {
        Ok(value as Self)
    }
}impl_try_from_unbounded!(isize => i64, i128);
477
478    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u64> for usize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(value: u64) -> Result<Self, Self::Error> { Ok(value as Self) }
}rev!(impl_try_from_unbounded, usize => u32, u64);
479    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u128> for usize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u128) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u128) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}rev!(impl_try_from_upper_bounded, usize => u128);
480    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i64> for usize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i64) -> Result<Self, Self::Error> {
        if u >= 0 {
            Ok(u as Self)
        } else { Err(TryFromIntError(IntErrorKind::NegOverflow)) }
    }
}rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64);
481    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i128> for usize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i128) -> Result<Self, Self::Error> {
        let min = Self::MIN as i128;
        let max = Self::MAX as i128;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}rev!(impl_try_from_both_bounded, usize => i128);
482
483    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u32> for isize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(value: u32) -> Result<Self, Self::Error> { Ok(value as Self) }
}rev!(impl_try_from_unbounded, isize => u16, u32);
484    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u128> for isize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: u128) -> Result<Self, Self::Error> {
        if u > (Self::MAX as u128) {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}rev!(impl_try_from_upper_bounded, isize => u64, u128);
485    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i64> for isize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(value: i64) -> Result<Self, Self::Error> { Ok(value as Self) }
}rev!(impl_try_from_unbounded, isize => i32, i64);
486    #[stable(feature = "try_from", since = "1.34.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i128> for isize {
    type Error = TryFromIntError;
    /// Tries to create the target number type from a source
    /// number type. This returns an error if the source value
    /// is outside of the range of the target type.
    #[inline]
    fn try_from(u: i128) -> Result<Self, Self::Error> {
        let min = Self::MIN as i128;
        let max = Self::MAX as i128;
        if u < min {
            Err(TryFromIntError(IntErrorKind::NegOverflow))
        } else if u > max {
            Err(TryFromIntError(IntErrorKind::PosOverflow))
        } else { Ok(u as Self) }
    }
}rev!(impl_try_from_both_bounded, isize => i128);
487}
488
489// Conversion traits for non-zero integer types
490use 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            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
498            // Rustdocs on functions do not.
499            #[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                // SAFETY: input type guarantees the value is non-zero
504                unsafe { Self::new_unchecked(From::from(small.get())) }
505            }
506        }
507    };
508}
509
510// non-zero unsigned integer -> non-zero unsigned integer
511#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<u16> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[u16]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => u16);
512#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<u32> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[u32]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => u32);
513#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<u64> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[u64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => u64);
514#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<u128> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[u128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => u128);
515#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<usize> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => usize);
516#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u16>> for NonZero<u32> {
    #[doc = "Converts <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[u32]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u16 => u32);
517#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u16>> for NonZero<u64> {
    #[doc = "Converts <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[u64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u16 => u64);
518#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u16>> for NonZero<u128> {
    #[doc = "Converts <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[u128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u16 => u128);
519#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u16>> for NonZero<usize> {
    #[doc = "Converts <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u16 => usize);
520#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u32>> for NonZero<u64> {
    #[doc = "Converts <code>[NonZero]\\<[u32]></code> "]
    #[doc = "to <code>[NonZero]\\<[u64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u32>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u32 => u64);
521#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u32>> for NonZero<u128> {
    #[doc = "Converts <code>[NonZero]\\<[u32]></code> "]
    #[doc = "to <code>[NonZero]\\<[u128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u32>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u32 => u128);
522#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u64>> for NonZero<u128> {
    #[doc = "Converts <code>[NonZero]\\<[u64]></code> "]
    #[doc = "to <code>[NonZero]\\<[u128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u64>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u64 => u128);
523
524// non-zero signed integer -> non-zero signed integer
525#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i8>> for NonZero<i16> {
    #[doc = "Converts <code>[NonZero]\\<[i8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i16]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i8 => i16);
526#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i8>> for NonZero<i32> {
    #[doc = "Converts <code>[NonZero]\\<[i8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i32]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i8 => i32);
527#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i8>> for NonZero<i64> {
    #[doc = "Converts <code>[NonZero]\\<[i8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i8 => i64);
528#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i8>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[i8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i8 => i128);
529#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i8>> for NonZero<isize> {
    #[doc = "Converts <code>[NonZero]\\<[i8]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i8 => isize);
530#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i16>> for NonZero<i32> {
    #[doc = "Converts <code>[NonZero]\\<[i16]></code> "]
    #[doc = "to <code>[NonZero]\\<[i32]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i16 => i32);
531#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i16>> for NonZero<i64> {
    #[doc = "Converts <code>[NonZero]\\<[i16]></code> "]
    #[doc = "to <code>[NonZero]\\<[i64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i16 => i64);
532#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i16>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[i16]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i16 => i128);
533#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i16>> for NonZero<isize> {
    #[doc = "Converts <code>[NonZero]\\<[i16]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i16 => isize);
534#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i32>> for NonZero<i64> {
    #[doc = "Converts <code>[NonZero]\\<[i32]></code> "]
    #[doc = "to <code>[NonZero]\\<[i64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i32>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i32 => i64);
535#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i32>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[i32]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i32>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i32 => i128);
536#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<i64>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[i64]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<i64>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(i64 => i128);
537
538// non-zero unsigned -> non-zero signed integer
539#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<i16> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i16]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => i16);
540#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<i32> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i32]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => i32);
541#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<i64> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => i64);
542#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => i128);
543#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u8>> for NonZero<isize> {
    #[doc = "Converts <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u8>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u8 => isize);
544#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u16>> for NonZero<i32> {
    #[doc = "Converts <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[i32]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u16 => i32);
545#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u16>> for NonZero<i64> {
    #[doc = "Converts <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[i64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u16 => i64);
546#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u16>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u16>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u16 => i128);
547#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u32>> for NonZero<i64> {
    #[doc = "Converts <code>[NonZero]\\<[u32]></code> "]
    #[doc = "to <code>[NonZero]\\<[i64]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u32>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u32 => i64);
548#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u32>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[u32]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u32>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_nonzero_int_from_nonzero_int!(u32 => i128);
549#[stable(feature = "nz_int_conv", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl From<NonZero<u64>> for NonZero<i128> {
    #[doc = "Converts <code>[NonZero]\\<[u64]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code> losslessly."]
    #[inline]
    fn from(small: NonZero<u64>) -> Self {
        unsafe { Self::new_unchecked(From::from(small.get())) }
    }
}impl_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            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
559            // Rustdocs on functions do not.
560            #[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
570// integer -> non-zero integer
571#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u8> for NonZero<u8> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`u8`] "]
    #[doc = "to <code>[NonZero]\\<[u8]></code>."]
    #[inline]
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(u8);
572#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u16> for NonZero<u16> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`u16`] "]
    #[doc = "to <code>[NonZero]\\<[u16]></code>."]
    #[inline]
    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(u16);
573#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u32> for NonZero<u32> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`u32`] "]
    #[doc = "to <code>[NonZero]\\<[u32]></code>."]
    #[inline]
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(u32);
574#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u64> for NonZero<u64> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`u64`] "]
    #[doc = "to <code>[NonZero]\\<[u64]></code>."]
    #[inline]
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(u64);
575#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<u128> for NonZero<u128> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`u128`] "]
    #[doc = "to <code>[NonZero]\\<[u128]></code>."]
    #[inline]
    fn try_from(value: u128) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(u128);
576#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<usize> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`usize`] "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: usize) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(usize);
577#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i8> for NonZero<i8> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`i8`] "]
    #[doc = "to <code>[NonZero]\\<[i8]></code>."]
    #[inline]
    fn try_from(value: i8) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(i8);
578#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i16> for NonZero<i16> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`i16`] "]
    #[doc = "to <code>[NonZero]\\<[i16]></code>."]
    #[inline]
    fn try_from(value: i16) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(i16);
579#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i32> for NonZero<i32> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`i32`] "]
    #[doc = "to <code>[NonZero]\\<[i32]></code>."]
    #[inline]
    fn try_from(value: i32) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(i32);
580#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i64> for NonZero<i64> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`i64`] "]
    #[doc = "to <code>[NonZero]\\<[i64]></code>."]
    #[inline]
    fn try_from(value: i64) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(i64);
581#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<i128> for NonZero<i128> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`i128`] "]
    #[doc = "to <code>[NonZero]\\<[i128]></code>."]
    #[inline]
    fn try_from(value: i128) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_nonzero_int_try_from_int!(i128);
582#[stable(feature = "nzint_try_from_int_conv", since = "1.46.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<isize> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert [`isize`] "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: isize) -> Result<Self, Self::Error> {
        Self::new(value).ok_or(TryFromIntError(IntErrorKind::Zero))
    }
}impl_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        const impl TryFrom<NonZero<$source>> for NonZero<$target> {
589            type Error = TryFromIntError;
590
591            // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
592            // Rustdocs on functions do not.
593            #[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                // SAFETY: Input is guaranteed to be non-zero.
598                Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) })
599            }
600        }
601    )*};
602}
603
604// unsigned non-zero integer -> unsigned non-zero integer
605#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u16>> for NonZero<u8> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[u8]></code>."]
    #[inline]
    fn try_from(value: NonZero<u16>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<u8>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
606#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u32>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u32]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<u32>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
607#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u64>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u64]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<u64>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
608#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u128>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u128]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<u128>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
609#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<usize>> for NonZero<u128> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[usize]></code> "]
    #[doc = "to <code>[NonZero]\\<[u128]></code>."]
    #[inline]
    fn try_from(value: NonZero<usize>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<u128>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
610
611// signed non-zero integer -> signed non-zero integer
612#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i16>> for NonZero<i8> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i16]></code> "]
    #[doc = "to <code>[NonZero]\\<[i8]></code>."]
    #[inline]
    fn try_from(value: NonZero<i16>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<i8>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
613#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i32>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i32]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i32>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
614#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i64>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i64]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i64>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
615#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i128>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i128]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i128>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
616#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<isize>> for NonZero<i128> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[isize]></code> "]
    #[doc = "to <code>[NonZero]\\<[i128]></code>."]
    #[inline]
    fn try_from(value: NonZero<isize>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<i128>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
617
618// unsigned non-zero integer -> signed non-zero integer
619#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u8>> for NonZero<i8> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u8]></code> "]
    #[doc = "to <code>[NonZero]\\<[i8]></code>."]
    #[inline]
    fn try_from(value: NonZero<u8>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<i8>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
620#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u16>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u16]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<u16>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
621#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u32>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u32]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<u32>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
622#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u64>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u64]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<u64>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
623#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<u128>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[u128]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<u128>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
624#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<usize>> for NonZero<isize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[usize]></code> "]
    #[doc = "to <code>[NonZero]\\<[isize]></code>."]
    #[inline]
    fn try_from(value: NonZero<usize>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<isize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
625
626// signed non-zero integer -> unsigned non-zero integer
627#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i8>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i8]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i8>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
628#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i16>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i16]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i16>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
629#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i32>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i32]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i32>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
630#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i64>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i64]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i64>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
631#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<i128>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[i128]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<i128>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
632#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
const impl TryFrom<NonZero<isize>> for NonZero<usize> {
    type Error = TryFromIntError;
    #[doc = "Attempts to convert <code>[NonZero]\\<[isize]></code> "]
    #[doc = "to <code>[NonZero]\\<[usize]></code>."]
    #[inline]
    fn try_from(value: NonZero<isize>) -> Result<Self, Self::Error> {
        Ok(unsafe { Self::new_unchecked(<usize>::try_from(value.get())?) })
    }
}impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);
633
634/// Conversion between integers, wrapping around or saturating at the target type's boundaries.
635#[unstable(feature = "integer_casts", issue = "157388")]
636#[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
637pub impl(self) const trait BoundedCastFromInt<T>: Sized {
638    /// Converts `value` to this type, wrapping around at the boundary of the type.
639    #[unstable(feature = "integer_casts", issue = "157388")]
640    fn wrapping_cast_from(value: T) -> Self;
641
642    /// Converts `value` to this type, saturating at the numeric bounds instead of overflowing.
643    #[unstable(feature = "integer_casts", issue = "157388")]
644    fn saturating_cast_from(value: T) -> Self;
645}
646
647/// Fallible conversion between integers.
648#[unstable(feature = "integer_casts", issue = "157388")]
649#[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
650pub impl(self) const trait CheckedCastFromInt<T>: Sized {
651    /// Converts `value` to this type, returning `None` if overflow would have occurred.
652    #[unstable(feature = "integer_casts", issue = "157388")]
653    fn checked_cast_from(value: T) -> Option<Self>;
654
655    /// Converts `value` to this type, assuming overflow cannot occur.
656    ///
657    /// # Safety
658    ///
659    /// This results in undefined behavior when `value` will overflow when
660    /// converted to this type.
661    #[unstable(feature = "integer_casts", issue = "157388")]
662    unsafe fn unchecked_cast_from(value: T) -> Self;
663
664    /// Converts `value` to this type, panicking on overflow.
665    ///
666    /// # Panics
667    ///
668    /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
669    #[unstable(feature = "integer_casts", issue = "157388")]
670    fn strict_cast_from(value: T) -> Self;
671}
672
673macro_rules! impl_int_cast {
674    ($Src:ty as [$($Dst:ty),*]) => {$(
675        #[unstable(feature = "integer_casts", issue = "157388")]
676        #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
677        const impl CheckedCastFromInt<$Src> for $Dst {
678            #[inline]
679            fn checked_cast_from(value: $Src) -> Option<Self> {
680                value.try_into().ok()
681            }
682
683            #[inline(always)]
684            unsafe fn unchecked_cast_from(value: $Src) -> Self {
685                // SAFETY: the safety contract must be upheld by the caller.
686                unsafe { value.try_into().unwrap_unchecked() }
687            }
688
689            #[inline]
690            #[track_caller]
691            fn strict_cast_from(value: $Src) -> Self {
692                match value.try_into() {
693                    Ok(x) => x,
694                    Err(_) => core::num::imp::overflow_panic::cast_integer()
695                }
696            }
697        }
698
699        #[unstable(feature = "integer_casts", issue = "157388")]
700        #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
701        const impl BoundedCastFromInt<$Src> for $Dst {
702            #[inline(always)]
703            fn wrapping_cast_from(value: $Src) -> Self {
704                value as Self
705            }
706
707            #[inline]
708            #[allow(unused_comparisons)]
709            #[allow(irrefutable_let_patterns)]
710            fn saturating_cast_from(value: $Src) -> Self {
711                if let Ok(x) = value.try_into() {
712                    return x;
713                }
714
715                if value < 0 { <$Dst>::MIN } else { <$Dst>::MAX }
716            }
717        }
718    )*};
719}
720
721macro_rules! impl_all_int_casts {
722    ([$($Src:ty),*]) => {$(
723        impl_int_cast!($Src as [u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize]);
724    )*};
725}
726
727#[unstable(feature = "integer_casts", issue = "157388")]
#[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
const impl CheckedCastFromInt<isize> for isize {
    #[inline]
    fn checked_cast_from(value: isize) -> Option<Self> {
        value.try_into().ok()
    }
    #[inline(always)]
    unsafe fn unchecked_cast_from(value: isize) -> Self {
        unsafe { value.try_into().unwrap_unchecked() }
    }
    #[inline]
    #[track_caller]
    fn strict_cast_from(value: isize) -> Self {
        match value.try_into() {
            Ok(x) => x,
            Err(_) => core::num::imp::overflow_panic::cast_integer(),
        }
    }
}
#[unstable(feature = "integer_casts", issue = "157388")]
#[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
const impl BoundedCastFromInt<isize> for isize {
    #[inline(always)]
    fn wrapping_cast_from(value: isize) -> Self { value as Self }
    #[inline]
    #[allow(unused_comparisons)]
    #[allow(irrefutable_let_patterns)]
    fn saturating_cast_from(value: isize) -> Self {
        if let Ok(x) = value.try_into() { return x; }
        if value < 0 { <isize>::MIN } else { <isize>::MAX }
    }
}impl_all_int_casts!([u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize]);