Add RefCell::take

In the same vein as `Cell::take` and `Option::take`.
This commit is contained in:
ThinkChaos 2020-04-21 20:03:50 +02:00
parent 019ab732ce
commit f121f094fe

View File

@ -1023,6 +1023,27 @@ pub unsafe fn try_borrow_unguarded(&self) -> Result<&T, BorrowError> {
}
}
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 {}