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

proposal: Assign trait #4329

Closed
thestinger opened this issue Jan 2, 2013 · 5 comments
Closed

proposal: Assign trait #4329

thestinger opened this issue Jan 2, 2013 · 5 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@thestinger
Copy link
Contributor

At the moment, a = b.clone() is the way to copy-assign to a value in generic code (at least once there's a default implementation for Copyable types, per #3313).

If a is uninitialized, that's perfectly optimal. When the value is already initialized, assignment can often be done with fewer allocations; a list or chunked list can be partially or fully reused, and even a vector/string can often be reused if it's big enough.

So, something like this (with a default impl for Cloneable types):

pub trait Assign<T>() {
    fn assign(&mut self, other: &T) -> self;
}

a.assign(&b);

There's no need for any operator overload, since a = b is just an implicit copy or a move.

@pcwalton
Copy link
Contributor

I don't think that this is backwards incompatible.

@thestinger
Copy link
Contributor Author

@pcwalton: agreed, this shouldn't break anything, it's just an extra method/trait

@graydon
Copy link
Contributor

graydon commented Aug 1, 2013

just a bug, removing milestone/nomination.

@sanxiyn
Copy link
Member

sanxiyn commented Oct 24, 2013

Tagging RFC.

@thestinger
Copy link
Contributor Author

I think this would be better as a default method on Clone rather than a trait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants