1
1
# Layout of scalar types
2
2
3
3
This chapter represents the consensus from issue [ #9 ] . It documents the memory
4
- layout and considerations for ` bool ` , ` char ` , floating point types (` f{32, 64} ` ), and integral types (` {i,u}{8,16,32,64,128,size} ` ).
4
+ layout and considerations for ` bool ` , ` char ` , floating point types (` f{32, 64} ` ),
5
+ and integral types (` {i,u}{8,16,32,64,128,size} ` ).
5
6
6
7
These types are all scalar types, representing a single value, and have no
7
8
layout ` #[repr()] ` flags.
@@ -10,9 +11,9 @@ layout `#[repr()]` flags.
10
11
11
12
## ` bool `
12
13
13
- Rust's ` bool ` has the same layout as C17's` _Bool ` , that is, its size and
14
- alignment are implementation-defined. Any ` bool ` can be cast into an integer,
15
- taking on the values 1 (` true ` ) or 0 (` false ` ).
14
+ Rust's ` bool ` has the same layout as C17's` _Bool ` , that is, its size
15
+ and alignment are [ implementation-defined] [ data-layout ] . Any ` bool ` can be
16
+ cast into an integer, taking on the values 1 (` true ` ) or 0 (` false ` ).
16
17
17
18
> ** Note** : on all platforms that Rust's currently supports, its size and
18
19
> alignment are 1, and its ABI class is ` INTEGER ` - see [ Rust Layout and ABIs] .
@@ -22,7 +23,7 @@ taking on the values 1 (`true`) or 0 (`false`).
22
23
## ` char `
23
24
24
25
Rust char is 32-bit wide and represents an [ unicode scalar value] . The alignment
25
- of ` char ` is _ implementation-defined _ .
26
+ of ` char ` is [ implementation-defined ] [ data-layout ] .
26
27
27
28
[ unicode scalar value ] : http://www.unicode.org/glossary/#unicode_scalar_value
28
29
@@ -38,7 +39,7 @@ They have the same layout as the [pointer types] for which the pointee is
38
39
` Sized ` , and are layout compatible with C's ` uintptr_t ` and ` intptr_t ` types.
39
40
40
41
> ** Note** : C99 [ 7.18.2.4] ( https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4 )
41
- > requires ` uintptr_t ` and ` intptr_t ` to be at least 16-bit wide. All
42
+ > requires ` uintptr_t ` and ` intptr_t ` to be at least 16-bit wide. All
42
43
> platforms we currently support have a C platform, and as a consequence,
43
44
> ` isize ` /` usize ` are at least 16-bit wide for all of them.
44
45
@@ -48,15 +49,15 @@ They have the same layout as the [pointer types] for which the pointee is
48
49
49
50
> ** Note** : in the current Rust implementation, the layouts of ` isize ` and
50
51
> ` usize ` determine the following:
51
- >
52
+ >
52
53
> * the maximum size of Rust _ allocations_ is limited to ` isize::MAX ` .
53
54
> The LLVM ` getelementptr ` instruction uses signed-integer field offsets. Rust
54
55
> calls ` getelementptr ` with the ` inbounds ` flag which assumes that field
55
56
> offsets do not overflow,
56
57
>
57
- > * the maximum number of elements in an array is ` usize::MAX ` (`[ T; N:
58
- > usize ] `. Only ZST arrays can probably be this large in practice, non-ZST
59
- > arrays are bound by the maximum size of Rust values,
58
+ > * the maximum number of elements in an array is ` usize::MAX ` (` [T; N: usize] ` ).
59
+ > Only ZST arrays can probably be this large in practice, non-ZST arrays
60
+ > are bound by the maximum size of Rust values,
60
61
>
61
62
> * the maximum value in bytes by which a pointer can be offseted using
62
63
> ` ptr.add ` or ` ptr.offset ` is ` isize::MAX ` .
@@ -85,7 +86,7 @@ The alignment of Rust's `{i,u}128` is _unspecified_ and allowed to change.
85
86
> ** Note** : While the C standard does not define fixed-width 128-bit wide
86
87
> integer types, many C compilers provide non-standard ` __int128 ` types as a
87
88
> language extension. The layout of ` {i,u}128 ` in the current Rust
88
- > implementation does ** not** match that of these C types, see
89
+ > implementation does ** not** match that of these C types, see
89
90
> [ rust-lang/#54341 ] ( https://github.com/rust-lang/rust/issues/54341 ) .
90
91
91
92
### Layout compatibility with C native integer types
@@ -94,7 +95,7 @@ The specification of native C integer types, `char`, `short`, `int`, `long`,
94
95
... as well as their ` unsigned ` variants, guarantees a lower bound on their size,
95
96
e.g., ` short ` is _ at least_ 16-bit wide and _ at least_ as wide as ` char ` .
96
97
97
- Their exact sizes are _ implementation-defined_ .
98
+ Their exact sizes are _ implementation-defined_ .
98
99
99
100
Libraries like ` libc ` use knowledge of this _ implementation-defined_ behavior on
100
101
each platform to select a layout-compatible Rust fixed-width integer type when
@@ -123,3 +124,4 @@ _implementation-defined_.
123
124
> ` libc::c_double ` types that can be used to safely interface with C via FFI.
124
125
125
126
[ IEEE-754 ] : https://en.wikipedia.org/wiki/IEEE_754
127
+ [ data-layout ] : https://doc.rust-lang.org/nightly/reference/type-layout.html#primitive-data-layout
0 commit comments