Skip to content

Commit a803046

Browse files
authored
Rollup merge of rust-lang#48265 - SimonSapin:nonzero, r=KodrAus
Add 12 num::NonZero* types for primitive integers, deprecate core::nonzero RFC: rust-lang/rfcs#2307 Tracking issue: ~~rust-lang#27730 rust-lang#49137 Fixes rust-lang#27730
2 parents 52f7e88 + a23f685 commit a803046

File tree

15 files changed

+210
-89
lines changed

15 files changed

+210
-89
lines changed

src/liballoc/btree/node.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
use core::marker::PhantomData;
4545
use core::mem;
46-
use core::nonzero::NonZero;
47-
use core::ptr::{self, Unique};
46+
use core::ptr::{self, Unique, NonNull};
4847
use core::slice;
4948

5049
use boxed::Box;
@@ -149,14 +148,12 @@ impl<K, V> BoxedNode<K, V> {
149148
}
150149
}
151150

152-
unsafe fn from_ptr(ptr: NonZero<*const LeafNode<K, V>>) -> Self {
153-
BoxedNode { ptr: Unique::new_unchecked(ptr.get() as *mut LeafNode<K, V>) }
151+
unsafe fn from_ptr(ptr: NonNull<LeafNode<K, V>>) -> Self {
152+
BoxedNode { ptr: Unique::from(ptr) }
154153
}
155154

156-
fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> {
157-
unsafe {
158-
NonZero::from(self.ptr.as_ref())
159-
}
155+
fn as_ptr(&self) -> NonNull<LeafNode<K, V>> {
156+
NonNull::from(self.ptr)
160157
}
161158
}
162159

@@ -276,7 +273,7 @@ impl<K, V> Root<K, V> {
276273
/// `NodeRef` could be pointing to either type of node.
277274
pub struct NodeRef<BorrowType, K, V, Type> {
278275
height: usize,
279-
node: NonZero<*const LeafNode<K, V>>,
276+
node: NonNull<LeafNode<K, V>>,
280277
// This is null unless the borrow type is `Mut`
281278
root: *const Root<K, V>,
282279
_marker: PhantomData<(BorrowType, Type)>
@@ -302,15 +299,15 @@ unsafe impl<K: Send, V: Send, Type> Send
302299
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
303300
fn as_internal(&self) -> &InternalNode<K, V> {
304301
unsafe {
305-
&*(self.node.get() as *const InternalNode<K, V>)
302+
&*(self.node.as_ptr() as *mut InternalNode<K, V>)
306303
}
307304
}
308305
}
309306

310307
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
311308
fn as_internal_mut(&mut self) -> &mut InternalNode<K, V> {
312309
unsafe {
313-
&mut *(self.node.get() as *mut InternalNode<K, V>)
310+
&mut *(self.node.as_ptr() as *mut InternalNode<K, V>)
314311
}
315312
}
316313
}
@@ -352,7 +349,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
352349

353350
fn as_leaf(&self) -> &LeafNode<K, V> {
354351
unsafe {
355-
&*self.node.get()
352+
self.node.as_ref()
356353
}
357354
}
358355

@@ -382,7 +379,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
382379
>,
383380
Self
384381
> {
385-
if let Some(non_zero) = NonZero::new(self.as_leaf().parent as *const LeafNode<K, V>) {
382+
let parent_as_leaf = self.as_leaf().parent as *const LeafNode<K, V>;
383+
if let Some(non_zero) = NonNull::new(parent_as_leaf as *mut _) {
386384
Ok(Handle {
387385
node: NodeRef {
388386
height: self.height + 1,
@@ -498,7 +496,7 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
498496

499497
fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> {
500498
unsafe {
501-
&mut *(self.node.get() as *mut LeafNode<K, V>)
499+
self.node.as_mut()
502500
}
503501
}
504502

@@ -1241,12 +1239,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12411239
}
12421240

12431241
Heap.dealloc(
1244-
right_node.node.get() as *mut u8,
1242+
right_node.node.as_ptr() as *mut u8,
12451243
Layout::new::<InternalNode<K, V>>(),
12461244
);
12471245
} else {
12481246
Heap.dealloc(
1249-
right_node.node.get() as *mut u8,
1247+
right_node.node.as_ptr() as *mut u8,
12501248
Layout::new::<LeafNode<K, V>>(),
12511249
);
12521250
}

src/libcore/cell.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,12 @@
146146
//!
147147
//! ```
148148
//! #![feature(core_intrinsics)]
149-
//! #![feature(shared)]
150149
//! use std::cell::Cell;
151-
//! use std::ptr::Shared;
150+
//! use std::ptr::NonNull;
152151
//! use std::intrinsics::abort;
153152
//!
154153
//! struct Rc<T: ?Sized> {
155-
//! ptr: Shared<RcBox<T>>
154+
//! ptr: NonNull<RcBox<T>>
156155
//! }
157156
//!
158157
//! struct RcBox<T: ?Sized> {

src/libcore/nonzero.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
// except according to those terms.
1010

1111
//! Exposes the NonZero lang item which provides optimization hints.
12-
#![unstable(feature = "nonzero",
13-
reason = "needs an RFC to flesh out the design",
14-
issue = "27730")]
12+
#![unstable(feature = "nonzero", reason = "deprecated", issue = "49137")]
13+
#![rustc_deprecated(reason = "use `std::ptr::NonNull` or `std::num::NonZero*` instead",
14+
since = "1.26.0")]
15+
#![allow(deprecated)]
1516

1617
use ops::CoerceUnsized;
1718

@@ -62,14 +63,11 @@ impl_zeroable_for_integer_types! {
6263
/// NULL or 0 that might allow certain optimizations.
6364
#[lang = "non_zero"]
6465
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
65-
pub struct NonZero<T: Zeroable>(T);
66+
pub struct NonZero<T: Zeroable>(pub(crate) T);
6667

6768
impl<T: Zeroable> NonZero<T> {
6869
/// Creates an instance of NonZero with the provided value.
6970
/// You must indeed ensure that the value is actually "non-zero".
70-
#[unstable(feature = "nonzero",
71-
reason = "needs an RFC to flesh out the design",
72-
issue = "27730")]
7371
#[inline]
7472
pub const unsafe fn new_unchecked(inner: T) -> Self {
7573
NonZero(inner)

src/libcore/num/mod.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,98 @@
1515
use convert::TryFrom;
1616
use fmt;
1717
use intrinsics;
18+
#[allow(deprecated)] use nonzero::NonZero;
1819
use ops;
1920
use str::FromStr;
2021

22+
macro_rules! impl_nonzero_fmt {
23+
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
24+
$(
25+
#[$stability]
26+
impl fmt::$Trait for $Ty {
27+
#[inline]
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
self.get().fmt(f)
30+
}
31+
}
32+
)+
33+
}
34+
}
35+
36+
macro_rules! nonzero_integers {
37+
( #[$stability: meta] $( $Ty: ident($Int: ty); )+ ) => {
38+
$(
39+
/// An integer that is known not to equal zero.
40+
///
41+
/// This may enable some memory layout optimization such as:
42+
///
43+
/// ```rust
44+
/// # #![feature(nonzero)]
45+
/// use std::mem::size_of;
46+
/// assert_eq!(size_of::<Option<std::num::NonZeroU32>>(), size_of::<u32>());
47+
/// ```
48+
#[$stability]
49+
#[allow(deprecated)]
50+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
51+
pub struct $Ty(NonZero<$Int>);
52+
53+
#[allow(deprecated)]
54+
impl $Ty {
55+
/// Create a non-zero without checking the value.
56+
///
57+
/// # Safety
58+
///
59+
/// The value must not be zero.
60+
#[$stability]
61+
#[inline]
62+
pub const unsafe fn new_unchecked(n: $Int) -> Self {
63+
$Ty(NonZero(n))
64+
}
65+
66+
/// Create a non-zero if the given value is not zero.
67+
#[$stability]
68+
#[inline]
69+
pub fn new(n: $Int) -> Option<Self> {
70+
if n != 0 {
71+
Some($Ty(NonZero(n)))
72+
} else {
73+
None
74+
}
75+
}
76+
77+
/// Returns the value as a primitive type.
78+
#[$stability]
79+
#[inline]
80+
pub fn get(self) -> $Int {
81+
self.0 .0
82+
}
83+
84+
}
85+
86+
impl_nonzero_fmt! {
87+
#[$stability]
88+
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
89+
}
90+
)+
91+
}
92+
}
93+
94+
nonzero_integers! {
95+
#[unstable(feature = "nonzero", issue = "49137")]
96+
NonZeroU8(u8); NonZeroI8(i8);
97+
NonZeroU16(u16); NonZeroI16(i16);
98+
NonZeroU32(u32); NonZeroI32(i32);
99+
NonZeroU64(u64); NonZeroI64(i64);
100+
NonZeroUsize(usize); NonZeroIsize(isize);
101+
}
102+
103+
nonzero_integers! {
104+
// Change this to `#[unstable(feature = "i128", issue = "35118")]`
105+
// if other NonZero* integer types are stabilizied before 128-bit integers
106+
#[unstable(feature = "nonzero", issue = "49137")]
107+
NonZeroU128(u128); NonZeroI128(i128);
108+
}
109+
21110
/// Provides intentionally-wrapped arithmetic on `T`.
22111
///
23112
/// Operations like `+` on `u32` values is intended to never overflow,

