Skip to content

Commit 2f75e78

Browse files
committed
Move aggregate values into appropriate chapters
1 parent dbafce3 commit 2f75e78

File tree

7 files changed

+60
-65
lines changed

7 files changed

+60
-65
lines changed

src/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
- [Type coercions](type-coercions.md)
100100
- [Destructors](destructors.md)
101101
- [Lifetime elision](lifetime-elision.md)
102-
- [Values and representation](values.md)
103102

104103
- [Special types and traits](special-types-and-traits.md)
105104

src/types/array.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ always bounds-checked in safe methods and operators.
3131
> Note: The [`Vec<T>`] standard library type provides a heap-allocated resizable
3232
> array type.
3333
34+
r[type.array.repr]
35+
The values and representation of a tuple type are the same as a [struct type][type.struct.value] with `N` fields of type `T` corresponding to each index in order, where the fields are layed out according to the [`C` representation][layout.repr.c].
36+
3437
[_Expression_]: ../expressions.md
3538
[_Type_]: ../types.md#type-expressions
3639
[`usize`]: numeric.md#machine-dependent-integer-types

src/types/enum.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,32 @@ unit-like struct.
1414
r[type.enum.constructor]
1515
New instances of an `enum` can be constructed with a [struct expression].
1616

17-
r[type.enum.value]
18-
Any `enum` value consumes as much memory as the largest variant for its
19-
corresponding `enum` type, as well as the size needed to store a discriminant.
20-
2117
r[type.enum.name]
2218
Enum types cannot be denoted *structurally* as types, but must be denoted by
2319
named reference to an [`enum` item].
2420

21+
## Enum values and representation
22+
23+
r[type.enum.value]
24+
25+
r[type.enum.value.intro]
26+
An enum value corresponds to exactly one variant of the enum, and consists of the fields of that variant
27+
28+
> [!NOTE]
29+
> An enum with no variants therefore has no values.
30+
31+
r[type.enum.value.variant-padding]
32+
A byte is a padding byte in a variant `V` if the byte is not used for computing the discriminant, and the byte would be a padding byte in a struct consisting of the fields of the variant at the same offsets.
33+
34+
r[type.enum.value.value-padding]
35+
A byte is a padding byte of an enum if it is a padding byte in each variant of the enum. A byte that is not a padding byte of an enum is a value byte.
36+
37+
r[type.enum.value.repr]
38+
The representation of a value of an enum type includes the representation of each field of the variant at the appropriate offsets. When encoding a value of an enum type, each byte which is a padding byte in the variant is set to uninit. In the case of a [`repr(C)`][layout.repr.c.adt] or a [primitive-repr][layout.repr.primitive.adt] enum, the discriminant of the variant is represented as though by the appropriate integer type stored at offset 0.
39+
40+
> [!NOTE]
41+
> Most `repr(Rust)` enums will also store a discriminant in the representation of the enum, but the exact placement or type of the discriminant is unspecified, as is the value that represents each variant.
42+
2543
[^enumtype]: The `enum` type is analogous to a `data` constructor declaration in
2644
Haskell, or a *pick ADT* in Limbo.
2745

src/types/struct.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ A _unit-like struct_ type is like a struct type, except that it has no fields.
2929
The one value constructed by the associated [struct expression] is the only
3030
value that inhabits such a type.
3131

32+
## Struct and aggregate values
33+
34+
r[type.struct.value]
35+
36+
r[type.struct.value.value-bytes]
37+
A byte `b` in the representation of an aggregate is a value byte if there exists a field of that aggregate such that:
38+
* The field has some type `T`,
39+
* The offset of that field `o` is such that `b` falls at an offset in `o..(o+size_of::<T>())`,
40+
* Either `T` is a primitive type or the offset of `b` within the field is a value byte in the representation of `T`.
41+
42+
> [!NOTE]
43+
> A byte in a union is a value byte if it is a value byte in *any* field.
44+
45+
r[type.struct.value.padding]
46+
Every byte in an aggregate which is not a value byte is a padding byte.
47+
48+
r[type.struct.value.struct]
49+
A value of a struct type consists of the values of each of its fields.
50+
The representation of such a struct contains the representation of the value of each field at its corresponding offset.
51+
52+
r[type.struct.value.padding-uninit]
53+
When a value of an aggregate is encoded, each padding byte is left as uninit
54+
55+
> [!NOTE]
56+
> It is valid for padding bytes to hold a value other than uninit when decoded, and these bytes are ignored when decoding an aggregate.
57+
3258
[^structtype]: `struct` types are analogous to `struct` types in C, the
3359
*record* types of the ML family, or the *struct* types of the Lisp family.
3460

src/types/tuple.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Furthermore, various expressions will produce the unit value if there is no othe
4949
r[type.tuple.access]
5050
Tuple fields can be accessed by either a [tuple index expression] or [pattern matching].
5151

52+
r[type.tuple.repr]
53+
The values and representation of a tuple type are the same as a [struct type][type.struct.value] with the same fields and layout.
54+
5255
[^1]: Structural types are always equivalent if their internal types are equivalent.
5356
For a nominal version of tuples, see [tuple structs].
5457

src/types/union.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ The memory layout of a `union` is undefined by default (in particular, fields do
2424
*not* have to be at offset 0), but the `#[repr(...)]` attribute can be used to
2525
fix a layout.
2626

27+
r[type.union.value]
28+
A value of a union type consists of a sequence of bytes, corresponding to each [value byte][type.struct.value.value-bytes]. The value bytes of a union are represented exactly. Each [padding byte][type.struct.value.padding] is set to uninit.
29+
30+
> [!NOTE]
31+
> When a union value is constructed or a field is read/written to, the value of that field is encoded or decoded appropriately.
32+
2733
[`Copy`]: ../special-types-and-traits.md#copy
2834
[item]: ../items/unions.md

src/values.md

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)