Skip to content

Zero-length arrays are non-Copy. #94313

Open
@JohnScience

Description

According to the documentation of core::marker::Copy,

Generally speaking, if your type can implement Copy, it should. Keep in mind, though, that implementing Copy is part of the public API of your type. If the type might become non-Copy in the future, it could be prudent to omit the Copy implementation now, to avoid a breaking API change.

Zero-sized types in general are a bit tricky in that regard because they (just like core::marker::PhantomData) can be used to represent ownership over values of types that non-trivially implement core::ops::Drop.

However, zero-length arrays perhaps should implement Copy because they can be bit-wise copied (assuming 0-bit/0-byte copy is permitted). It would allow implementing Copy on overaligned types like the one below:

Screenshot 2022-02-23 202852

Screenshot 2022-02-23 203825

Is there anything that stops implementing Copy on zero-length arrays aside from marker trait attribute dependency?

P.S. I would love to have a generic type where the first generic parameter would be the type of the stored value and the second parameter would be a generic constant of type usize whose value would be used as a constraint for alignment, yet it is a story for discussion on Rust internals/another issue/RFC.

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`A-specializationArea: Trait impl specializationA-trait-systemArea: Trait systemT-compilerRelevant to the compiler 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