src/libcore/ptr.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use fmt;
2323
use hash;
2424
use marker::{PhantomData, Unsize};
2525
use mem;
26-
use nonzero::NonZero;
26+
#[allow(deprecated)] use nonzero::NonZero;
2727

2828
use cmp::Ordering::{self, Less, Equal, Greater};
2929

@@ -2285,6 +2285,7 @@ impl<T: ?Sized> PartialOrd for *mut T {
22852285
#[unstable(feature = "ptr_internals", issue = "0",
22862286
reason = "use NonNull instead and consider PhantomData<T> \
22872287
(if you also use #[may_dangle]), Send, and/or Sync")]
2288+
#[allow(deprecated)]
22882289
pub struct Unique<T: ?Sized> {
22892290
pointer: NonZero<*const T>,
22902291
// NOTE: this marker has no consequences for variance, but is necessary
@@ -2332,24 +2333,29 @@ impl<T: Sized> Unique<T> {
23322333
}
23332334

23342335
#[unstable(feature = "ptr_internals", issue = "0")]
2336+
#[allow(deprecated)]
23352337
impl<T: ?Sized> Unique<T> {
23362338
/// Creates a new `Unique`.
23372339
///
23382340
/// # Safety
23392341
///
23402342
/// `ptr` must be non-null.
23412343
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
2342-
Unique { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData }
2344+
Unique { pointer: NonZero(ptr as _), _marker: PhantomData }
23432345
}
23442346

23452347
/// Creates a new `Unique` if `ptr` is non-null.
23462348
pub fn new(ptr: *mut T) -> Option<Self> {
2347-
NonZero::new(ptr as *const T).map(|nz| Unique { pointer: nz, _marker: PhantomData })
2349+
if !ptr.is_null() {
2350+
Some(Unique { pointer: NonZero(ptr as _), _marker: PhantomData })
2351+
} else {
2352+
None
2353+
}
23482354
}
23492355

23502356
/// Acquires the underlying `*mut` pointer.
23512357
pub fn as_ptr(self) -> *mut T {
2352-
self.pointer.get() as *mut T
2358+
self.pointer.0 as *mut T
23532359
}
23542360

23552361
/// Dereferences the content.
@@ -2392,16 +2398,18 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
23922398
}
23932399

23942400
#[unstable(feature = "ptr_internals", issue = "0")]
2401+
#[allow(deprecated)]
23952402
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
23962403
fn from(reference: &'a mut T) -> Self {
2397-
Unique { pointer: NonZero::from(reference), _marker: PhantomData }
2404+
Unique { pointer: NonZero(reference as _), _marker: PhantomData }
23982405
}
23992406
}
24002407

24012408
#[unstable(feature = "ptr_internals", issue = "0")]
2409+
#[allow(deprecated)]
24022410
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
24032411
fn from(reference: &'a T) -> Self {
2404-
Unique { pointer: NonZero::from(reference), _marker: PhantomData }
2412+
Unique { pointer: NonZero(reference as _), _marker: PhantomData }
24052413
}
24062414
}
24072415

