-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Zero-length arrays are non-Copy
.
#94313
Comments
cc @rust-lang/project-const-generics |
pretty much, yeah. Though I am not sure we want it even once marker trait attributes, as it will be inconsistent with other traits which also rely on lattice specialization, e.g. |
@lcnr Can you please explain the inconsistency? |
We won't be able to implement e.g. see So if we add Additionally, |
Using 0-length arrays to align things is a hack and should be replaced by an actually fluent interface to adjust layout computations based on various parameters. |
I suggest the following labels for the issue: Since I can't account for everything and write a reasonable RFC, I think that this issue can remain open until someone finds it useful enough to implement. |
I want to do something terribly unsafe. I would like to have a struct implement Note: |
Not really solving #![feature(generic_const_exprs)]
use std::mem::align_of;
fn main() {
println!("{}", align_of::<K>()); // 8
println!("{}", align_of::<X<K>>()); // 8
}
struct K(u64);
struct X<A: Sized>
where
(): AlignT<{ align_of::<A>() }>,
{
k: [<() as AlignT<{ align_of::<A>() }>>::T; 0],
x: u8,
}
trait AlignT<const N: usize> {
type T: Copy;
}
impl AlignT<8> for () {
type T = u64;
}
impl Copy for X<K> {}
impl Clone for X<K> {
fn clone(&self) -> Self {
*self
}
} |
According to the documentation of
core::marker::Copy
,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 implementcore::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 implementingCopy
on overaligned types like the one below: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
The text was updated successfully, but these errors were encountered: