1
Fork 0

Rollup merge of #50808 - SimonSapin:nonzero, r=alexcrichton

Stabilize num::NonZeroU*

Tracking issue: https://github.com/rust-lang/rust/issues/49137
This commit is contained in:
kennytm 2018-05-17 03:07:52 +08:00
commit 02aedec722
No known key found for this signature in database
GPG key ID: FEF6C8051D0E013C
14 changed files with 18 additions and 150 deletions

View file

@ -102,7 +102,6 @@
#![feature(lang_items)] #![feature(lang_items)]
#![feature(libc)] #![feature(libc)]
#![feature(needs_allocator)] #![feature(needs_allocator)]
#![feature(nonzero)]
#![feature(optin_builtin_traits)] #![feature(optin_builtin_traits)]
#![feature(pattern)] #![feature(pattern)]
#![feature(pin)] #![feature(pin)]

View file

@ -171,7 +171,6 @@ pub mod prelude;
pub mod intrinsics; pub mod intrinsics;
pub mod mem; pub mod mem;
pub mod nonzero;
pub mod ptr; pub mod ptr;
pub mod hint; pub mod hint;
@ -221,6 +220,7 @@ pub mod heap {
// note: does not need to be public // note: does not need to be public
mod iter_private; mod iter_private;
mod nonzero;
mod tuple; mod tuple;
mod unit; mod unit;

View file

@ -9,103 +9,13 @@
// except according to those terms. // except according to those terms.
//! Exposes the NonZero lang item which provides optimization hints. //! Exposes the NonZero lang item which provides optimization hints.
#![unstable(feature = "nonzero", reason = "deprecated", issue = "49137")]
#![rustc_deprecated(reason = "use `std::ptr::NonNull` or `std::num::NonZero*` instead",
since = "1.26.0")]
#![allow(deprecated)]
use ops::CoerceUnsized; use ops::CoerceUnsized;
/// Unsafe trait to indicate what types are usable with the NonZero struct
pub unsafe trait Zeroable {
/// Whether this value is zero
fn is_zero(&self) -> bool;
}
macro_rules! impl_zeroable_for_pointer_types {
( $( $Ptr: ty )+ ) => {
$(
/// For fat pointers to be considered "zero", only the "data" part needs to be null.
unsafe impl<T: ?Sized> Zeroable for $Ptr {
#[inline]
fn is_zero(&self) -> bool {
(*self).is_null()
}
}
)+
}
}
macro_rules! impl_zeroable_for_integer_types {
( $( $Int: ty )+ ) => {
$(
unsafe impl Zeroable for $Int {
#[inline]
fn is_zero(&self) -> bool {
*self == 0
}
}
)+
}
}
impl_zeroable_for_pointer_types! {
*const T
*mut T
}
impl_zeroable_for_integer_types! {
usize u8 u16 u32 u64 u128
isize i8 i16 i32 i64 i128
}
/// A wrapper type for raw pointers and integers that will never be /// A wrapper type for raw pointers and integers that will never be
/// NULL or 0 that might allow certain optimizations. /// NULL or 0 that might allow certain optimizations.
#[lang = "non_zero"] #[lang = "non_zero"]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct NonZero<T: Zeroable>(pub(crate) T); pub(crate) struct NonZero<T>(pub(crate) T);
impl<T: Zeroable> NonZero<T> { impl<T: CoerceUnsized<U>, U> CoerceUnsized<NonZero<U>> for NonZero<T> {}
/// Creates an instance of NonZero with the provided value.
/// You must indeed ensure that the value is actually "non-zero".
#[inline]
pub const unsafe fn new_unchecked(inner: T) -> Self {
NonZero(inner)
}
/// Creates an instance of NonZero with the provided value.
#[inline]
pub fn new(inner: T) -> Option<Self> {
if inner.is_zero() {
None
} else {
Some(NonZero(inner))
}
}
/// Gets the inner value.
pub fn get(self) -> T {
self.0
}
}
impl<T: Zeroable+CoerceUnsized<U>, U: Zeroable> CoerceUnsized<NonZero<U>> for NonZero<T> {}
impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*mut T> {
fn from(reference: &'a mut T) -> Self {
NonZero(reference)
}
}
impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*const T> {
fn from(reference: &'a mut T) -> Self {
let ptr: *mut T = reference;
NonZero(ptr)
}
}
impl<'a, T: ?Sized> From<&'a T> for NonZero<*const T> {
fn from(reference: &'a T) -> Self {
NonZero(reference)
}
}