@@ -2412,11 +2420,6 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
24122420
}
24132421
}
24142422

2415-
/// Previous name of `NonNull`.
2416-
#[rustc_deprecated(since = "1.25.0", reason = "renamed to `NonNull`")]
2417-
#[unstable(feature = "shared", issue = "27730")]
2418-
pub type Shared<T> = NonNull<T>;
2419-
24202423
/// `*mut T` but non-zero and covariant.
24212424
///
24222425
/// This is often the correct thing to use when building data structures using
@@ -2436,7 +2439,7 @@ pub type Shared<T> = NonNull<T>;
24362439
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
24372440
#[stable(feature = "nonnull", since = "1.25.0")]
24382441
pub struct NonNull<T: ?Sized> {
2439-
pointer: NonZero<*const T>,
2442+
#[allow(deprecated)] pointer: NonZero<*const T>,
24402443
}
24412444

24422445
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@@ -2463,6 +2466,7 @@ impl<T: Sized> NonNull<T> {
24632466
}
24642467
}
24652468

2469+
#[allow(deprecated)]
24662470
impl<T: ?Sized> NonNull<T> {
24672471
/// Creates a new `NonNull`.
24682472
///
@@ -2471,19 +2475,23 @@ impl<T: ?Sized> NonNull<T> {
24712475
/// `ptr` must be non-null.
24722476
#[stable(feature = "nonnull", since = "1.25.0")]
24732477
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
2474-
NonNull { pointer: NonZero::new_unchecked(ptr) }
2478+
NonNull { pointer: NonZero(ptr as _) }
24752479
}
24762480

24772481
/// Creates a new `NonNull` if `ptr` is non-null.
24782482
#[stable(feature = "nonnull", since = "1.25.0")]
24792483
pub fn new(ptr: *mut T) -> Option<Self> {
2480-
NonZero::new(ptr as *const T).map(|nz| NonNull { pointer: nz })
2484+
if !ptr.is_null() {
2485+
Some(NonNull { pointer: NonZero(ptr as _) })
2486+
} else {
2487+
None
2488+
}
24812489
}
24822490

24832491
/// Acquires the underlying `*mut` pointer.
24842492
#[stable(feature = "nonnull", since = "1.25.0")]
24852493
pub fn as_ptr(self) -> *mut T {
2486-
self.pointer.get() as *mut T
2494+
self.pointer.0 as *mut T
24872495
}
24882496

24892497
/// Dereferences the content.
@@ -2581,15 +2589,17 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
25812589
}
25822590

25832591
#[stable(feature = "nonnull", since = "1.25.0")]
2592+
#[allow(deprecated)]
25842593
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
25852594
fn from(reference: &'a mut T) -> Self {
2586-
NonNull { pointer: NonZero::from(reference) }
2595+
NonNull { pointer: NonZero(reference as _) }
25872596
}
25882597
}
25892598

25902599
#[stable(feature = "nonnull", since = "1.25.0")]
2600+
#[allow(deprecated)]
25912601
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
25922602
fn from(reference: &'a T) -> Self {
2593-
NonNull { pointer: NonZero::from(reference) }
2603+
NonNull { pointer: NonZero(reference as _) }
25942604
}
25952605
}

0 commit comments

Comments
 (0)