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 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). + /// + /// # 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 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)]