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

What about a Take trait? #61148

Closed
lachlansneff opened this issue May 25, 2019 · 4 comments
Closed

What about a Take trait? #61148

lachlansneff opened this issue May 25, 2019 · 4 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

@lachlansneff
Copy link

lachlansneff commented May 25, 2019

I saw issue #61129, and thought that it might be a good idea to generalize it.

The prelude could export a new trait called Take.

pub trait Take {
    fn take(&mut self) -> Self;
}

A lot of types could implement it, Vec<T> for example. The take method on Option<T> could be moved into this trait, etc.

The Take trait could even be implemented for every type that implements Default:

impl<T> Take for T
where
    T: Default
{
    fn take(&mut self) -> Self {
        mem::replace(self, Default::default())
    }
}
@estebank estebank added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 25, 2019
@jonas-schievink jonas-schievink added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label May 25, 2019
@czipperz
Copy link
Contributor

This was suggested in the other thread. However, it doesn't seem to make much sense to override this in any other way that the default implementation.

@jonhoo
Copy link
Contributor

jonhoo commented May 26, 2019

Yeah, I have the same suspicion as @czipperz -- how often do you expect the implementation for this to differ from replacing with the default?

@czipperz
Copy link
Contributor

It also would suffer from the fact that traits must be explicitly implemented. Anyway, thank you for your comment and contribution to the discussion!

@Mark-Simulacrum
Copy link
Member

I think this is essentially a topic for discussion and we don't really have any clear idea of what we'd use this for (there's also #61129 where this idea has already been discussed quite a bit). If you'd like to discuss this further I would do so on internals. Thanks!

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

6 participants