Skip to content

Commit 50f250d

Browse files
committed
Change definition of memory.encoding in response to PR comments
1 parent 5465cc0 commit 50f250d

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/memory-model.md

+27-6
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,52 @@ The most basic unit of memory in Rust is a byte. All values in Rust are computed
1515
> While bytes in Rust are typically lowered to hardware bytes, they may contain additional values,
1616
> such as being uninitialized, or storing part of a pointer.
1717
18+
r[memory.byte.contents]
19+
Each byte may have one of the following values:
20+
1821
r[memory.byte.init]
19-
Each byte may be initialized, and contain a value of type `u8`, as well as an optional pointer fragment. When present, the pointer fragment carries [provenance][type.pointer.provenance] information.
22+
* An initialized byte containing a `u8` value and optional [provenance][type.pointer.provenance],
2023

2124
r[memory.byte.uninit]
22-
Each byte may be uninitialized.
25+
* An uninitialized byte.
2326

2427
> [!NOTE]
2528
> Uninitialized bytes do not have a value and do not have a pointer fragment.
2629
30+
> [!NOTE]
31+
> The above list is not yet guaranteed to be exhaustive.
32+
2733
## Value Encoding
2834

2935
r[memory.encoding]
3036

3137
r[memory.encoding.intro]
32-
Each type in Rust has 0 or more values, which can have operations performed on them
38+
Each type in Rust has 0 or more values, which can have operations performed on them. Values are represented in memory by encoding them
3339

3440
> [!NOTE]
3541
> `0u8`, `1337i16`, and `Foo{bar: "baz"}` are all values
3642
3743
r[memory.encoding.op]
38-
Each value of a type can be encoded into a sequence of bytes, and decoded from a sequence of bytes, which has a length equal to the size of the type.
39-
The operation to encode or decode a value is determined by the representation of the type.
44+
Each type defines a pair of properties which, together, define the representation of values of the type. The *encode* operation takes a value of the type and converts it into a sequence of bytes equal in length to the size of the type, and the *decode* operation takes such a sequence of bytes and optionally converts it into a value. Encoding occurs when a value is written to memory, and decoding occurs when a value is read from memory.
45+
46+
> [!NOTE]
47+
> Only certain byte sequences may decode into a value of a given type. For example, a byte sequence consisting of all zeroes does not decode to a value of a reference type.
48+
49+
r[memory.encoding.representation]
50+
A sequence of bytes is said to represent a value of a type, if the decode operation for that type produces that value from that sequence of bytes. The representation of a type is the partial relation between byte sequences and values those sequences represent.
4051

4152
> [!NOTE]
4253
> Representation is related to, but is not the same property as, the layout of the type.
4354
55+
r[memory.encoding.symmetric]
56+
The result of encoding a given value of a type is a sequence of bytes that represents that value.
57+
58+
> [!NOTE]
59+
> This means that a value can be copied into memory and copied out and the result is the same value.
60+
> The reverse is not necessarily true, a sequence of bytes read as a value then written to another location (called a typed copy) will not necessarily yield the same sequence of bytes. For example, a typed copy of a struct type will leave the padding bytes of that struct uninitialized.
61+
4462
r[memory.encoding.decode]
45-
If a value of type `T` is decoded from a sequence of bytes that does not correspond to a defined value, the behavior is undefined. If a value of type `T` is decoded from a sequence of bytes that contain pointer fragments, which are not used to represent the value, the pointer fragments are ignored.
63+
If a value of type `T` is decoded from a sequence of bytes that does not represent any value, the behavior is undefined.
64+
65+
> [!NOTE]
66+
> For example, it is undefined behavior to read a `0x02` byte as `bool`.

0 commit comments

Comments
 (0)