Skip to content
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

try_replace, try_swap in RefCell (code included) #54493

Open
Phlosioneer opened this issue Sep 23, 2018 · 0 comments
Open

try_replace, try_swap in RefCell (code included) #54493

Phlosioneer opened this issue Sep 23, 2018 · 0 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Phlosioneer
Copy link
Contributor

Phlosioneer commented Sep 23, 2018

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...

@Phlosioneer Phlosioneer changed the title try_replace, and try_swap in RefCell (code included) try_replace, try_swap in RefCell (code included) Sep 23, 2018
@csmoe csmoe added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Sep 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants