Skip to content

Commit

Permalink
Add Box::into_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jul 22, 2017
1 parent 1ef24bb commit cbd2b6b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/liballoc/arc.rs
Expand Up @@ -280,7 +280,7 @@ impl<T> Arc<T> {
weak: atomic::AtomicUsize::new(1),
data: data,
};
Arc { ptr: unsafe { Shared::new_unchecked(Box::into_raw(x)) } }
Arc { ptr: Shared::from(Box::into_unique(x)) }
}

/// Returns the contained value, if the `Arc` has exactly one strong reference.
Expand Down Expand Up @@ -842,7 +842,7 @@ impl<T> Weak<T> {
pub fn new() -> Weak<T> {
unsafe {
Weak {
ptr: Shared::new_unchecked(Box::into_raw(box ArcInner {
ptr: Shared::from(Box::into_unique(box ArcInner {
strong: atomic::AtomicUsize::new(0),
weak: atomic::AtomicUsize::new(1),
data: uninitialized(),
Expand Down
31 changes: 31 additions & 0 deletions src/liballoc/boxed.rs
Expand Up @@ -297,6 +297,37 @@ impl<T: ?Sized> Box<T> {
pub fn into_raw(b: Box<T>) -> *mut T {
unsafe { mem::transmute(b) }
}

/// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`.
///
/// After calling this function, the caller is responsible for the
/// memory previously managed by the `Box`. In particular, the
/// caller should properly destroy `T` and release the memory. The
/// proper way to do so is to convert the raw pointer back into a
/// `Box` with the [`Box::from_raw`] function.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Box::into_unique(b)` instead of `b.into_unique()`. This
/// is so that there is no conflict with a method on the inner type.
///
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
///
/// # Examples
///
/// ```
/// #![feature(unique)]
///
/// fn main() {
/// let x = Box::new(5);
/// let ptr = Box::into_unique(x);
/// }
/// ```
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
issue = "27730")]
#[inline]
pub fn into_unique(b: Box<T>) -> Unique<T> {
unsafe { mem::transmute(b) }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
4 changes: 1 addition & 3 deletions src/liballoc/btree/node.rs
Expand Up @@ -140,9 +140,7 @@ struct BoxedNode<K, V> {

impl<K, V> BoxedNode<K, V> {
fn from_leaf(node: Box<LeafNode<K, V>>) -> Self {
unsafe {
BoxedNode { ptr: Unique::new_unchecked(Box::into_raw(node)) }
}
BoxedNode { ptr: Box::into_unique(node) }
}

fn from_internal(node: Box<InternalNode<K, V>>) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions src/liballoc/linked_list.rs
Expand Up @@ -157,7 +157,7 @@ impl<T> LinkedList<T> {
unsafe {
node.next = self.head;
node.prev = None;
let node = Some(Shared::new_unchecked(Box::into_raw(node)));
let node = Some(Shared::from(Box::into_unique(node)));

match self.head {
None => self.tail = node,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<T> LinkedList<T> {
unsafe {
node.next = None;
node.prev = self.tail;
let node = Some(Shared::new_unchecked(Box::into_raw(node)));
let node = Some(Shared::from(Box::into_unique(node)));

match self.tail {
None => self.head = node,
Expand Down Expand Up @@ -921,7 +921,7 @@ impl<'a, T> IterMut<'a, T> {
Some(prev) => prev,
};

let node = Some(Shared::new_unchecked(Box::into_raw(box Node {
let node = Some(Shared::from(Box::into_unique(box Node {
next: Some(head),
prev: Some(prev),
element: element,
Expand Down
24 changes: 11 additions & 13 deletions src/liballoc/rc.rs
Expand Up @@ -303,18 +303,16 @@ impl<T> Rc<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new(value: T) -> Rc<T> {
unsafe {
Rc {
// there is an implicit weak pointer owned by all the strong
// pointers, which ensures that the weak destructor never frees
// the allocation while the strong destructor is running, even
// if the weak pointer is stored inside the strong one.
ptr: Shared::new_unchecked(Box::into_raw(box RcBox {
strong: Cell::new(1),
weak: Cell::new(1),
value: value,
})),
}
Rc {
// there is an implicit weak pointer owned by all the strong
// pointers, which ensures that the weak destructor never frees
// the allocation while the strong destructor is running, even
// if the weak pointer is stored inside the strong one.
ptr: Shared::from(Box::into_unique(box RcBox {
strong: Cell::new(1),
weak: Cell::new(1),
value: value,
})),
}
}

Expand Down Expand Up @@ -1016,7 +1014,7 @@ impl<T> Weak<T> {
pub fn new() -> Weak<T> {
unsafe {
Weak {
ptr: Shared::new_unchecked(Box::into_raw(box RcBox {
ptr: Shared::from(Box::into_unique(box RcBox {
strong: Cell::new(0),
weak: Cell::new(1),
value: uninitialized(),
Expand Down

0 comments on commit cbd2b6b

Please sign in to comment.