2
2
3
3
### Terminology
4
4
5
- Reference types are types of the form ` &T ` , ` &mut T ` or ` &dyn T ` .
5
+ Reference types are types of the form ` &T ` , ` &mut T ` .
6
6
7
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
8
10
9
### Representation
11
10
12
11
The alignment of ` &T ` , ` &mut T ` , ` *const T ` and ` *mut T ` are the same,
13
12
and are at least the word size.
14
13
15
- * If ` T ` is a trait, then the alignment of ` &dyn T ` is the word size.
16
14
* If ` T ` is a sized type then the alignment of ` &T ` is the word size.
15
+ * The alignment of ` &dyn Trait ` is the word size.
17
16
* The alignment of ` &[T] ` is the word size.
18
17
* The alignment of ` &str ` is the word size.
19
18
* Alignment in other cases may be more than the word size (e.g., for other dynamically sized types).
20
19
21
20
The sizes of ` &T ` , ` &mut T ` , ` *const T ` and ` *mut T ` are the same,
22
21
and are at least one word.
23
22
24
- * If ` T ` is a trait, then the size of ` &dyn T ` is two words.
25
23
* If ` T ` is a sized type then the size of ` &T ` is one word.
24
+ * The size of ` &dyn Trait ` is two words.
26
25
* The size of ` &[T] ` is two words.
27
26
* The size of ` &str ` is two words.
28
27
* Size in other cases may be more than one word (e.g., for other dynamically sized types).
29
28
30
29
### Notes
31
30
32
- The layouts of ` &T ` , ` &mut T ` and ` *T ` are the same.
31
+ The layouts of ` &T ` , ` &mut T ` , ` *const T ` and ` *mut T ` are the same.
33
32
34
33
If ` T ` is sized, references and pointers to ` T ` have a size and alignment of one
35
34
word and have therefore the same layout as C pointers.
@@ -40,25 +39,30 @@ word and have therefore the same layout as C pointers.
40
39
> or, in the case of ` &mut T ` , aliasing.
41
40
42
41
We do not make any guarantees about the layout of
43
- multi-trait objects ` &(dyn T + U ) ` or references to other dynamically sized types,
42
+ multi-trait objects ` &(dyn Trait1 + Trait2 ) ` or references to other dynamically sized types,
44
43
other than that they are at least word-aligned, and have size at least one word.
45
44
46
- The layout of ` &dyn T ` when ` T ` is a trait is the same as that of:
47
- ``` rust,ignore
45
+ The layout of ` &dyn Trait ` when ` Trait ` is a trait is the same as that of:
46
+ ``` rust
48
47
#[repr(C )]
49
48
struct DynObject {
50
- data: *u8,
51
- vtable: *u8,
49
+ data : * const u8 ,
50
+ vtable : * const u8 ,
52
51
}
53
52
```
54
53
54
+ > ** note** : In the layout of ` &dyn mut Trait ` the field ` data ` is of the type ` *mut u8 ` .
55
+
55
56
The layout of ` &[T] ` is the same as that of:
56
- ``` rust,ignore
57
+ ``` rust
57
58
#[repr(C )]
58
59
struct Slice <T > {
59
- ptr: *T,
60
+ ptr : * const T ,
60
61
len : usize ,
61
62
}
62
63
```
63
64
64
- The layout of ` &str ` is the same as that of ` &[u8] ` .
65
+ > ** note** : In the layout of ` &mut [T] ` the field ` ptr ` is of the type ` *mut T ` .
66
+
67
+ The layout of ` &str ` is the same as that of ` &[u8] ` , and the layout of ` &mut str ` is
68
+ the same as that of ` &mut [u8] ` .
0 commit comments