diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 54f35d17974fb2412db5e25bdcb802d350ef96e6..06fbfcecba801d31c2a2c3a34ef12ae10e433884 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -171,7 +171,6 @@ pub mod intrinsics; pub mod mem; -pub mod nonzero; pub mod ptr; pub mod hint; @@ -221,6 +220,7 @@ pub mod heap { // note: does not need to be public mod iter_private; +mod nonzero; mod tuple; mod unit; diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs index 19836d98844e292d7be235601e344664faac182f..ee5230cef8dd9597d89809ec0451bd0397d78fd6 100644 --- a/src/libcore/nonzero.rs +++ b/src/libcore/nonzero.rs @@ -9,103 +9,13 @@ // except according to those terms. //! 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; -/// 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 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 /// NULL or 0 that might allow certain optimizations. #[lang = "non_zero"] -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)] -pub struct NonZero(pub(crate) T); - -impl NonZero { - /// 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 { - if inner.is_zero() { - None - } else { - Some(NonZero(inner)) - } - } - - /// Gets the inner value. - pub fn get(self) -> T { - self.0 - } -} - -impl, U: Zeroable> CoerceUnsized> for NonZero {} - -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) - } -} +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub(crate) struct NonZero(pub(crate) T); -impl<'a, T: ?Sized> From<&'a T> for NonZero<*const T> { - fn from(reference: &'a T) -> Self { - NonZero(reference) - } -} +impl, U> CoerceUnsized> for NonZero {} diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index a062fbda5bad0bcd0add87188743a596f2679a5b..ef914a0fc5cd7fc727d45b08a7d8467151cc4412 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -16,7 +16,7 @@ use fmt; use intrinsics; use mem; -#[allow(deprecated)] use nonzero::NonZero; +use nonzero::NonZero; use ops; use str::FromStr; @@ -49,11 +49,9 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { /// ``` #[$stability] #[$deprecation] - #[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. /// diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 83dfac7a3a2ea4f09b5e25aeb4033acc751c40d9..63bcc024020155fe753716f68dc2d6f1e2e2c046 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -23,7 +23,7 @@ use hash; use marker::{PhantomData, Unsize}; use mem; -#[allow(deprecated)] use nonzero::NonZero; +use nonzero::NonZero; use cmp::Ordering::{self, Less, Equal, Greater}; @@ -2742,7 +2742,6 @@ fn ge(&self, other: &*mut T) -> bool { *self >= *other } #[unstable(feature = "ptr_internals", issue = "0", reason = "use NonNull instead and consider PhantomData \ (if you also use #[may_dangle]), Send, and/or Sync")] -#[allow(deprecated)] #[doc(hidden)] pub struct Unique { pointer: NonZero<*const T>, @@ -2790,7 +2789,6 @@ pub const fn empty() -> Self { } #[unstable(feature = "ptr_internals", issue = "0")] -#[allow(deprecated)] impl Unique { /// Creates a new `Unique`. /// @@ -2855,7 +2853,6 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } #[unstable(feature = "ptr_internals", issue = "0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a mut T> for Unique { fn from(reference: &'a mut T) -> Self { Unique { pointer: NonZero(reference as _), _marker: PhantomData } @@ -2863,7 +2860,6 @@ fn from(reference: &'a mut T) -> Self { } #[unstable(feature = "ptr_internals", issue = "0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a T> for Unique { fn from(reference: &'a T) -> Self { Unique { pointer: NonZero(reference as _), _marker: PhantomData } @@ -2896,7 +2892,7 @@ fn from(p: NonNull) -> Self { /// provide a public API that follows the normal shared XOR mutable rules of Rust. #[stable(feature = "nonnull", since = "1.25.0")] pub struct NonNull { - #[allow(deprecated)] pointer: NonZero<*const T>, + pointer: NonZero<*const T>, } /// `NonNull` pointers are not `Send` because the data they reference may be aliased. @@ -2923,7 +2919,6 @@ pub fn dangling() -> Self { } } -#[allow(deprecated)] impl NonNull { /// Creates a new `NonNull`. /// @@ -3054,7 +3049,6 @@ fn from(unique: Unique) -> Self { } #[stable(feature = "nonnull", since = "1.25.0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a mut T> for NonNull { fn from(reference: &'a mut T) -> Self { NonNull { pointer: NonZero(reference as _) } @@ -3062,7 +3056,6 @@ fn from(reference: &'a mut T) -> Self { } #[stable(feature = "nonnull", since = "1.25.0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a T> for NonNull { fn from(reference: &'a T) -> Self { NonNull { pointer: NonZero(reference as _) } diff --git a/src/test/run-pass/ctfe/tuple-struct-constructors.rs b/src/test/run-pass/ctfe/tuple-struct-constructors.rs index ecc5d3766367949db7f4cc6907fa87b95437cf34..ee1ce192fe03caa252826711583d71d67db13c5a 100644 --- a/src/test/run-pass/ctfe/tuple-struct-constructors.rs +++ b/src/test/run-pass/ctfe/tuple-struct-constructors.rs @@ -10,11 +10,10 @@ // https://github.com/rust-lang/rust/issues/41898 -#![feature(nonzero, const_fn)] -extern crate core; -use core::nonzero::NonZero; +#![feature(nonzero)] +use std::num::NonZeroU64; fn main() { - const FOO: NonZero = unsafe { NonZero::new_unchecked(2) }; + const FOO: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(2) }; if let FOO = FOO {} }