Skip to content

Commit 10c0899

Browse files
authored
Merge pull request #447 from JakobDegen/stride
Clarify size == stride
2 parents 89eef38 + b3e6683 commit 10c0899

File tree

1 file changed

+3
-51
lines changed

1 file changed

+3
-51
lines changed

reference/src/layout/arrays-and-slices.md

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## Layout of Rust array types
44

5-
Array types, `[T; N]`, store `N` values of type `T` with a constant _stride_.
6-
Here, _stride_ is the distance between each pair of consecutive values within
7-
the array.
5+
Array types, `[T; N]`, store `N` values of type `T` with a _stride_ that is
6+
equal to the size of `T`. Here, _stride_ is the distance between each pair of
7+
consecutive values within the array.
88

99
The _offset_ of the first array element is `0`, that is, a pointer to the array
1010
and a pointer to its first element both point to the same memory address.
@@ -21,33 +21,11 @@ guaranteed to be the same as the layout of a C array with the same element type.
2121
> }`. Pointers to arrays are fine: `extern { fn foo(x: *const [T; N]) -> *const
2222
> [U; M]; }`, and `struct`s and `union`s containing arrays are also fine.
2323
24-
The _stride_ of the array is constant for all element pairs and it is computed
25-
as the _size_ of the element type rounded up to the next multiple of the
26-
_alignment_ of the element type.
27-
2824
### Arrays of zero-size
2925

3026
Arrays `[T; N]` have zero size if and only if their count `N` is zero or their
3127
element type `T` is zero-sized.
3228

33-
### Special case `stride == size`
34-
35-
When the element _size_ is a multiple of the element's _alignment_, then `stride
36-
== size`, and the elements are laid out contiguously in memory, e.g., `[u8; 4]`.
37-
In this case, the _size_ of the array can be computed as `size_of::<T>() * N`,
38-
and a pointer to the `i`-th element of the array can be obtained by offsetting a
39-
pointer to the first element of the array by `i`[^1].
40-
41-
> **Note:** In the current Rust implementation, _size_ is always a multiple of
42-
> the element's _alignment_, and therefore `stride == size` always holds. This
43-
> is, however, not guaranteed by the [layout of structs and tuples].
44-
45-
[^1]: When `stride > size` the pointer needs to be advanced by the array
46-
_stride_ instead of by the element _size_.
47-
48-
[layout of structs and tuples]: ./structs-and-tuples.md
49-
50-
5129
### Layout compatibility with packed SIMD vectors
5230

5331
The [layout of packed SIMD vector types][Vector] [^2] requires the _size_ and
@@ -63,29 +41,3 @@ type and the same number of elements as the vector.
6341
## Layout of Rust slices
6442

6543
The layout of a slice `[T]` of length `N` is the same as that of a `[T; N]` array.
66-
67-
## Unresolved questions
68-
69-
### Guaranteeing `stride == size` ?
70-
71-
Currently, the [layout of structs and tuples] does not guarantee that the
72-
element _size_ is a multiple of its _alignment_. For example, consider:
73-
74-
```rust,ignore
75-
struct A(u16, u8);
76-
type B = [A; 4];
77-
```
78-
79-
In the current Rust implementation, `A` has an alignment of `2` and a size of `4`,
80-
and `B` has a size of `16`, such that `B` contains four `A`s that are contiguously
81-
laid in memory.
82-
83-
However, a future Rust implementation could implement a layout optimization that
84-
reduces the size of `A` to `3`. For the elements of `B` to be properly aligned,
85-
`B` would need to choose a `stride == 4`, resulting in a `stride > size`.
86-
87-
Guaranteeing `stride >= size` is forward-compatible with such
88-
layout-optimization proposals:
89-
90-
* [rust-lang/rfcs/1397: Spearate size and stride for types](https://github.com/rust-lang/rfcs/issues/1397)
91-
* [rust-lang/rust/17027: Collapse trailing padding](https://github.com/rust-lang/rust/issues/17027)

0 commit comments

Comments
 (0)