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

Add .cloned() method to Result? #50951

Closed
novacrazy opened this issue May 21, 2018 · 3 comments
Closed

Add .cloned() method to Result? #50951

novacrazy opened this issue May 21, 2018 · 3 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

@novacrazy
Copy link

novacrazy commented May 21, 2018

It caught me off-guard that there isn't a .cloned() method for Result<&T, E>, yet there is one for Option<&T>.

I don't think this is a big enough feature to have a whole RFC for, even though I can do that if necessary, but it would be nice to have a .cloned() method like this:

impl<'a, T, E> Result<&'a T, E>
where
    T: Clone,
{
    #[inline]
    pub fn cloned(self) -> Result<T, E> {
        self.map(Clone::clone)
    }
}

and the equivalent mutable reference version.

However, if there is a reason why this is not already in place, I can understand. It simply feels a little awkward having to manually map Clone::clone in otherwise idiomatic code.

@scottmcm
Copy link
Member

A bunch of questions from #50267 come up here too, like should cloned() clone both T and E, or should there be cloned_ok() and cloned_err()?

And, in general, if figuring out these answers better than just .map(Clone::clone) 🤣

@novacrazy
Copy link
Author

I think it would make sense to follow the pattern of .map and .map_err by having .cloned() and .cloned_err(), as they would just call .map and .map_err internally.

I've seen a few times where functions return references in a Result, but I've never seen a Result with a reference in the Err value. The likelihood of having both the Ok and Err values be references is probably pretty small.

@Mark-Simulacrum Mark-Simulacrum 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 May 24, 2018
@novacrazy
Copy link
Author

Added in #63166

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

3 participants