Skip to content

Commit b3e6683

Browse files
committed
remove references to stride as separate from element size
1 parent 94abe1d commit b3e6683

File tree

1 file changed

+3
-56
lines changed

1 file changed

+3
-56
lines changed
Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
# Layout of Rust array types and slices
22

3-
## Editor's Note
4-
5-
Issue #176 clarified that as of this writing, Rust does not distinguish between size and stride, ie
6-
they are always equal.
7-
83
## Layout of Rust array types
94

10-
Array types, `[T; N]`, store `N` values of type `T` with a constant _stride_.
11-
Here, _stride_ is the distance between each pair of consecutive values within
12-
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.
138

149
The _offset_ of the first array element is `0`, that is, a pointer to the array
1510
and a pointer to its first element both point to the same memory address.
@@ -26,33 +21,11 @@ guaranteed to be the same as the layout of a C array with the same element type.
2621
> }`. Pointers to arrays are fine: `extern { fn foo(x: *const [T; N]) -> *const
2722
> [U; M]; }`, and `struct`s and `union`s containing arrays are also fine.
2823
29-
The _stride_ of the array is constant for all element pairs and it is computed
30-
as the _size_ of the element type rounded up to the next multiple of the
31-
_alignment_ of the element type.
32-
3324
### Arrays of zero-size
3425

3526
Arrays `[T; N]` have zero size if and only if their count `N` is zero or their
3627
element type `T` is zero-sized.
3728

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

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

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

0 commit comments

Comments
 (0)