#![stable(feature = "rust1", since = "1.0.0")]
-use convert::{Infallible, TryFrom};
+use convert::TryFrom;
use fmt;
use intrinsics;
+#[allow(deprecated)] use nonzero::NonZero;
use ops;
use str::FromStr;
+macro_rules! impl_nonzero_fmt {
+ ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
+ $(
+ #[$stability]
+ impl fmt::$Trait for $Ty {
+ #[inline]
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.get().fmt(f)
+ }
+ }
+ )+
+ }
+}
+
+macro_rules! nonzero_integers {
+ ( #[$stability: meta] $( $Ty: ident($Int: ty); )+ ) => {
+ $(
+ /// An integer that is known not to equal zero.
+ ///
+ /// This may enable some memory layout optimization such as:
+ ///
+ /// ```rust
+ /// # #![feature(nonzero)]
+ /// use std::mem::size_of;
+ /// assert_eq!(size_of::<Option<std::num::NonZeroU32>>(), size_of::<u32>());
+ /// ```
+ #[$stability]
+ #[allow(deprecated)]
+ #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+ pub struct $Ty(NonZero<$Int>);
+
+ #[allow(deprecated)]
+ impl $Ty {
+ /// Create a non-zero without checking the value.
+ ///
+ /// # Safety
+ ///
+ /// The value must not be zero.
+ #[$stability]
+ #[inline]
+ pub const unsafe fn new_unchecked(n: $Int) -> Self {
+ $Ty(NonZero(n))
+ }
+
+ /// Create a non-zero if the given value is not zero.
+ #[$stability]
+ #[inline]
+ pub fn new(n: $Int) -> Option<Self> {
+ if n != 0 {
+ Some($Ty(NonZero(n)))
+ } else {
+ None
+ }
+ }
+
+ /// Returns the value as a primitive type.
+ #[$stability]
+ #[inline]
+ pub fn get(self) -> $Int {
+ self.0 .0
+ }
+
+ }
+
+ impl_nonzero_fmt! {
+ #[$stability]
+ (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
+ }
+ )+
+ }
+}
+
+nonzero_integers! {
+ #[unstable(feature = "nonzero", issue = "49137")]
+ NonZeroU8(u8); NonZeroI8(i8);
+ NonZeroU16(u16); NonZeroI16(i16);
+ NonZeroU32(u32); NonZeroI32(i32);
+ NonZeroU64(u64); NonZeroI64(i64);
+ NonZeroUsize(usize); NonZeroIsize(isize);
+}
+
+nonzero_integers! {
+ // Change this to `#[unstable(feature = "i128", issue = "35118")]`
+ // if other NonZero* integer types are stabilizied before 128-bit integers
+ #[unstable(feature = "nonzero", issue = "49137")]
+ NonZeroU128(u128); NonZeroI128(i128);
+}
+
/// Provides intentionally-wrapped arithmetic on `T`.
///
/// Operations like `+` on `u32` values is intended to never overflow,
#[lang = "i128"]
impl i128 {
int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728,
- 170141183460469231731687303715884105727, "#![feature(i128_type)]
-#![feature(i128)]
-# fn main() {
-", "
-# }" }
+ 170141183460469231731687303715884105727, "", "" }
}
#[cfg(target_pointer_width = "16")]
#[lang = "u128"]
impl u128 {
- uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "#![feature(i128_type)]
-#![feature(i128)]
-
-# fn main() {
-", "
-# }" }
+ uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "", "" }
}
#[cfg(target_pointer_width = "16")]
}
#[unstable(feature = "try_from", issue = "33417")]
-impl From<Infallible> for TryFromIntError {
- fn from(infallible: Infallible) -> TryFromIntError {
- match infallible {
- }
+impl From<!> for TryFromIntError {
+ fn from(never: !) -> TryFromIntError {
+ never
}
}
($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<$source> for $target {
- type Error = Infallible;
+ type Error = !;
#[inline]
fn try_from(value: $source) -> Result<Self, Self::Error> {
#[cfg(target_pointer_width = "16")]
mod ptr_try_from_impls {
use super::TryFromIntError;
- use convert::{Infallible, TryFrom};
+ use convert::TryFrom;
try_from_upper_bounded!(usize, u8);
try_from_unbounded!(usize, u16, u32, u64, u128);
#[cfg(target_pointer_width = "32")]
mod ptr_try_from_impls {
use super::TryFromIntError;
- use convert::{Infallible, TryFrom};
+ use convert::TryFrom;
try_from_upper_bounded!(usize, u8, u16);
try_from_unbounded!(usize, u32, u64, u128);
#[cfg(target_pointer_width = "64")]
mod ptr_try_from_impls {
use super::TryFromIntError;
- use convert::{Infallible, TryFrom};
+ use convert::TryFrom;
try_from_upper_bounded!(usize, u8, u16, u32);
try_from_unbounded!(usize, u64, u128);
impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { u8, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { u8, u128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { u8, u128, #[stable(feature = "i128", since = "1.26.0")] }
impl_from! { u8, usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { u16, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { u16, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { u16, u128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { u16, u128, #[stable(feature = "i128", since = "1.26.0")] }
impl_from! { u32, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { u32, u128, #[unstable(feature = "i128", issue = "35118")] }
-impl_from! { u64, u128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { u32, u128, #[stable(feature = "i128", since = "1.26.0")] }
+impl_from! { u64, u128, #[stable(feature = "i128", since = "1.26.0")] }
// Signed -> Signed
impl_from! { i8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { i8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { i8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { i8, i128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { i8, i128, #[stable(feature = "i128", since = "1.26.0")] }
impl_from! { i8, isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { i16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { i16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { i16, i128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { i16, i128, #[stable(feature = "i128", since = "1.26.0")] }
impl_from! { i32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { i32, i128, #[unstable(feature = "i128", issue = "35118")] }
-impl_from! { i64, i128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { i32, i128, #[stable(feature = "i128", since = "1.26.0")] }
+impl_from! { i64, i128, #[stable(feature = "i128", since = "1.26.0")] }
// Unsigned -> Signed
impl_from! { u8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { u8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { u8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { u8, i128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { u8, i128, #[stable(feature = "i128", since = "1.26.0")] }
impl_from! { u16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
impl_from! { u16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { u16, i128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { u16, i128, #[stable(feature = "i128", since = "1.26.0")] }
impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
-impl_from! { u32, i128, #[unstable(feature = "i128", issue = "35118")] }
-impl_from! { u64, i128, #[unstable(feature = "i128", issue = "35118")] }
+impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
+impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
// Note: integers can only be represented with full precision in a float if
// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.