Skip to content

Commit bdda8d6

Browse files
committed
Auto merge of #46952 - SimonSapin:nonnull, r=alexcrichton
Rename std::ptr::Shared to NonNull and stabilize it This implements the changes proposed at #27730 (comment): > * Rename `Shared<T>` to `NonNull<T>` and stabilize it. (Being in the `ptr` module is enough to say that it’s a pointer. I’m not very attached to this specific name though.) > * Rename `Box<T>` methods ~~`from_unique`~~/`into_unique` to ~~`from_nonnull`~~/`into_nonnull` (or whatever names are deemed appropriate), replace `Unique<T>` with `NonNull<T>` in their signatures, and stabilize them. > * Replace `Unique<T>` with `NonNull<T>` in the signatures of methods of the `Alloc` trait. > * Mark `Unique` “permanently-unstable” by replacing remaining occurrences of `#[unstable(feature = "unique", issue = "27730")]` with: > > ```rust > #[unstable(feature = "ptr_internals", issue = "0", reason = "\ > use NonNull instead and consider PhantomData<T> (if you also use #[may_dangle]), \ > Send, and/or Sync")] > ``` > > (Maybe the `reason` string is only useful on the struct definition.) Ideally it would be made private to some crate instead, but it needs to be used in both liballoc and libstd. > * (Leave `NonZero` and `Zeroable` unstable for now, and subject to future bikeshedding.)
2 parents 816d765 + 602a445 commit bdda8d6

File tree

22 files changed

+182
-197
lines changed

22 files changed

+182
-197
lines changed

src/doc/nomicon

src/liballoc/allocator.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::cmp;
1919
use core::fmt;
2020
use core::mem;
2121
use core::usize;
22-
use core::ptr::{self, Unique};
22+
use core::ptr::{self, NonNull};
2323

