Skip to content

[slice] Document slice DSTs, including size guarantees #117474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
26 changes: 26 additions & 0 deletions library/core/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,32 @@ mod prim_array {}
/// * Further methods that return iterators are [`.split`], [`.splitn`],
/// [`.chunks`], [`.windows`] and more.
///
/// ## Use in slice-based dynamically-sized types
///
/// Slices can be used to construct "slice-based dynamically-sized types", also
/// known as "slice DSTs" or "custom DSTs":
///
/// ```
/// struct SliceDst {
/// a: u8,
/// b: u16,
/// c: [u32],
/// }
/// ```
///
/// Just like a slice, a slice DST is a `!Sized` type, and cannot be used by-value.
/// References to slice DSTs encode the number of elements in the trailing slice
/// field:
///
/// ```
/// # struct SliceDst { a: u8, b: u16, c: [u32] }
/// let pointer_size = std::mem::size_of::<&u8>();
/// assert_eq!(2 * pointer_size, std::mem::size_of::<&SliceDst>());
/// ```
///
/// Rust guarantees that, if `T` is a slice DST, the instance of `T` with 0 elements
/// in its trailing slice is no more than `isize::MAX` bytes in size.
///
/// [`Hash`]: core::hash::Hash
/// [`.iter`]: slice::iter
/// [`.iter_mut`]: slice::iter_mut
Expand Down