Skip to content

Commit b37e8f6

Browse files
Alan Jeffreyavadacatavra
Alan Jeffrey
authored andcommitted
Added representation of pointer types. (#51)
* Added representation of pointer types. * Moved same-representation discussion of fat pointers to non-normative text * Responding to review comments * Responding to review comments * Responding to review comments * Update reference/src/representation/pointers.md Co-Authored-By: asajeffrey <[email protected]> * Responding to review comments * Added text about &str * Added bullet points making size/alignment clearer * Use *T rather than &T
1 parent b34616c commit b37e8f6

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Representation of reference and pointer types
2+
3+
### Terminology
4+
5+
Reference types are types of the form `&T`, `&mut T` or `&dyn T`.
6+
7+
Raw pointer types are types of the form `*const T` or `*mut T`.
8+
We write `*T` when the mutability attribute is unimportant.
9+
10+
### Representation
11+
12+
The alignment of `&T`, `&mut T`, `*const T` and `*mut T` are the same,
13+
and are at least the word size.
14+
15+
* If `T` is a trait, then the alignment of `&dyn T` is the word size.
16+
* If `T` is a sized type then the alignment of `&T` is the word size.
17+
* The alignment of `&[T]` is the word size.
18+
* The alignment of `&str` is the word size.
19+
* Alignment in other cases may be more than the word size (e.g., for other dynamically sized types).
20+
21+
The sizes of `&T`, `&mut T`, `*const T` and `*mut T` are the same,
22+
and are at least one word.
23+
24+
* If `T` is a trait, then the size of `&dyn T` is two words.
25+
* If `T` is a sized type then the size of `&T` is one word.
26+
* The size of `&[T]` is two words.
27+
* The size of `&str` is two words.
28+
* Size in other cases may be more than one word (e.g., for other dynamically sized types).
29+
30+
### Notes
31+
32+
The representations of `&T`, `&mut T` and `*T` are the same.
33+
34+
We do not make any guarantees about the representation of
35+
multi-trait objects `&(dyn T + U)` or references to other dynamically sized types,
36+
other than that they are at least word-aligned, and have size at least one word.
37+
38+
The representation of `&dyn T` when `T` is a trait is the same as that of:
39+
```rust
40+
#[repr(C)]
41+
struct DynObject {
42+
data: *u8,
43+
vtable: *u8,
44+
}
45+
```
46+
47+
The representation of `&[T]` is the same as that of:
48+
```rust
49+
#[repr(C)]
50+
struct Slice<T> {
51+
ptr: *T,
52+
len: usize,
53+
}
54+
```
55+
56+
The representation of `&str` is the same as that of `&[u8]`.

0 commit comments

Comments
 (0)