diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-05-03 18:34:34 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-03 18:34:34 +0200 |
| commit | 6f5de87d3f9f192e00defe9a15e8396c13c2ec0c (patch) | |
| tree | 2abe45831799f97a5f86e08c910cc7d5a515778f | |
| parent | e5f35df2c6944b843b08369c4b2ff3bdb0beb2d2 (diff) | |
| parent | 4a79424b748338bd9635f0115c025942af2d5c3d (diff) | |
| download | rust-6f5de87d3f9f192e00defe9a15e8396c13c2ec0c.tar.gz rust-6f5de87d3f9f192e00defe9a15e8396c13c2ec0c.zip | |
Rollup merge of #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 #71395. This is my first contribution, please tell me if there's anything I could improve, thanks!
| -rw-r--r-- | src/libcore/cell.rs | 25 | ||||
| -rw-r--r-- | src/libstd/sync/once.rs | 2 | ||||
| -rw-r--r-- | src/test/run-make/wasm-panic-small/foo.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs | 2 |
5 files changed, 29 insertions, 4 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index a922d4f118b..0f2665eba6f 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -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 {} diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 1e6b6c430be..a3ee14e85d2 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -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`. diff --git a/src/test/run-make/wasm-panic-small/foo.rs b/src/test/run-make/wasm-panic-small/foo.rs index fd3dddb18eb..6df52affe39 100644 --- a/src/test/run-make/wasm-panic-small/foo.rs +++ b/src/test/run-make/wasm-panic-small/foo.rs @@ -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) } diff --git a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs index 499ac461e59..a5b85646581 100644 --- a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs +++ b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs @@ -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() } } diff --git a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs index 245be800780..606cc65a84b 100644 --- a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs +++ b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs @@ -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() } } |
