Skip to content

Commit cbd2b6b

Browse files
committed
Add Box::into_unique
1 parent 1ef24bb commit cbd2b6b

File tree

5 files changed

+48
-21
lines changed

5 files changed

+48
-21
lines changed

src/liballoc/arc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<T> Arc<T> {
280280
weak: atomic::AtomicUsize::new(1),
281281
data: data,
282282
};
283-
Arc { ptr: unsafe { Shared::new_unchecked(Box::into_raw(x)) } }
283+
Arc { ptr: Shared::from(Box::into_unique(x)) }
284284
}
285285

286286
/// Returns the contained value, if the `Arc` has exactly one strong reference.
@@ -842,7 +842,7 @@ impl<T> Weak<T> {
842842
pub fn new() -> Weak<T> {
843843
unsafe {
844844
Weak {
845-
ptr: Shared::new_unchecked(Box::into_raw(box ArcInner {
845+
ptr: Shared::from(Box::into_unique(box ArcInner {
846846
strong: atomic::AtomicUsize::new(0),
847847
weak: atomic::AtomicUsize::new(1),
848848
data: uninitialized(),

src/liballoc/boxed.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,37 @@ impl<T: ?Sized> Box<T> {
297297
pub fn into_raw(b: Box<T>) -> *mut T {
298298
unsafe { mem::transmute(b) }
299299
}
300+
301+
/// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`.
302+
///
303+
/// After calling this function, the caller is responsible for the
304+
/// memory previously managed by the `Box`. In particular, the
305+
/// caller should properly destroy `T` and release the memory. The
306+
/// proper way to do so is to convert the raw pointer back into a
307+
/// `Box` with the [`Box::from_raw`] function.
308+
///
309+
/// Note: this is an associated function, which means that you have
310+
/// to call it as `Box::into_unique(b)` instead of `b.into_unique()`. This
311+
/// is so that there is no conflict with a method on the inner type.
312+
///
313+
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
314+
///
315+
/// # Examples
316+
///
317+
/// ```
318+
/// #![feature(unique)]
319+
///
320+
/// fn main() {
321+
/// let x = Box::new(5);
322+
/// let ptr = Box::into_unique(x);
323+
/// }
324+
/// ```
325+
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
326+
issue = "27730")]
327+
#[inline]
328+
pub fn into_unique(b: Box<T>) -> Unique<T> {
329+
unsafe { mem::transmute(b) }
330+
}
300331
}
301332

302333
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/btree/node.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ struct BoxedNode<K, V> {
140140

141141
impl<K, V> BoxedNode<K, V> {
142142
fn from_leaf(node: Box<LeafNode<K, V>>) -> Self {
143-
unsafe {
144-
BoxedNode { ptr: Unique::new_unchecked(Box::into_raw(node)) }
145-
}
143+
BoxedNode { ptr: Box::into_unique(node) }
146144
}
147145

148146
fn from_internal(node: Box<InternalNode<K, V>>) -> Self {

src/liballoc/linked_list.rs

Lines changed: 3 additions & 3 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(Shared::new_unchecked(Box::into_raw(node)));
160+
let node = Some(Shared::from(Box::into_unique(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(Shared::new_unchecked(Box::into_raw(node)));
195+
let node = Some(Shared::from(Box::into_unique(node)));
196196

197197
match self.tail {
198198
None => self.head = node,
@@ -921,7 +921,7 @@ impl<'a, T> IterMut<'a, T> {
921921
Some(prev) => prev,
922922
};
923923

924-
let node = Some(Shared::new_unchecked(Box::into_raw(box Node {
924+
let node = Some(Shared::from(Box::into_unique(box Node {
925925
next: Some(head),
926926
prev: Some(prev),
927927
element: element,

src/liballoc/rc.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,16 @@ impl<T> Rc<T> {
303303
/// ```
304304
#[stable(feature = "rust1", since = "1.0.0")]
305305
pub fn new(value: T) -> Rc<T> {
306-
unsafe {
307-
Rc {
308-
// there is an implicit weak pointer owned by all the strong
309-
// pointers, which ensures that the weak destructor never frees
310-
// the allocation while the strong destructor is running, even
311-
// if the weak pointer is stored inside the strong one.
312-
ptr: Shared::new_unchecked(Box::into_raw(box RcBox {
313-
strong: Cell::new(1),
314-
weak: Cell::new(1),
315-
value: value,
316-
})),
317-
}
306+
Rc {
307+
// there is an implicit weak pointer owned by all the strong
308+
// pointers, which ensures that the weak destructor never frees
309+
// the allocation while the strong destructor is running, even
310+
// if the weak pointer is stored inside the strong one.
311+
ptr: Shared::from(Box::into_unique(box RcBox {
312+
strong: Cell::new(1),
313+
weak: Cell::new(1),
314+
value: value,
315+
})),
318316
}
319317
}
320318

@@ -1016,7 +1014,7 @@ impl<T> Weak<T> {
10161014
pub fn new() -> Weak<T> {
10171015
unsafe {
10181016
Weak {
1019-
ptr: Shared::new_unchecked(Box::into_raw(box RcBox {
1017+
ptr: Shared::from(Box::into_unique(box RcBox {
10201018
strong: Cell::new(0),
10211019
weak: Cell::new(1),
10221020
value: uninitialized(),

0 commit comments

Comments
 (0)