-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
/// without deinitializing either one. | ||
/// | ||
/// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html). | ||
/// | ||
/// # Example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/// | ||
/// ``` | ||
/// #![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). | ||
/// | ||
/// # Example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/// | ||
/// ``` | ||
/// #![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 the `RefCell` has any outstanding borrows, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is only one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, yeah, I thought this was below. |
||
/// 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> { | ||
|
There was a problem hiding this comment.
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.