diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2017-07-14 12:47:06 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2017-07-22 20:38:40 +0200 |
| commit | cbd2b6b4842754495a2673df234e2496494245be (patch) | |
| tree | bbeead5c3d88388c9d5e2b029797ebf47171386d /src/liballoc | |
| parent | 1ef24bb3e23975b8183a04b0691ee0ecb878c17e (diff) | |
| download | rust-cbd2b6b4842754495a2673df234e2496494245be.tar.gz rust-cbd2b6b4842754495a2673df234e2496494245be.zip | |
Add Box::into_unique
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/arc.rs | 4 | ||||
| -rw-r--r-- | src/liballoc/boxed.rs | 31 | ||||
| -rw-r--r-- | src/liballoc/btree/node.rs | 4 | ||||
| -rw-r--r-- | src/liballoc/linked_list.rs | 6 | ||||
| -rw-r--r-- | src/liballoc/rc.rs | 24 |
5 files changed, 48 insertions, 21 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index cc792c9f83f..9e314251934 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -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. @@ -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(), diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 94f5f4042e1..6318d22059f 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -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")] diff --git a/src/liballoc/btree/node.rs b/src/liballoc/btree/node.rs index 0a752702b12..05ac9cba5e0 100644 --- a/src/liballoc/btree/node.rs +++ b/src/liballoc/btree/node.rs @@ -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 { diff --git a/src/liballoc/linked_list.rs b/src/liballoc/linked_list.rs index 08d6fac3849..850dd6adcf0 100644 --- a/src/liballoc/linked_list.rs +++ b/src/liballoc/linked_list.rs @@ -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, @@ -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, @@ -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, diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 6ff6b6b0372..a2184054b37 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -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, + })), } } @@ -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(), |
