Make core::nonzero private
It is now an implementation detail of ptr::NonNull and num::NonZero*
This commit is contained in:
parent
2a3f5367a2
commit
ee85bfdcc2
5 changed files with 10 additions and 110 deletions
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ 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;
|
||||||
|
|
||||||
|
@ -49,11 +49,9 @@ macro_rules! nonzero_integers {
|
||||||
/// ```
|
/// ```
|
||||||
#[$stability]
|
#[$stability]
|
||||||
#[$deprecation]
|
#[$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.
|
||||||
///
|
///
|
||||||
|
|
|
@ -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 _) }
|
||||||
|
|
|
@ -10,11 +10,10 @@
|
||||||
|
|
||||||
// https://github.com/rust-lang/rust/issues/41898
|
// https://github.com/rust-lang/rust/issues/41898
|
||||||
|
|
||||||
#![feature(nonzero, const_fn)]
|
#![feature(nonzero)]
|
||||||
extern crate core;
|
use std::num::NonZeroU64;
|
||||||
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 {}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue