Add 12 num::NonZero* types for each primitive integer

RFC: https://github.com/rust-lang/rfcs/pull/2307
This commit is contained in:
Simon Sapin 2018-02-16 10:39:24 +01:00
parent adf2135adc
commit f40877feeb
4 changed files with 100 additions and 1 deletions

View File

@ -62,7 +62,7 @@ impl_zeroable_for_integer_types! {
/// NULL or 0 that might allow certain optimizations.
#[lang = "non_zero"]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub struct NonZero<T: Zeroable>(T);
pub struct NonZero<T: Zeroable>(pub(crate) T);
impl<T: Zeroable> NonZero<T> {
/// Creates an instance of NonZero with the provided value.

View File

@ -15,9 +15,96 @@
use convert::{Infallible, TryFrom};
use fmt;
use intrinsics;
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]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct $Ty(NonZero<$Int>);
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 = "27730")]
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 = "27730")]
NonZeroU128(u128); NonZeroI128(i128);
}
/// Provides intentionally-wrapped arithmetic on `T`.
///
/// Operations like `+` on `u32` values is intended to never overflow,

View File

@ -282,6 +282,7 @@
#![feature(macro_vis_matcher)]
#![feature(needs_panic_runtime)]
#![feature(exhaustive_patterns)]
#![feature(nonzero)]
#![feature(num_bits_bytes)]
#![feature(old_wrapping)]
#![feature(on_unimplemented)]

View File

@ -21,6 +21,17 @@ pub use core::num::{FpCategory, ParseIntError, ParseFloatError, TryFromIntError}
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::Wrapping;
#[unstable(feature = "nonzero", issue = "27730")]
pub use core::num::{
NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32,
NonZeroU64, NonZeroI64, NonZeroUsize, NonZeroIsize,
};
// Change this to `#[unstable(feature = "i128", issue = "35118")]`
// if other NonZero* integer types are stabilizied before 128-bit integers
#[unstable(feature = "nonzero", issue = "27730")]
pub use core::num::{NonZeroU128, NonZeroI128};
#[cfg(test)] use fmt;
#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};