使用Rust对齐功能(issue 33626)

Mat*_*ias 3 memory-alignment rust

RFC 1358提出了一个对齐属性#[repr(align="N")],它被接受了.Rust问题33626将该功能纳入夜间版本.

我无法使用此功能rustc 1.19.0-nightly (777ee2079 2017-05-01).如果我在没有 feature gate(#![feature(repr_align)])的情况下编译:

#[repr(align="16")]
struct Foo {
    bar: u32,
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误声明:

error: the struct `#[repr(align(u16))]` attribute is experimental (see issue #33626)
 --> foo.rs:3:1
  |
3 | / struct Foo {
4 | |     bar: u32,
5 | | }
  | |_^
  |
  = help: add #![feature(repr_align)] to the crate attributes to enable
Run Code Online (Sandbox Code Playgroud)

当我使用功能门编译,错误消息显示:

error[E0552]: unrecognized representation hint
 --> foo.rs:3:8
  |
3 | #[repr(align="16")]
  |        ^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

我也尝试了第一条错误消息建议的版本(即使它不符合问题),但仍然没有成功.使用对齐功能的正确方法是什么?

E_n*_*ate 7

当与属性文字(Playground)结合使用时,您可以使该功能起作用 :

#![feature(repr_align)]
#![feature(attr_literals)]

#[repr(align(16))]
struct Foo {
    bar: u32,
}
Run Code Online (Sandbox Code Playgroud)

众所周知,这可以在最新的开发版本(PR#41673)中使用.在Rust编译器的代码库中搜索"repr align",所有出现都依赖于属性文字,因此似乎repr(align="N")还不支持所记录的表单.

  • 我更喜欢新的语法,但是当没有提示"文档化"语法实际上不受支持时,它肯定是棘手的. (2认同)