Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/dynamically-sized-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ r[dynamic-sized.restriction]
Such types can only be used in certain cases:

r[dynamic-sized.pointer-types]
* [Pointer types] to <abbr title="dynamically sized types">DSTs</abbr> are sized but have twice the size of pointers to sized types, since they also store *metadata*:
* Pointers to slices store the number of elements; pointers to `str` store the length in bytes.
* Pointers to trait objects store a pointer to a vtable.
* Pointers to a struct or tuple with an [unsized tail] store the same metadata as a pointer to that tail.
* [Pointer types] to <abbr title="dynamically sized types">DSTs</abbr> are sized but have twice the size of pointers to sized types, since they also store *metadata*. The type of the metadata depends on the type of the [unsized tail]:
* If the unsized tail is a slice, metadata stores the number of elements.
* If the unsized tail is a `str`, metadata stores the length in bytes.
* if the unsized tail is a trait object, metadata stores a pointer to a vtable.

r[dynamic-sized.question-sized]
* <abbr title="dynamically sized types">DSTs</abbr> can be provided as type arguments to generic type parameters having the special `?Sized` bound. They can also be used for associated type definitions when the corresponding associated type declaration has a `?Sized` bound. By default, any type parameter or associated type has a `Sized` bound, unless it is relaxed using `?Sized`.
Expand All @@ -27,7 +27,10 @@ r[dynamic-sized.struct-field]
> [Variables], function parameters, [const] items, and [static] items must be `Sized`.

r[dynamic-sized.tail]
The *unsized tail* of a type is the dynamically sized component that the [metadata] of a pointer to the type describes. A [slice] (`[T]`) and a [`str`] are each their own unsized tail, described by a length; a [trait object] (`dyn Trait`) is its own unsized tail, described by a pointer to a vtable. When a struct (per [dynamic-sized.struct-field]) or a tuple has an unsized last field, its unsized tail is the unsized tail of that field. A sized type has no unsized tail.
The *unsized tail* of a type is the dynamically sized component at the "end" of the type, after recursively descending through compound types:
- A [slice] (`[T]`), [`str`], and [trait object] (`dyn Trait`) are each their own unsized tail.
- When a struct (per [dynamic-sized.struct-field]) or a tuple has an unsized last field, its unsized tail is the unsized tail of that field.
- A sized type has no unsized tail.

[metadata]: dynamic-sized.pointer-types
[sized]: special-types-and-traits.md#sized
Expand Down
Loading