2424
/// Represents the combination of a starting address and
2525
/// a total capacity of the returned block.
@@ -895,12 +895,12 @@ pub unsafe trait Alloc {
895895
/// Clients wishing to abort computation in response to an
896896
/// allocation error are encouraged to call the allocator's `oom`
897897
/// method, rather than directly invoking `panic!` or similar.
898-
fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
898+
fn alloc_one<T>(&mut self) -> Result<NonNull<T>, AllocErr>
899899
where Self: Sized
900900
{
901901
let k = Layout::new::<T>();
902902
if k.size() > 0 {
903-
unsafe { self.alloc(k).map(|p| Unique::new_unchecked(p as *mut T)) }
903+
unsafe { self.alloc(k).map(|p| NonNull::new_unchecked(p as *mut T)) }
904904
} else {
905905
Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
906906
}
@@ -923,7 +923,7 @@ pub unsafe trait Alloc {
923923
/// * `ptr` must denote a block of memory currently allocated via this allocator
924924
///
925925
/// * the layout of `T` must *fit* that block of memory.
926-
unsafe fn dealloc_one<T>(&mut self, ptr: Unique<T>)
926+
unsafe fn dealloc_one<T>(&mut self, ptr: NonNull<T>)
927927
where Self: Sized
928928
{
929929
let raw_ptr = ptr.as_ptr() as *mut u8;
@@ -963,15 +963,15 @@ pub unsafe trait Alloc {
963963
/// Clients wishing to abort computation in response to an
964964
/// allocation error are encouraged to call the allocator's `oom`
965965
/// method, rather than directly invoking `panic!` or similar.
966-
fn alloc_array<T>(&mut self, n: usize) -> Result<Unique<T>, AllocErr>
966+
fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, AllocErr>
967967
where Self: Sized
968968
{
969969
match Layout::array::<T>(n) {
970970
Some(ref layout) if layout.size() > 0 => {
971971
unsafe {
972972
self.alloc(layout.clone())
973973
.map(|p| {
974-
Unique::new_unchecked(p as *mut T)
974+
NonNull::new_unchecked(p as *mut T)
975975
})
976976
}
977977
}
@@ -1012,15 +1012,15 @@ pub unsafe trait Alloc {
10121012
/// reallocation error are encouraged to call the allocator's `oom`
10131013
/// method, rather than directly invoking `panic!` or similar.
10141014
unsafe fn realloc_array<T>(&mut self,
1015-
ptr: Unique<T>,
1015+
ptr: NonNull<T>,
10161016
n_old: usize,
1017-
n_new: usize) -> Result<Unique<T>, AllocErr>
1017+
n_new: usize) -> Result<NonNull<T>, AllocErr>
10181018
where Self: Sized
10191019
{
10201020
match (Layout::array::<T>(n_old), Layout::array::<T>(n_new), ptr.as_ptr()) {
10211021
(Some(ref k_old), Some(ref k_new), ptr) if k_old.size() > 0 && k_new.size() > 0 => {
10221022
self.realloc(ptr as *mut u8, k_old.clone(), k_new.clone())
1023-
.map(|p|Unique::new_unchecked(p as *mut T))
1023+
.map(|p| NonNull::new_unchecked(p as *mut T))
10241024
}
10251025
_ => {
10261026
Err(AllocErr::invalid_input("invalid layout for realloc_array"))
@@ -1048,7 +1048,7 @@ pub unsafe trait Alloc {
10481048
/// constraints.
10491049
///
10501050
/// Always returns `Err` on arithmetic overflow.
1051-
unsafe fn dealloc_array<T>(&mut self, ptr: Unique<T>, n: usize) -> Result<(), AllocErr>
1051+
unsafe fn dealloc_array<T>(&mut self, ptr: NonNull<T>, n: usize) -> Result<(), AllocErr>
10521052
where Self: Sized
10531053
{
10541054
let raw_ptr = ptr.as_ptr() as *mut u8;

src/liballoc/arc.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::intrinsics::abort;
2525
use core::mem::{self, align_of_val, size_of_val, uninitialized};
2626
use core::ops::Deref;
2727
use core::ops::CoerceUnsized;
28-
use core::ptr::{self, Shared};
28+
use core::ptr::{self, NonNull};
2929
use core::marker::{Unsize, PhantomData};
3030
use core::hash::{Hash, Hasher};
3131
use core::{isize, usize};
@@ -197,7 +197,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
197197
/// [rc_examples]: ../../std/rc/index.html#examples
198198
#[stable(feature = "rust1", since = "1.0.0")]
199199
pub struct Arc<T: ?Sized> {
200-
ptr: Shared<ArcInner<T>>,
200+
ptr: NonNull<ArcInner<T>>,
201201
phantom: PhantomData<T>,
202202
}
203203

@@ -234,7 +234,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
234234
/// [`None`]: ../../std/option/enum.Option.html#variant.None
235235
#[stable(feature = "arc_weak", since = "1.4.0")]
236236
pub struct Weak<T: ?Sized> {
237-
ptr: Shared<ArcInner<T>>,
237+
ptr: NonNull<ArcInner<T>>,
238238
}
239239

240240
#[stable(feature = "arc_weak", since = "1.4.0")]
@@ -286,7 +286,7 @@ impl<T> Arc<T> {
286286
weak: atomic::AtomicUsize::new(1),
287287
data,
288288
};
289-
Arc { ptr: Shared::from(Box::into_unique(x)), phantom: PhantomData }
289+
Arc { ptr: Box::into_raw_non_null(x), phantom: PhantomData }
290290
}
291291

292292
/// Returns the contained value, if the `Arc` has exactly one strong reference.
@@ -397,7 +397,7 @@ impl<T: ?Sized> Arc<T> {
397397
let arc_ptr = set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset));
398398

399399
Arc {
400-
ptr: Shared::new_unchecked(arc_ptr),
400+
ptr: NonNull::new_unchecked(arc_ptr),
401401
phantom: PhantomData,
402402
}
403403
}
@@ -582,7 +582,7 @@ impl<T: ?Sized> Arc<T> {
582582
// Free the allocation without dropping its contents
583583
box_free(bptr);
584584

585-
Arc { ptr: Shared::new_unchecked(ptr), phantom: PhantomData }
585+
Arc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
586586
}
587587
}
588588
}
@@ -609,7 +609,7 @@ impl<T> Arc<[T]> {
609609
&mut (*ptr).data as *mut [T] as *mut T,
610610
v.len());
611611

612-
Arc { ptr: Shared::new_unchecked(ptr), phantom: PhantomData }
612+
Arc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
613613
}
614614
}
615615

@@ -669,7 +669,7 @@ impl<T: Clone> ArcFromSlice<T> for Arc<[T]> {
669669
// All clear. Forget the guard so it doesn't free the new ArcInner.
670670
mem::forget(guard);
671671

672-
Arc { ptr: Shared::new_unchecked(ptr), phantom: PhantomData }
672+
Arc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
673673
}
674674
}
675675
}
@@ -991,11 +991,11 @@ impl<T> Weak<T> {
991991
pub fn new() -> Weak<T> {
992992
unsafe {
993993
Weak {
994-
ptr: Shared::from(Box::into_unique(box ArcInner {
994+
ptr: Box::into_raw_non_null(box ArcInner {
995995
strong: atomic::AtomicUsize::new(0),
996996
weak: atomic::AtomicUsize::new(1),
997997
data: uninitialized(),
998-
})),
998+
}),
999999
}
10001000
}
10011001
}

src/liballoc/boxed.rs

+18-47
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use core::marker::{self, Unsize};
6868
use core::mem;
6969
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
7070
use core::ops::{BoxPlace, Boxed, InPlace, Place, Placer};
71-
use core::ptr::{self, Unique};
71+
use core::ptr::{self, NonNull, Unique};
7272
use core::convert::From;
7373
use str::from_boxed_utf8_unchecked;
7474

@@ -269,38 +269,7 @@ impl<T: ?Sized> Box<T> {
269269
#[stable(feature = "box_raw", since = "1.4.0")]
270270
#[inline]
271271
pub unsafe fn from_raw(raw: *mut T) -> Self {
272-
Box::from_unique(Unique::new_unchecked(raw))
273-
}
274-
275-
/// Constructs a `Box` from a `Unique<T>` pointer.
276-
///
277-
/// After calling this function, the memory is owned by a `Box` and `T` can
278-
/// then be destroyed and released upon drop.
279-
///
280-
/// # Safety
281-
///
282-
/// A `Unique<T>` can be safely created via [`Unique::new`] and thus doesn't
283-
/// necessarily own the data pointed to nor is the data guaranteed to live
284-
/// as long as the pointer.
285-
///
286-
/// [`Unique::new`]: ../../core/ptr/struct.Unique.html#method.new
287-
///
288-
/// # Examples
289-
///
290-
/// ```
291-
/// #![feature(unique)]
292-
///
293-
/// fn main() {
294-
/// let x = Box::new(5);
295-
/// let ptr = Box::into_unique(x);
296-
/// let x = unsafe { Box::from_unique(ptr) };
297-
/// }
298-
/// ```
299-
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
300-
issue = "27730")]
301-
#[inline]
302-
pub unsafe fn from_unique(u: Unique<T>) -> Self {
303-
Box(u)
272+
Box(Unique::new_unchecked(raw))
304273
}
305274

306275
/// Consumes the `Box`, returning the wrapped raw pointer.
@@ -326,40 +295,42 @@ impl<T: ?Sized> Box<T> {
326295
#[stable(feature = "box_raw", since = "1.4.0")]
327296
#[inline]
328297
pub fn into_raw(b: Box<T>) -> *mut T {
329-
Box::into_unique(b).as_ptr()
298+
Box::into_raw_non_null(b).as_ptr()
330299
}
331300

332-
/// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`.
301+
/// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
333302
///
334303
/// After calling this function, the caller is responsible for the
335304
/// memory previously managed by the `Box`. In particular, the
336305
/// caller should properly destroy `T` and release the memory. The
337-
/// proper way to do so is to either convert the `Unique<T>` pointer:
338-
///
339-
/// - Into a `Box` with the [`Box::from_unique`] function.
340-
///
341-
/// - Into a raw pointer and back into a `Box` with the [`Box::from_raw`]
342-
/// function.
306+
/// proper way to do so is to convert the `NonNull<T>` pointer
307+
/// into a raw pointer and back into a `Box` with the [`Box::from_raw`]
308+
/// function.
343309
///
344310
/// Note: this is an associated function, which means that you have
345-
/// to call it as `Box::into_unique(b)` instead of `b.into_unique()`. This
311+
/// to call it as `Box::into_raw_non_null(b)`
312+
/// instead of `b.into_raw_non_null()`. This
346313
/// is so that there is no conflict with a method on the inner type.
347314
///
348-
/// [`Box::from_unique`]: struct.Box.html#method.from_unique
349315
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
350316
///
351317
/// # Examples
352318
///
353319
/// ```
354-
/// #![feature(unique)]
320+
/// #![feature(box_into_raw_non_null)]
355321
///
356322
/// fn main() {
357323
/// let x = Box::new(5);
358-
/// let ptr = Box::into_unique(x);
324+
/// let ptr = Box::into_raw_non_null(x);
359325
/// }
360326
/// ```
361-
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
362-
issue = "27730")]
327+
#[unstable(feature = "box_into_raw_non_null", issue = "47336")]
328+
#[inline]
329+
pub fn into_raw_non_null(b: Box<T>) -> NonNull<T> {
330+
Box::into_unique(b).into()
331+
}
332+
333+
#[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")]
363334
#[inline]
364335
pub fn into_unique(b: Box<T>) -> Unique<T> {
365336
let unique = b.0;

src/liballoc/heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ unsafe impl Alloc for Heap {
232232
///
233233
/// This preserves the non-null invariant for types like `Box<T>`. The address
234234
/// may overlap with non-zero-size memory allocations.
235-
#[rustc_deprecated(since = "1.19", reason = "Use Unique/Shared::empty() instead")]
235+
#[rustc_deprecated(since = "1.19", reason = "Use Unique/NonNull::empty() instead")]
236236
#[unstable(feature = "heap_api", issue = "27700")]
237237
pub const EMPTY: *mut () = 1 as *mut ();
238238

src/liballoc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#![cfg_attr(test, feature(rand, test))]
8585
#![feature(allow_internal_unstable)]
8686
#![feature(ascii_ctype)]
87+
#![feature(box_into_raw_non_null)]
8788
#![feature(box_patterns)]
8889
#![feature(box_syntax)]
8990
#![feature(cfg_target_has_atomic)]
@@ -109,8 +110,8 @@
109110
#![feature(pattern)]
110111
#![feature(placement_in_syntax)]
111112
#![feature(placement_new_protocol)]
113+
#![feature(ptr_internals)]
112114
#![feature(rustc_attrs)]
113-
#![feature(shared)]
114115
#![feature(slice_get_slice)]
115116
#![feature(slice_patterns)]
116117
#![feature(slice_rsplit)]
@@ -120,7 +121,6 @@
120121
#![feature(trusted_len)]
121122
#![feature(unboxed_closures)]
122123
#![feature(unicode)]
123-
#![feature(unique)]
124124
#![feature(unsize)]
125125
#![feature(allocator_internals)]
126126
#![feature(on_unimplemented)]

0 commit comments

Comments
 (0)