Skip to content

Commit

Permalink
Layout of arrays
Browse files Browse the repository at this point in the history
Closes rust-lang#91 .
  • Loading branch information
gnzlbg committed Mar 6, 2019
1 parent 19f4223 commit 8582d8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
33 changes: 33 additions & 0 deletions reference/src/layout/arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Layout of Rust array types

Array types, `[T; N]`, store `N` values of type `T` contiguously with a constant
_stride_ (the distance between each two consecutive values).

The offset of the first array element is `0`.

The stride of the array is computed as the size of the element type rounded up
to the next multiple of the alignment of the element type. That is, the _stride_
of an array can be larger than the element size iff the alignment requirements
of the element are larger than its size.

Having a stride larger than the element size allows:

```rust,ignore
struct A(u16, u8); // size_of == 3, align_of == 4
type B = [A; 4]; // => stride == 4 > 3
```

The size and alignment of `Vector` element types match, such that `Vector` types
and arrays are layout compatible.

`repr(C)` arrays have the same layout as C arrays and are passed by pointer in C
FFI according to the C ABI.

## Unresolved questions

### Stride > size

The current layout guarantees for `repr(Rust)` structs guarantee that Rust is forward-compatible with proposals allowing `stride > size`, like:

* [rust-lang/rfcs/1397: Spearate size and stride for types](https://github.com/rust-lang/rfcs/issues/1397)
* [rust-lang/rust/17027: Collapse trailing padding](https://github.com/rust-lang/rust/issues/17027)
5 changes: 3 additions & 2 deletions reference/src/layout/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ currently different for each architecture.
## Vector types

Vector types are `repr(simd)` homogeneous tuple-structs containing `N` elements
of type `T` where `N` is a power-of-two:
of type `T` where `N` is a power-of-two and the size and alignment requirements
of `T` are equal:

```rust
#[repr(simd)]
struct Vector<T, N>(T_0, ..., T_(N - 1));
```

The set of supported values of `T` and `N` is _implementation-defined_.
The exact set of supported values of `T` and `N` is _implementation-defined_.

The size of `Vector` is `N * size_of::<T>()` and its alignment is an
_implementation-defined_ function of `T` and `N` greater than or equal to
Expand Down

0 comments on commit 8582d8f

Please sign in to comment.