Skip to content

Commit f1ca76c

Browse files
committed
Auto merge of #43574 - notriddle:master, r=sfackler
Implement `RefCell::replace` and `RefCell::swap` Tracking issue: #43570
2 parents 0d12553 + 846d373 commit f1ca76c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

Diff for: src/libcore/cell.rs

+53
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,59 @@ impl<T> RefCell<T> {
571571
debug_assert!(self.borrow.get() == UNUSED);
572572
unsafe { self.value.into_inner() }
573573
}
574+
575+
/// Replaces the wrapped value with a new one, returning the old value,
576+
/// without deinitializing either one.
577+
///
578+
/// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
579+
///
580+
/// # Examples
581+
///
582+
/// ```
583+
/// #![feature(refcell_replace_swap)]
584+
/// use std::cell::RefCell;
585+
/// let c = RefCell::new(5);
586+
/// let u = c.replace(6);
587+
/// assert_eq!(u, 5);
588+
/// assert_eq!(c, RefCell::new(6));
589+
/// ```
590+
///
591+
/// # Panics
592+
///
593+
/// This function will panic if the `RefCell` has any outstanding borrows,
594+
/// whether or not they are full mutable borrows.
595+
#[inline]
596+
#[unstable(feature = "refcell_replace_swap", issue="43570")]
597+
pub fn replace(&self, t: T) -> T {
598+
mem::replace(&mut *self.borrow_mut(), t)
599+
}
600+
601+
/// Swaps the wrapped value of `self` with the wrapped value of `other`,
602+
/// without deinitializing either one.
603+
///
604+
/// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html).
605+
///
606+
/// # Examples
607+
///
608+
/// ```
609+
/// #![feature(refcell_replace_swap)]
610+
/// use std::cell::RefCell;
611+
/// let c = RefCell::new(5);
612+
/// let d = RefCell::new(6);
613+
/// c.swap(&d);
614+
/// assert_eq!(c, RefCell::new(6));
615+
/// assert_eq!(d, RefCell::new(5));
616+
/// ```
617+
///
618+
/// # Panics
619+
///
620+
/// This function will panic if either `RefCell` has any outstanding borrows,
621+
/// whether or not they are full mutable borrows.
622+
#[inline]
623+
#[unstable(feature = "refcell_replace_swap", issue="43570")]
624+
pub fn swap(&self, other: &Self) {
625+
mem::swap(&mut *self.borrow_mut(), &mut *other.borrow_mut())
626+
}
574627
}
575628

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

Diff for: 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+
}

Diff for: 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)