Skip to content

Commit 03acea6

Browse files
committed
Implement RefCell::replace and RefCell::swap
1 parent 2a6828e commit 03acea6

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/libcore/cell.rs

+53
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,59 @@ impl<T> RefCell<T> {
543543
debug_assert!(self.borrow.get() == UNUSED);
544544
unsafe { self.value.into_inner() }
545545
}
546+
547+
/// Replaces the wrapped value with a new one, returning the old value,
548+
/// without deinitializing either one.
549+
///
550+
/// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
551+
///
552+
/// # Example
553+
///
554+
/// ```
555+
/// #![feature(refcell_replace_swap)]
556+
/// use std::cell::RefCell;
557+
/// let c = RefCell::new(5);
558+
/// let u = c.replace(6);
559+
/// assert_eq!(u, 5);
560+
/// assert_eq!(c, RefCell::new(6));
561+
/// ```
562+
///
563+
/// # Panics
564+
///
565+
/// This function will panic if the `RefCell` has any outstanding borrows,
566+
/// whether or not they are full mutable borrows.
567+
#[inline]
568+
#[unstable(feature = "refcell_replace_swap", issue="43570")]
569+
pub fn replace(&self, t: T) -> T {
570+
mem::replace(&mut *self.borrow_mut(), t)
571+
}
572+
573+
/// Swaps the wrapped value of `self` with the wrapped value of `other`,
574+
/// without deinitializing either one.
575+
///
576+
/// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html).
577+
///
578+
/// # Example
579+
///
580+
/// ```
581+
/// #![feature(refcell_replace_swap)]
582+
/// use std::cell::RefCell;
583+
/// let c = RefCell::new(5);
584+
/// let d = RefCell::new(6);
585+
/// c.swap(&d);
586+
/// assert_eq!(c, RefCell::new(6));
587+
/// assert_eq!(d, RefCell::new(5));
588+
/// ```
589+
///
590+
/// # Panics
591+
///
592+
/// This function will panic if the `RefCell` has any outstanding borrows,
593+
/// whether or not they are full mutable borrows.
594+
#[inline]
595+
#[unstable(feature = "refcell_replace_swap", issue="43570")]
596+
pub fn swap(&self, other: &Self) {
597+
mem::swap(&mut *self.borrow_mut(), &mut *other.borrow_mut())
598+
}
546599
}
547600

548601
impl<T: ?Sized> RefCell<T> {

src/libcore/tests/cell.rs

+17
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,20 @@ fn refcell_ref_coercion() {
287287
assert_eq!(&*coerced, comp);
288288
}
289289
}
290+
291+
#[test]
292+
#[should_panic]
293+
fn refcell_swap_borrows() {
294+
let x = RefCell::new(0);
295+
let _b = x.borrow();
296+
let y = RefCell::new(1);
297+
x.swap(&y);
298+
}
299+
300+
#[test]
301+
#[should_panic]
302+
fn refcell_replace_borrows() {
303+
let x = RefCell::new(0);
304+
let _b = x.borrow();
305+
x.replace(1);
306+
}

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(ord_max_min)]
3232
#![feature(rand)]
3333
#![feature(raw)]
34+
#![feature(refcell_replace_swap)]
3435
#![feature(sip_hash_13)]
3536
#![feature(slice_patterns)]
3637
#![feature(slice_rotate)]

0 commit comments

Comments
 (0)