@@ -36,9 +36,9 @@ struct A {
3636}
3737```
3838
39- will be 32-bit aligned assuming these primitives are aligned to their size.
40- It will therefore have a size that is a multiple of 32-bits. It will potentially
41- * really * become:
39+ will be 32-bit aligned on an architecture that aligns these primitives to their
40+ respective sizes. The whole struct will therefore have a size that is a multiple
41+ of 32-bits. It will potentially become:
4242
4343``` rust
4444struct A {
@@ -50,10 +50,10 @@ struct A {
5050}
5151```
5252
53- There is * no indirection* for these types; all data is stored contiguously as
54- you would expect in C. However with the exception of arrays (which are densely
55- packed and in-order), the layout of data is not by default specified in Rust.
56- Given the two following struct definitions:
53+ There is * no indirection* for these types; all data is stored within the struct,
54+ as you would expect in C. However with the exception of arrays (which are
55+ densely packed and in-order), the layout of data is not by default specified in
56+ Rust. Given the two following struct definitions:
5757
5858``` rust
5959struct A {
@@ -62,18 +62,17 @@ struct A {
6262}
6363
6464struct B {
65- x : i32 ,
65+ a : i32 ,
6666 b : u64 ,
6767}
6868```
6969
7070Rust * does* guarantee that two instances of A have their data laid out in
71- exactly the same way. However Rust * does not* guarantee that an instance of A
72- has the same field ordering or padding as an instance of B (in practice there's
73- no particular reason why they wouldn't, other than that its not currently
74- guaranteed).
71+ exactly the same way. However Rust * does not* currently guarantee that an
72+ instance of A has the same field ordering or padding as an instance of B, though
73+ in practice there's no reason why they wouldn't.
7574
76- With A and B as written, this is basically nonsensical , but several other
75+ With A and B as written, this point would seem to be pedantic , but several other
7776features of Rust make it desirable for the language to play with data layout in
7877complex ways.
7978
@@ -133,18 +132,21 @@ struct FooRepr {
133132}
134133```
135134
136- And indeed this is approximately how it would be laid out in general
137- (modulo the size and position of ` tag ` ). However there are several cases where
138- such a representation is inefficient. The classic case of this is Rust's
139- "null pointer optimization". Given a pointer that is known to not be null
140- (e.g. ` &u32 ` ), an enum can * store* a discriminant bit * inside* the pointer
141- by using null as a special value. The net result is that
142- ` size_of::<Option<&T>>() == size_of::<&T>() `
135+ And indeed this is approximately how it would be laid out in general (modulo the
136+ size and position of ` tag ` ).
137+
138+ However there are several cases where such a representation is inefficient. The
139+ classic case of this is Rust's "null pointer optimization": an enum consisting
140+ of a single outer unit variant (e.g. ` None ` ) and a (potentially nested) non-
141+ nullable pointer variant (e.g. ` &T ` ) makes the tag unnecessary, because a null
142+ pointer value can safely be interpreted to mean that the unit variant is chosen
143+ instead. The net result is that, for example, `size_of::<Option<&T>>() ==
144+ size_of::<&T>()`.
143145
144- There are many types in Rust that are, or contain, "not null" pointers such as
146+ There are many types in Rust that are, or contain, non-nullable pointers such as
145147` Box<T> ` , ` Vec<T> ` , ` String ` , ` &T ` , and ` &mut T ` . Similarly, one can imagine
146148nested enums pooling their tags into a single discriminant, as they are by
147- definition known to have a limited range of valid values. In principle enums can
149+ definition known to have a limited range of valid values. In principle enums could
148150use fairly elaborate algorithms to cache bits throughout nested types with
149151special constrained representations. As such it is * especially* desirable that
150152we leave enum layout unspecified today.
0 commit comments