Skip to content

Commit

Permalink
Rollup merge of #71398 - ThinkChaos:feat_refcell_take, r=LukasKalbertodt
Browse files Browse the repository at this point in the history
Add `RefCell::take`

Add `RefCell::take` to match `Cell` and `Option`.
I also changed a couple of calls to `.replace` to `.take`.

Tracking issue is #71395.

This is my first contribution, please tell me if there's anything I could improve, thanks!
  • Loading branch information
Dylan-DPC committed May 3, 2020
2 parents e5f35df + 4a79424 commit 6f5de87
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl Drop for WaiterQueue<'_> {
let mut queue = (state_and_queue & !STATE_MASK) as *const Waiter;
while !queue.is_null() {
let next = (*queue).next;
let thread = (*queue).thread.replace(None).unwrap();
let thread = (*queue).thread.take().unwrap();
(*queue).signaled.store(true, Ordering::Release);
// ^- FIXME (maybe): This is another case of issue #55005
// `store()` has a potentially dangling ref to `signaled`.
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/wasm-panic-small/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pub fn foo() {
pub fn foo() -> usize {
use std::cell::Cell;
thread_local!(static A: Cell<Vec<u32>> = Cell::new(Vec::new()));
A.try_with(|x| x.replace(Vec::new()).len()).unwrap_or(0)
A.try_with(|x| x.take().len()).unwrap_or(0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct MyType<'a>(Cell<Option<&'a mut MyType<'a>>>, PhantomPinned);
impl<'a> Clone for &'a mut MyType<'a> {
//~^ ERROR E0751
fn clone(&self) -> &'a mut MyType<'a> {
self.0.replace(None).unwrap()
self.0.take().unwrap()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct MyType<'a>(Cell<Option<&'a mut MyType<'a>>>, PhantomPinned);
impl<'a> DerefMut for &'a MyType<'a> {
//~^ ERROR E0751
fn deref_mut(&mut self) -> &mut MyType<'a> {
self.0.replace(None).unwrap()
self.0.take().unwrap()
}
}

Expand Down

0 comments on commit 6f5de87

Please sign in to comment.