From f121f094fe4d4f4e92142984e9230373c311e1e6 Mon Sep 17 00:00:00 2001 From: ThinkChaos Date: Tue, 21 Apr 2020 20:03:50 +0200 Subject: [PATCH] Add `RefCell::take` In the same vein as `Cell::take` and `Option::take`. --- src/libcore/cell.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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 RefCell { } } +impl RefCell { + /// 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 Send for RefCell where T: Send {}