Skip to content

Commit 18e8b9d

Browse files
authored
Rollup merge of rust-lang#71398 - ThinkChaos:feat_refcell_take, r=LukasKalbertodt
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 rust-lang#71395. This is my first contribution, please tell me if there's anything I could improve, thanks!
2 parents e5f35df + 4a79424 commit 18e8b9d

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

src/libcore/cell.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,31 @@ impl<T: ?Sized> RefCell<T> {
10231023
}
10241024
}
10251025

1026+
impl<T: Default> RefCell<T> {
1027+
/// Takes the wrapped value, leaving `Default::default()` in its place.
1028+
///
1029+
/// # Panics
1030+
///
1031+
/// Panics if the value is currently borrowed.
1032+
///
1033+
/// # Examples
1034+
///
1035+
/// ```
1036+
/// #![feature(refcell_take)]
1037+
/// use std::cell::RefCell;
1038+
///
1039+
/// let c = RefCell::new(5);
1040+
/// let five = c.take();
1041+
///
1042+
/// assert_eq!(five, 5);
1043+
/// assert_eq!(c.into_inner(), 0);
1044+
/// ```
1045+
#[unstable(feature = "refcell_take", issue = "71395")]
1046+
pub fn take(&self) -> T {
1047+
self.replace(Default::default())
1048+
}
1049+
}
1050+
10261051
#[stable(feature = "rust1", since = "1.0.0")]
10271052
unsafe impl<T: ?Sized> Send for RefCell<T> where T: Send {}
10281053

src/libstd/sync/once.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl Drop for WaiterQueue<'_> {
497497
let mut queue = (state_and_queue & !STATE_MASK) as *const Waiter;
498498
while !queue.is_null() {
499499
let next = (*queue).next;
500-
let thread = (*queue).thread.replace(None).unwrap();
500+
let thread = (*queue).thread.take().unwrap();
501501
(*queue).signaled.store(true, Ordering::Release);
502502
// ^- FIXME (maybe): This is another case of issue #55005
503503
// `store()` has a potentially dangling ref to `signaled`.

src/test/run-make/wasm-panic-small/foo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ pub fn foo() {
2323
pub fn foo() -> usize {
2424
use std::cell::Cell;
2525
thread_local!(static A: Cell<Vec<u32>> = Cell::new(Vec::new()));
26-
A.try_with(|x| x.replace(Vec::new()).len()).unwrap_or(0)
26+
A.try_with(|x| x.take().len()).unwrap_or(0)
2727
}

src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct MyType<'a>(Cell<Option<&'a mut MyType<'a>>>, PhantomPinned);
77
impl<'a> Clone for &'a mut MyType<'a> {
88
//~^ ERROR E0751
99
fn clone(&self) -> &'a mut MyType<'a> {
10-
self.0.replace(None).unwrap()
10+
self.0.take().unwrap()
1111
}
1212
}
1313

src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct MyType<'a>(Cell<Option<&'a mut MyType<'a>>>, PhantomPinned);
1212
impl<'a> DerefMut for &'a MyType<'a> {
1313
//~^ ERROR E0751
1414
fn deref_mut(&mut self) -> &mut MyType<'a> {
15-
self.0.replace(None).unwrap()
15+
self.0.take().unwrap()
1616
}
1717
}
1818

0 commit comments

Comments
 (0)