From 03acea646c025846e9421e33d77a5b19782762db Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 31 Jul 2017 17:14:16 +0000 Subject: [PATCH 1/3] Implement `RefCell::replace` and `RefCell::swap` --- src/libcore/cell.rs | 53 +++++++++++++++++++++++++++++++++++++++ src/libcore/tests/cell.rs | 17 +++++++++++++ src/libcore/tests/lib.rs | 1 + 3 files changed, 71 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 35744f3f16b..b250bf49e0c 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -543,6 +543,59 @@ impl RefCell { debug_assert!(self.borrow.get() == UNUSED); unsafe { self.value.into_inner() } } + + /// Replaces the wrapped value with a new one, returning the old value, + /// without deinitializing either one. + /// + /// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html). + /// + /// # Example + /// + /// ``` + /// #![feature(refcell_replace_swap)] + /// use std::cell::RefCell; + /// let c = RefCell::new(5); + /// let u = c.replace(6); + /// assert_eq!(u, 5); + /// assert_eq!(c, RefCell::new(6)); + /// ``` + /// + /// # Panics + /// + /// This function will panic if the `RefCell` has any outstanding borrows, + /// whether or not they are full mutable borrows. + #[inline] + #[unstable(feature = "refcell_replace_swap", issue="43570")] + pub fn replace(&self, t: T) -> T { + mem::replace(&mut *self.borrow_mut(), t) + } + + /// Swaps the wrapped value of `self` with the wrapped value of `other`, + /// without deinitializing either one. + /// + /// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html). + /// + /// # Example + /// + /// ``` + /// #![feature(refcell_replace_swap)] + /// use std::cell::RefCell; + /// let c = RefCell::new(5); + /// let d = RefCell::new(6); + /// c.swap(&d); + /// assert_eq!(c, RefCell::new(6)); + /// assert_eq!(d, RefCell::new(5)); + /// ``` + /// + /// # Panics + /// + /// This function will panic if the `RefCell` has any outstanding borrows, + /// whether or not they are full mutable borrows. + #[inline] + #[unstable(feature = "refcell_replace_swap", issue="43570")] + pub fn swap(&self, other: &Self) { + mem::swap(&mut *self.borrow_mut(), &mut *other.borrow_mut()) + } } impl RefCell { diff --git a/src/libcore/tests/cell.rs b/src/libcore/tests/cell.rs index 8585f2f0871..cc0ef6a6f17 100644 --- a/src/libcore/tests/cell.rs +++ b/src/libcore/tests/cell.rs @@ -287,3 +287,20 @@ fn refcell_ref_coercion() { assert_eq!(&*coerced, comp); } } + +#[test] +#[should_panic] +fn refcell_swap_borrows() { + let x = RefCell::new(0); + let _b = x.borrow(); + let y = RefCell::new(1); + x.swap(&y); +} + +#[test] +#[should_panic] +fn refcell_replace_borrows() { + let x = RefCell::new(0); + let _b = x.borrow(); + x.replace(1); +} diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index a85c347146b..84a3be99c27 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -31,6 +31,7 @@ #![feature(ord_max_min)] #![feature(rand)] #![feature(raw)] +#![feature(refcell_replace_swap)] #![feature(sip_hash_13)] #![feature(slice_patterns)] #![feature(slice_rotate)] From dc76247a0a7778a0f38d63bcc3ae5428327bfcf1 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 2 Aug 2017 10:58:27 -0700 Subject: [PATCH 2/3] Change "Example" to "Examples" in the doc comments --- src/libcore/cell.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index b250bf49e0c..675fcef5e5a 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -549,7 +549,7 @@ impl RefCell { /// /// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html). /// - /// # Example + /// # Examples /// /// ``` /// #![feature(refcell_replace_swap)] @@ -575,7 +575,7 @@ impl RefCell { /// /// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html). /// - /// # Example + /// # Examples /// /// ``` /// #![feature(refcell_replace_swap)] From 846d373ddf31aeb76dabedb81a4ae9200d9da1cd Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 8 Aug 2017 09:53:51 -0700 Subject: [PATCH 3/3] Clarify the language around `RefCell::swap` --- src/libcore/cell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 675fcef5e5a..f0a94a634f5 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -589,7 +589,7 @@ impl RefCell { /// /// # Panics /// - /// This function will panic if the `RefCell` has any outstanding borrows, + /// This function will panic if either `RefCell` has any outstanding borrows, /// whether or not they are full mutable borrows. #[inline] #[unstable(feature = "refcell_replace_swap", issue="43570")]