1- - Feature Name: ` repr_pack `
1+ - Feature Name: ` repr_packed `
22- Start Date: 2015-12-06
3- - RFC PR: (leave this empty )
4- - Rust Issue: (leave this empty )
3+ - RFC PR: [ rust-lang/rfcs # 1399 ] ( https://github.com/rust-lang/rfcs/pull/1399 )
4+ - Rust Issue: [ rust-lang/rust # 33158 ] ( https://github.com/rust-lang/rust/issues/33158 )
55
66# Summary
77[ summary ] : #summary
88
9- Extend the existing ` #[repr] ` attribute on structs with a ` pack = "N"` option to
9+ Extend the existing ` #[repr] ` attribute on structs with a ` packed = "N"` option to
1010specify a custom packing for ` struct ` types.
1111
1212# Motivation
@@ -19,7 +19,7 @@ C/C++ libraries (such as Windows API which uses it pervasively making writing
1919Rust libraries such as ` winapi ` challenging).
2020
2121At the moment the only way to work around the lack of a proper
22- ` #[repr(pack = "N")] ` attribute is to use ` #[repr(packed)] ` and then manually
22+ ` #[repr(packed = "N")] ` attribute is to use ` #[repr(packed)] ` and then manually
2323fill in padding which is a burdensome task. Even then that isn't quite right
2424because the overall alignment of the struct would end up as 1 even though it
2525needs to be N (or the default if that is smaller than N), so this fills in a gap
@@ -31,7 +31,7 @@ which is impossible to do in Rust at the moment.
3131The ` #[repr] ` attribute on ` struct ` s will be extended to include a form such as:
3232
3333``` rust
34- #[repr(pack = " 2" )]
34+ #[repr(packed = " 2" )]
3535struct LessAligned (i16 , i32 );
3636```
3737
@@ -42,7 +42,7 @@ alignment of 4 and a size of 8, and the second field would have an offset of 4
4242from the base of the struct.
4343
4444Syntactically, the ` repr ` meta list will be extended to accept a meta item
45- name/value pair with the name "pack " and the value as a string which can be
45+ name/value pair with the name "packed " and the value as a string which can be
4646parsed as a ` u64 ` . The restrictions on where this attribute can be placed along
4747with the accepted values are:
4848
@@ -61,34 +61,29 @@ struct, then the alignment and layout of the struct should be unaffected.
6161When combined with ` #[repr(C)] ` the size alignment and layout of the struct
6262should match the equivalent struct in C.
6363
64- ` #[repr(packed)] ` and ` #[repr(pack = "1")] ` should have identical behavior.
64+ ` #[repr(packed)] ` and ` #[repr(packed = "1")] ` should have identical behavior.
6565
6666Because this lowers the effective alignment of fields in the same way that
6767` #[repr(packed)] ` does (which caused [ issue #27060 ] [ gh27060 ] ), while accessing a
6868field should be safe, borrowing a field should be unsafe.
6969
70- Specifying ` #[repr(packed)] ` and ` #[repr(pack = "N")] ` where N is not 1 should
70+ Specifying ` #[repr(packed)] ` and ` #[repr(packed = "N")] ` where N is not 1 should
7171result in an error.
7272
73- Specifying ` #[repr(pack = "A")] ` and ` #[repr(align = "B")] ` should still pack
73+ Specifying ` #[repr(packed = "A")] ` and ` #[repr(align = "B")] ` should still pack
7474together fields with the packing specified, but then increase the overall
7575alignment to the alignment specified. Depends on [ RFC #1358 ] [ rfc1358 ] landing.
7676
7777# Drawbacks
7878[ drawbacks ] : #drawbacks
7979
80- Duplication in the language where ` #[repr(packed)] ` and ` #[repr(pack = "1")] `
81- have identical behavior.
82-
8380# Alternatives
8481[ alternatives ] : #alternatives
8582
8683* The alternative is not doing this and forcing people to continue using
8784 ` #[repr(packed)] ` with manual padding, although such structs would always have
8885 an alignment of 1 which is often wrong.
8986* Alternatively a new attribute could be used such as ` #[pack] ` .
90- * ` #[repr(packed)] ` could be extended as either ` #[repr(packed(N))] ` or
91- ` #[repr(packed = "N")] ` .
9287
9388# Unresolved questions
9489[ unresolved ] : #unresolved-questions
0 commit comments