-
Notifications
You must be signed in to change notification settings - Fork 13.3k
There is no syntax for struct alignment #17537
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
Comments
You can currently use empty arrays to get aligned structs. No need for transmute: #[repr(C)]
struct X {
x: u8,
_alignment: [u64, ..0],
}
fn main() {
println!("{}", std::mem::pref_align_of::<X>());
// Prints 8 on x86_64 linux
} However, being able to manually align stuff would be good. In particular, GCC allows you to specify the alignment of each field. I've suggested this syntax before: struct X {
#[aligned(16)]
x: u8,
#[aligned(align_of(libc::c_double))]
y: u8,
} |
I'm not sure, but I think that this change would require an RFC, and is better as an issue on that repo than here. |
Yep, I'm not really skilled at writing RFC-style things though. |
@edef1c I think @steveklabnik just means that this issue belongs on the rfcs issue tracker, which is where we're trying to put feature requests now. @eddyb I'm not sure what the plan is. |
Yes, that's what I meant. |
This issue has been moved to the RFCs repo: rust-lang/rfcs#325 |
Currently, a C struct definition including an alignment hint (
ALIGN(64)
) cannot be translated to Rust.Our inability to translate this makes some C ABIs impossible to adhere to, causing crashes and undefined behaviour.
The only way to hack around this is to build prepend your struct with a zero-length array of a SIMD type.
We should have some kind of syntax for this. Perhaps
#[align(64)]
?@eddyb proposed
#[repr(align(64))]
, but I fear that complicates therepr
attribute a bit much.The text was updated successfully, but these errors were encountered: