-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Description
Every method has a non-panicking variant except for replace
and swap
. It would be fairly simple to add them, leveraging the try_borrow_mut
method; here's the code:
/// An error returned by [`RefCell::try_replace`](struct.RefCell.html#method.try_replace).
pub struct ReplaceError { _private: () }
/// An error returned by [`RefCell::try_swap`](struct.RefCell.html#method.try_swap).
pub struct SwapError { _private: () }
/// Replaces the wrapped value with a new one, returning the old value,
/// without deinitializing either one, or an error if the value is currently
/// borrowed.
///
/// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html).
///
/// This is the non-panicking variant of [`replace`](#method.replace)
#[inline]
#[unstable(feature = "try_replace_swap")]
pub fn try_replace(&self, t: T) -> Result<T, ReplaceError> {
match self.try_borrow_mut() {
Ok(mut b) => Ok(mem::replace(&mut *b, t)),
Err(_) => Err(ReplaceError { _private: () })
}
}
/// Swaps the wrapped value of `self` with the wrapped value of `other`,
/// without deinitializing either one. Returns an error if either value is
/// currently borrowed.
///
/// This function corresponds to [`std::mem::swap`](../mem/fn.swap.html).
///
/// This is the non-panicking variant of [`swap`](#method.swap)
#[inline]
#[unstable(feature = "try_replace_swap")]
pub fn try_swap(&self, other: &Self) -> Result<(), SwapError> {
match (self.try_borrow_mut(), other.try_borrow_mut()) {
(Ok(mut s), Ok(mut o)) => {
mem::swap(&mut *s, &mut *o);
Ok(())
},
_ => Err(SwapError { _private: () })
}
}
I currently don't have the ability to clone the repo, test, etc. right now. I'm also bad at making examples, so those need to be added. 😏
So I've made this issue. If / when I have time, I will probably try to make a PR for this myself, if no one's done it yet. However, that might be months...
Metadata
Metadata
Assignees
Labels
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.