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 2 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
27 changes: 27 additions & 0 deletions library/core/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,33 @@ 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 a slice DST compiles successfully, then the instance
/// of that type with 0 elements in its trailing slice is no more than `isize::MAX`
/// bytes in size.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "a slice DST compiles successfully" mean? Is there precedent for a statement like this somewhere else that we can follow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just meant to account for the fact that you could in principle write down a type which, were it to compile, would violate this guarantee (i.e., a type whose fixed part itself overflows isize). I can just remove that parenthetical, though, since it's implicit - obviously you only care about code that compiles.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see. Do we usually phrase this like that? "Rust guarantees that all types are no larger than isize." That's not what I would have said, I would have said that Ruts requires that all types fit in an isize, and halts compilation otherwise. But I don't know which wording we are using elsewhere.

Do we have a test for this? We have tests for large types but it might be worth having one specifically for this clause.

///
/// [`Hash`]: core::hash::Hash
/// [`.iter`]: slice::iter
/// [`.iter_mut`]: slice::iter_mut
Expand Down