View file

@ -16,15 +16,14 @@ use convert::TryFrom;
use fmt; use fmt;
use intrinsics; use intrinsics;
use mem; use mem;
#[allow(deprecated)] use nonzero::NonZero; use nonzero::NonZero;
use ops; use ops;
use str::FromStr; use str::FromStr;
macro_rules! impl_nonzero_fmt { macro_rules! impl_nonzero_fmt {
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => { ( ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
$( $(
#[$stability] #[stable(feature = "nonzero", since = "1.28.0")]
#[allow(deprecated)]
impl fmt::$Trait for $Ty { impl fmt::$Trait for $Ty {
#[inline] #[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -36,7 +35,7 @@ macro_rules! impl_nonzero_fmt {
} }
macro_rules! nonzero_integers { macro_rules! nonzero_integers {
( #[$stability: meta] #[$deprecation: meta] $( $Ty: ident($Int: ty); )+ ) => { ( $( $Ty: ident($Int: ty); )+ ) => {
$( $(
/// An integer that is known not to equal zero. /// An integer that is known not to equal zero.
/// ///
@ -47,27 +46,24 @@ macro_rules! nonzero_integers {
/// use std::mem::size_of; /// use std::mem::size_of;
/// assert_eq!(size_of::<Option<std::num::NonZeroU32>>(), size_of::<u32>()); /// assert_eq!(size_of::<Option<std::num::NonZeroU32>>(), size_of::<u32>());
/// ``` /// ```
#[$stability] #[stable(feature = "nonzero", since = "1.28.0")]
#[$deprecation]
#[allow(deprecated)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct $Ty(NonZero<$Int>); pub struct $Ty(NonZero<$Int>);
#[allow(deprecated)]
impl $Ty { impl $Ty {
/// Create a non-zero without checking the value. /// Create a non-zero without checking the value.
/// ///
/// # Safety /// # Safety
/// ///
/// The value must not be zero. /// The value must not be zero.
#[$stability] #[stable(feature = "nonzero", since = "1.28.0")]
#[inline] #[inline]
pub const unsafe fn new_unchecked(n: $Int) -> Self { pub const unsafe fn new_unchecked(n: $Int) -> Self {
$Ty(NonZero(n)) $Ty(NonZero(n))
} }
/// Create a non-zero if the given value is not zero. /// Create a non-zero if the given value is not zero.
#[$stability] #[stable(feature = "nonzero", since = "1.28.0")]
#[inline] #[inline]
pub fn new(n: $Int) -> Option<Self> { pub fn new(n: $Int) -> Option<Self> {
if n != 0 { if n != 0 {
@ -78,7 +74,7 @@ macro_rules! nonzero_integers {
} }
/// Returns the value as a primitive type. /// Returns the value as a primitive type.
#[$stability] #[stable(feature = "nonzero", since = "1.28.0")]
#[inline] #[inline]
pub fn get(self) -> $Int { pub fn get(self) -> $Int {
self.0 .0 self.0 .0
@ -87,7 +83,6 @@ macro_rules! nonzero_integers {
} }
impl_nonzero_fmt! { impl_nonzero_fmt! {
#[$stability]
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
} }
)+ )+
@ -95,8 +90,6 @@ macro_rules! nonzero_integers {
} }
nonzero_integers! { nonzero_integers! {
#[unstable(feature = "nonzero", issue = "49137")]
#[allow(deprecated)] // Redundant, works around "error: inconsistent lockstep iteration"
NonZeroU8(u8); NonZeroU8(u8);
NonZeroU16(u16); NonZeroU16(u16);
NonZeroU32(u32); NonZeroU32(u32);
@ -105,19 +98,6 @@ nonzero_integers! {
NonZeroUsize(usize); NonZeroUsize(usize);
} }
nonzero_integers! {
#[unstable(feature = "nonzero", issue = "49137")]
#[rustc_deprecated(since = "1.26.0", reason = "\
signed non-zero integers are considered for removal due to lack of known use cases. \
If youre using them, please comment on https://github.com/rust-lang/rust/issues/49137")]
NonZeroI8(i8);
NonZeroI16(i16);
NonZeroI32(i32);
NonZeroI64(i64);
NonZeroI128(i128);
NonZeroIsize(isize);
}
/// Provides intentionally-wrapped arithmetic on `T`. /// Provides intentionally-wrapped arithmetic on `T`.
/// ///
/// Operations like `+` on `u32` values is intended to never overflow, /// Operations like `+` on `u32` values is intended to never overflow,

View file

@ -23,7 +23,7 @@ use fmt;
use hash; use hash;
use marker::{PhantomData, Unsize}; use marker::{PhantomData, Unsize};
use mem; use mem;
#[allow(deprecated)] use nonzero::NonZero; use nonzero::NonZero;
use cmp::Ordering::{self, Less, Equal, Greater}; use cmp::Ordering::{self, Less, Equal, Greater};
@ -2742,7 +2742,6 @@ impl<T: ?Sized> PartialOrd for *mut T {
#[unstable(feature = "ptr_internals", issue = "0", #[unstable(feature = "ptr_internals", issue = "0",
reason = "use NonNull instead and consider PhantomData<T> \ reason = "use NonNull instead and consider PhantomData<T> \
(if you also use #[may_dangle]), Send, and/or Sync")] (if you also use #[may_dangle]), Send, and/or Sync")]
#[allow(deprecated)]
#[doc(hidden)] #[doc(hidden)]
pub struct Unique<T: ?Sized> { pub struct Unique<T: ?Sized> {
pointer: NonZero<*const T>, pointer: NonZero<*const T>,
@ -2790,7 +2789,6 @@ impl<T: Sized> Unique<T> {
} }
#[unstable(feature = "ptr_internals", issue = "0")] #[unstable(feature = "ptr_internals", issue = "0")]
#[allow(deprecated)]
impl<T: ?Sized> Unique<T> { impl<T: ?Sized> Unique<T> {
/// Creates a new `Unique`. /// Creates a new `Unique`.
/// ///
@ -2855,7 +2853,6 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
} }
#[unstable(feature = "ptr_internals", issue = "0")] #[unstable(feature = "ptr_internals", issue = "0")]
#[allow(deprecated)]
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> { impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
fn from(reference: &'a mut T) -> Self { fn from(reference: &'a mut T) -> Self {
Unique { pointer: NonZero(reference as _), _marker: PhantomData } Unique { pointer: NonZero(reference as _), _marker: PhantomData }
@ -2863,7 +2860,6 @@ impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
} }
#[unstable(feature = "ptr_internals", issue = "0")] #[unstable(feature = "ptr_internals", issue = "0")]
#[allow(deprecated)]
impl<'a, T: ?Sized> From<&'a T> for Unique<T> { impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
fn from(reference: &'a T) -> Self { fn from(reference: &'a T) -> Self {
Unique { pointer: NonZero(reference as _), _marker: PhantomData } Unique { pointer: NonZero(reference as _), _marker: PhantomData }
@ -2896,7 +2892,7 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
/// provide a public API that follows the normal shared XOR mutable rules of Rust. /// provide a public API that follows the normal shared XOR mutable rules of Rust.
#[stable(feature = "nonnull", since = "1.25.0")] #[stable(feature = "nonnull", since = "1.25.0")]
pub struct NonNull<T: ?Sized> { pub struct NonNull<T: ?Sized> {
#[allow(deprecated)] pointer: NonZero<*const T>, pointer: NonZero<*const T>,
} }
/// `NonNull` pointers are not `Send` because the data they reference may be aliased. /// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@ -2923,7 +2919,6 @@ impl<T: Sized> NonNull<T> {
} }
} }
#[allow(deprecated)]
impl<T: ?Sized> NonNull<T> { impl<T: ?Sized> NonNull<T> {
/// Creates a new `NonNull`. /// Creates a new `NonNull`.
/// ///
@ -3054,7 +3049,6 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
} }
#[stable(feature = "nonnull", since = "1.25.0")] #[stable(feature = "nonnull", since = "1.25.0")]
#[allow(deprecated)]
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> { impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
fn from(reference: &'a mut T) -> Self { fn from(reference: &'a mut T) -> Self {
NonNull { pointer: NonZero(reference as _) } NonNull { pointer: NonZero(reference as _) }
@ -3062,7 +3056,6 @@ impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
} }
#[stable(feature = "nonnull", since = "1.25.0")] #[stable(feature = "nonnull", since = "1.25.0")]
#[allow(deprecated)]
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> { impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
fn from(reference: &'a T) -> Self { fn from(reference: &'a T) -> Self {
NonNull { pointer: NonZero(reference as _) } NonNull { pointer: NonZero(reference as _) }

View file

@ -26,7 +26,6 @@
#![feature(iterator_step_by)] #![feature(iterator_step_by)]
#![feature(iterator_flatten)] #![feature(iterator_flatten)]
#![feature(iterator_repeat_with)] #![feature(iterator_repeat_with)]
#![feature(nonzero)]
#![feature(pattern)] #![feature(pattern)]
#![feature(range_is_empty)] #![feature(range_is_empty)]
#![feature(raw)] #![feature(raw)]

View file

@ -56,7 +56,6 @@
#![feature(never_type)] #![feature(never_type)]
#![feature(exhaustive_patterns)] #![feature(exhaustive_patterns)]
#![feature(non_exhaustive)] #![feature(non_exhaustive)]
#![feature(nonzero)]
#![feature(proc_macro_internals)] #![feature(proc_macro_internals)]
#![feature(quote)] #![feature(quote)]
#![feature(optin_builtin_traits)] #![feature(optin_builtin_traits)]

View file

@ -21,7 +21,6 @@
html_root_url = "https://doc.rust-lang.org/nightly/")] html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(collections_range)] #![feature(collections_range)]
#![feature(nonzero)]
#![feature(unboxed_closures)] #![feature(unboxed_closures)]
#![feature(fn_traits)] #![feature(fn_traits)]
#![feature(unsize)] #![feature(unsize)]

View file

@ -30,7 +30,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
#![feature(exhaustive_patterns)] #![feature(exhaustive_patterns)]
#![feature(range_contains)] #![feature(range_contains)]
#![feature(rustc_diagnostic_macros)] #![feature(rustc_diagnostic_macros)]
#![feature(nonzero)]
#![feature(inclusive_range_methods)] #![feature(inclusive_range_methods)]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
#![feature(never_type)] #![feature(never_type)]

View file

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

View file

@ -21,12 +21,8 @@ pub use core::num::{FpCategory, ParseIntError, ParseFloatError, TryFromIntError}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::Wrapping; pub use core::num::Wrapping;
#[unstable(feature = "nonzero", issue = "49137")] #[stable(feature = "nonzero", since = "1.28.0")]
#[allow(deprecated)] pub use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
pub use core::num::{
NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32,
NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize,
};
#[cfg(test)] use fmt; #[cfg(test)] use fmt;
#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem}; #[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};

View file

@ -10,11 +10,9 @@
// https://github.com/rust-lang/rust/issues/41898 // https://github.com/rust-lang/rust/issues/41898
#![feature(nonzero, const_fn)] use std::num::NonZeroU64;
extern crate core;
use core::nonzero::NonZero;
fn main() { fn main() {
const FOO: NonZero<u64> = unsafe { NonZero::new_unchecked(2) }; const FOO: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(2) };
if let FOO = FOO {} if let FOO = FOO {}
} }

View file

@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#![feature(nonzero, core)]
use std::mem::size_of; use std::mem::size_of;
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
use std::ptr::NonNull; use std::ptr::NonNull;

View file

@ -22,7 +22,6 @@
// padding and overall computed sizes can be quite different. // padding and overall computed sizes can be quite different.
#![feature(start)] #![feature(start)]
#![feature(nonzero)]
#![allow(dead_code)] #![allow(dead_code)]
use std::num::NonZeroU32; use std::num::NonZeroU32;