about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-08-14 17:05:02 +0000
committerbors <bors@rust-lang.org>2017-08-14 17:05:02 +0000
commitf1ca76c4978b793ec2e6332009b1ba84d164eb03 (patch)
treea77147bd27b563f44d1c24728d99f5352bbf17d1
parent0d12553320150750f41446291d0e19a3ca648c82 (diff)
parent846d373ddf31aeb76dabedb81a4ae9200d9da1cd (diff)
downloadrust-f1ca76c4978b793ec2e6332009b1ba84d164eb03.tar.gz
rust-f1ca76c4978b793ec2e6332009b1ba84d164eb03.zip
Auto merge of #43574 - notriddle:master, r=sfackler
Implement `RefCell::replace` and `RefCell::swap`

Tracking issue: #43570
-rw-r--r--src/libcore/cell.rs53
-rw-r--r--src/libcore/tests/cell.rs17
-rw-r--r--src/libcore/tests/lib.rs1
3 files changed, 71 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index 21b5557db99..6068f1a7961 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -571,6 +571,59 @@ impl<T> RefCell<T> {
         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).
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![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).
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![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 either `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<T: ?Sized> RefCell<T> {
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)]