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

impl Clone for Cow #19359

Merged
merged 1 commit into from
Dec 5, 2014
Merged

impl Clone for Cow #19359

merged 1 commit into from
Dec 5, 2014

Conversation

japaric
Copy link
Member

@japaric japaric commented Nov 27, 2014

Now we can use #[deriving(Clone)] on structs that contain Cow.

r? @aturon or anyone else

@japaric
Copy link
Member Author

japaric commented Nov 27, 2014

Just want to add that due to the bad interaction of struct bounds and the deriving syntax extension, in the case of CowVec<T> you are forced to bound T: Clone with a where clause:

This compiles:

use std::vec::CowVec;

#[deriving(Clone)]
struct Foo<'a, T: 'a> where T: Clone {
    bar: CowVec<'a, T>,
}

fn main() {}

But this does not:

use std::vec::CowVec;

#[deriving(Clone)]
struct Foo<'a, T: 'a + Clone> {
    bar: CowVec<'a, T>,
}

fn main() {}

See #19361 and #19358 for more details

Borrowed(b) => Borrowed(b),
Owned(ref o) => {
let b: &B = BorrowFrom::borrow_from(o);
Owned(b.to_owned())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must confess that it feels weird to me to always into_owned a Clone of an owned Cow. I imagine that always producing a Borrow would have unfortunate consequences? Can you provide an example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Self = SendStr = CowString<'static>. Cloning that can't return a Borrowed value, since that would need an inner &'static str, and you can't produce &'static str from String.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we explicitly document the behaviour, since it's subtle and might be unexpected? Or do you think it can be considered an implementation detail?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to require T: Clone instead of B: BorrowFrom<T> so you could just call .clone() on the owned variant (which may be more expected)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require T: Clone instead of B: BorrowFrom

Note that the Cow struct enforces the B: BorrowFrom<T> bound, so it can't be removed. I wrote the impl in this way to reduce the numbers of bounds on the input parameters.

@alexcrichton alexcrichton merged commit ddb7718 into rust-lang:master Dec 5, 2014
@japaric japaric deleted the clone-cow branch December 16, 2014 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants