Skip to content

Implement RefCell::replace and RefCell::swap #43574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the docs should mention that these are simply convenience functions for their one-liner implementations? I wouldn't want people thinking that they're more "magical" than they really are. Thoughts @steveklabnik? Not sure if we do something similar elsewhere.

/// 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> {
Expand Down
17 changes: 17 additions & 0 deletions src/libcore/tests/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down