about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-05-03 16:38:41 +0000
committerbors <bors@rust-lang.org>2020-05-03 16:38:41 +0000
commitea733c3a59be77b752d6d3f722596bfd5b1fcf31 (patch)
tree61894abbcaaaf485a676ff25e81596bb79463299 /src/libcore
parente5f35df2c6944b843b08369c4b2ff3bdb0beb2d2 (diff)
parentce1dba99185a87eafcc99fc2738a1f8473f13b6e (diff)
Auto merge of #71853 - Dylan-DPC:rollup-4qi6ry9, r=Dylan-DPC
Rollup of 4 pull requests

Successful merges:

 - #71398 (Add `RefCell::take`)
 - #71663 (Fix exceeding bitshifts not emitting for assoc. consts (properly this time, I swear!))
 - #71726 (Suggest deref when coercing `ty::Ref` to `ty::RawPtr` with arbitrary mutability)
 - #71808 (Add long error explanation for E0539)

Failed merges:

r? @ghost
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/cell.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index a922d4f118b..0f2665eba6f 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -1023,6 +1023,31 @@ impl<T: ?Sized> RefCell<T> {
     }
 }
 
+impl<T: Default> RefCell<T> {
+    /// Takes the wrapped value, leaving `Default::default()` in its place.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the value is currently borrowed.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(refcell_take)]
+    /// use std::cell::RefCell;
+    ///
+    /// let c = RefCell::new(5);
+    /// let five = c.take();
+    ///
+    /// assert_eq!(five, 5);
+    /// assert_eq!(c.into_inner(), 0);
+    /// ```
+    #[unstable(feature = "refcell_take", issue = "71395")]
+    pub fn take(&self) -> T {
+        self.replace(Default::default())
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 unsafe impl<T: ?Sized> Send for RefCell<T> where T: Send {}