diff options
| author | ThinkChaos <ThinkChaos@users.noreply.github.com> | 2020-04-21 20:03:50 +0200 |
|---|---|---|
| committer | ThinkChaos <ThinkChaos@users.noreply.github.com> | 2020-04-26 11:43:34 +0200 |
| commit | f121f094fe4d4f4e92142984e9230373c311e1e6 (patch) | |
| tree | 2d26e7717bb359fcb0b6ce7241d88b67317224ab /src | |
| parent | 019ab732ce63a117cbb446db1488916c5c0bd2a7 (diff) | |
Add `RefCell::take`
In the same vein as `Cell::take` and `Option::take`.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/cell.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index a922d4f118b..05c9c97a612 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1023,6 +1023,27 @@ impl<T: ?Sized> RefCell<T> { } } +impl<T: Default> RefCell<T> { + /// Takes the wrapped value, leaving `Default::default()` in its place. + /// + /// # 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 {} |
