Skip to content

#[derive(Clone)] should rely on Copy when possible #31085

@SimonSapin

Description

@SimonSapin

When a type that is also Copy has #[derive(Clone)], the derived implementation should be:

    fn clone(&self) -> Self { *self }

From #27750 (comment)

As the example in #27750 (comment) shows, even when a composite type like Composite is Copy, its clone implementation may be less efficient than its Copy impl, due to copying fields individually instead of as a block.

Maybe being Copy is hard to determine exactly by the time #[derive(Clone)] needs to do its thing, but at least this could happen when both traits are in the same derive attribute? #[derive(Copy, Clone)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions