Skip to content

Cow is not Copy-On-Write #50160

Closed
Closed
@Ekleog

Description

@Ekleog

So. I hope I'm misreading the code or reading at the wrong place. But…

If I can read https://doc.rust-lang.org/src/alloc/borrow.rs.html#159-182 correctly, the implementation of Clone for Cow is like this:

    fn clone(&self) -> Cow<'a, B> {
        match *self {
            Borrowed(b) => Borrowed(b),
            Owned(ref o) => {
                let b: &B = o.borrow();
                Owned(b.to_owned())
            }
        }
    }

So this copies the data when the Cow is copied.

I think it should instead be written like this:

    fn clone(&self) -> Cow<'a, B> {
        match *self {
            Borrowed(b) => Borrowed(b),
            Owned(ref o) => Borrowed(o.borrow()),
        }
    }

(and a similar fix for clone_from)

If I didn't misunderstand the code or its purpose, I can try to write up a PR to fix this.
(and plz gimme kudos for the clickbait title)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions