Skip to content

Commit a2f878a

Browse files
committed
Replace Box::{from,into}_unique with {from,into}_nonnull_raw
Thew `_raw` prefix is included because the fact that `Box`’s ownership semantics are "dissolved" or recreated seem more important than the exact parameter type or return type.
1 parent fb03a49 commit a2f878a

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

src/liballoc/arc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<T> Arc<T> {
286286
weak: atomic::AtomicUsize::new(1),
287287
data,
288288
};
289-
Arc { ptr: NonNull::from(Box::into_unique(x)), phantom: PhantomData }
289+
Arc { ptr: Box::into_nonnull_raw(x), phantom: PhantomData }
290290
}
291291

292292
/// Returns the contained value, if the `Arc` has exactly one strong reference.
@@ -991,11 +991,11 @@ impl<T> Weak<T> {
991991
pub fn new() -> Weak<T> {
992992
unsafe {
993993
Weak {
994-
ptr: NonNull::from(Box::into_unique(box ArcInner {
994+
ptr: Box::into_nonnull_raw(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

Lines changed: 27 additions & 20 deletions
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,38 @@ 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))
272+
Box(Unique::new_unchecked(raw))
273273
}
274274

275-
/// Constructs a `Box` from a `Unique<T>` pointer.
275+
/// Constructs a `Box` from a `NonNull<T>` pointer.
276276
///
277277
/// After calling this function, the memory is owned by a `Box` and `T` can
278278
/// then be destroyed and released upon drop.
279279
///
280280
/// # Safety
281281
///
282-
/// A `Unique<T>` can be safely created via [`Unique::new`] and thus doesn't
282+
/// A `NonNull<T>` can be safely created via [`NonNull::new`] and thus doesn't
283283
/// necessarily own the data pointed to nor is the data guaranteed to live
284284
/// as long as the pointer.
285285
///
286-
/// [`Unique::new`]: ../../core/ptr/struct.Unique.html#method.new
286+
/// [`NonNull::new`]: ../../core/ptr/struct.NonNull.html#method.new
287287
///
288288
/// # Examples
289289
///
290290
/// ```
291-
/// #![feature(unique)]
291+
/// #![feature(nonnull)]
292292
///
293293
/// fn main() {
294294
/// let x = Box::new(5);
295-
/// let ptr = Box::into_unique(x);
296-
/// let x = unsafe { Box::from_unique(ptr) };
295+
/// let ptr = Box::into_nonnull_raw(x);
296+
/// let x = unsafe { Box::from_nonnull_raw(ptr) };
297297
/// }
298298
/// ```
299-
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
299+
#[unstable(feature = "nonnull", reason = "needs an RFC to flesh out design",
300300
issue = "27730")]
301301
#[inline]
302-
pub unsafe fn from_unique(u: Unique<T>) -> Self {
303-
Box(u)
302+
pub unsafe fn from_nonnull_raw(u: NonNull<T>) -> Self {
303+
Box(u.into())
304304
}
305305

306306
/// Consumes the `Box`, returning the wrapped raw pointer.
@@ -326,41 +326,48 @@ impl<T: ?Sized> Box<T> {
326326
#[stable(feature = "box_raw", since = "1.4.0")]
327327
#[inline]
328328
pub fn into_raw(b: Box<T>) -> *mut T {
329-
Box::into_unique(b).as_ptr()
329+
Box::into_nonnull_raw(b).as_ptr()
330330
}
331331

332-
/// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`.
332+
/// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
333333
///
334334
/// After calling this function, the caller is responsible for the
335335
/// memory previously managed by the `Box`. In particular, the
336336
/// caller should properly destroy `T` and release the memory. The
337-
/// proper way to do so is to either convert the `Unique<T>` pointer:
337+
/// proper way to do so is to either convert the `NonNull<T>` pointer:
338338
///
339-
/// - Into a `Box` with the [`Box::from_unique`] function.
339+
/// - Into a `Box` with the [`Box::from_nonnull_raw`] function.
340340
///
341341
/// - Into a raw pointer and back into a `Box` with the [`Box::from_raw`]
342342
/// function.
343343
///
344344
/// 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
345+
/// to call it as `Box::into_nonnull_raw(b)`
346+
/// instead of `b.into_nonnull_raw()`. This
346347
/// is so that there is no conflict with a method on the inner type.
347348
///
348-
/// [`Box::from_unique`]: struct.Box.html#method.from_unique
349+
/// [`Box::from_nonnull_raw`]: struct.Box.html#method.from_nonnull_raw
349350
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
350351
///
351352
/// # Examples
352353
///
353354
/// ```
354-
/// #![feature(unique)]
355+
/// #![feature(nonnull)]
355356
///
356357
/// fn main() {
357358
/// let x = Box::new(5);
358-
/// let ptr = Box::into_unique(x);
359+
/// let ptr = Box::into_nonnull_raw(x);
359360
/// }
360361
/// ```
361-
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
362+
#[unstable(feature = "nonnull", reason = "needs an RFC to flesh out design",
362363
issue = "27730")]
363364
#[inline]
365+
pub fn into_nonnull_raw(b: Box<T>) -> NonNull<T> {
366+
Box::into_unique(b).into()
367+
}
368+
369+
#[unstable(feature = "ptr_internals", issue = "0", reason = "use into_nonnull_raw instead")]
370+
#[inline]
364371
pub fn into_unique(b: Box<T>) -> Unique<T> {
365372
let unique = b.0;
366373
mem::forget(b);

src/liballoc/linked_list.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<T> LinkedList<T> {
157157
unsafe {
158158
node.next = self.head;
159159
node.prev = None;
160-
let node = Some(NonNull::from(Box::into_unique(node)));
160+
let node = Some(Box::into_nonnull_raw(node));
161161

162162
match self.head {
163163
None => self.tail = node,
@@ -192,7 +192,7 @@ impl<T> LinkedList<T> {
192192
unsafe {
193193
node.next = None;
194194
node.prev = self.tail;
195-
let node = Some(NonNull::from(Box::into_unique(node)));
195+
let node = Some(Box::into_nonnull_raw(node));
196196

197197
match self.tail {
198198
None => self.head = node,
@@ -986,11 +986,11 @@ impl<'a, T> IterMut<'a, T> {
986986
Some(prev) => prev,
987987
};
988988

989-
let node = Some(NonNull::from(Box::into_unique(box Node {
989+
let node = Some(Box::into_nonnull_raw(box Node {
990990
next: Some(head),
991991
prev: Some(prev),
992992
element,
993-
})));
993+
}));
994994

995995
prev.as_mut().next = node;
996996
head.as_mut().prev = node;

src/liballoc/rc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,11 @@ impl<T> Rc<T> {
311311
// pointers, which ensures that the weak destructor never frees
312312
// the allocation while the strong destructor is running, even
313313
// if the weak pointer is stored inside the strong one.
314-
ptr: NonNull::from(Box::into_unique(box RcBox {
314+
ptr: Box::into_nonnull_raw(box RcBox {
315315
strong: Cell::new(1),
316316
weak: Cell::new(1),
317317
value,
318-
})),
318+
}),
319319
phantom: PhantomData,
320320
}
321321
}
@@ -1190,11 +1190,11 @@ impl<T> Weak<T> {
11901190
pub fn new() -> Weak<T> {
11911191
unsafe {
11921192
Weak {
1193-
ptr: NonNull::from(Box::into_unique(box RcBox {
1193+
ptr: Box::into_nonnull_raw(box RcBox {
11941194
strong: Cell::new(0),
11951195
weak: Cell::new(1),
11961196
value: uninitialized(),
1197-
})),
1197+
}),
11981198
}
11991199
}
12001200
}

src/test/rustdoc-js/from_u.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const EXPECTED = {
1515
{ 'path': 'std::char', 'name': 'from_u32' },
1616
{ 'path': 'std::str', 'name': 'from_utf8' },
1717
{ 'path': 'std::string::String', 'name': 'from_utf8' },
18-
{ 'path': 'std::boxed::Box', 'name': 'from_unique' },
1918
{ 'path': 'std::i32', 'name': 'from_unsigned' },
2019
{ 'path': 'std::i128', 'name': 'from_unsigned' },
2120
],

0 commit comments

Comments
 (0)