@@ -36,9 +36,9 @@ struct A {
36
36
}
37
37
```
38
38
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:
42
42
43
43
``` rust
44
44
struct A {
@@ -50,10 +50,10 @@ struct A {
50
50
}
51
51
```
52
52
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:
57
57
58
58
``` rust
59
59
struct A {
@@ -62,18 +62,17 @@ struct A {
62
62
}
63
63
64
64
struct B {
65
- x : i32 ,
65
+ a : i32 ,
66
66
b : u64 ,
67
67
}
68
68
```
69
69
70
70
Rust * 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.
75
74
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
77
76
features of Rust make it desirable for the language to play with data layout in
78
77
complex ways.
79
78
@@ -133,18 +132,21 @@ struct FooRepr {
133
132
}
134
133
```
135
134
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>()`.
143
145
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
145
147
` Box<T> ` , ` Vec<T> ` , ` String ` , ` &T ` , and ` &mut T ` . Similarly, one can imagine
146
148
nested 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
148
150
use fairly elaborate algorithms to cache bits throughout nested types with
149
151
special constrained representations. As such it is * especially* desirable that
150
152
we leave enum layout unspecified today.
0 commit comments