Why don't slices use dedicated metadata structs?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Curious question: why don't slices also use dedicated wrapper structs? Why just &dyn Trait?
I'd think std::ptr::metadata(slice).len() would be clearer than std::ptr::metadata(slice), for example. You could also add other helper methods, to simplify common tasks that might use it, and the added type safety would make it safer to use. (Safe enough, in fact, for you to entirely replace slice construction APIs with it.)
Concretely, I'm thinking of the following, for &str and &[T]:
// Implements each of the following traits, independent of `T` (can't use `derive`):
// - Debug
// - Clone
// - Copy
// - PartialEq
// - Eq
// - PartialOrd
// - Ord
// - Hash
pub struct SliceMetadata<T> {
len: usize,
_phantom: core::marker::PhantomData<T>,
}
impl<T> SliceMetadata<T> {
/// Create a new synthetic metadata, to pair with a pointer.
const fn new(len: usize) -> Self {
Self {
len: self.len,
_phantom: core::marker::PhantomData,
}
}
/// Returns the stored length
const fn len(&self) -> usize {
self.len
}
/// Aligns with `DynMetadata::size_of`
const fn size_of(&self) -> usize {
self.len * core::mem::size_of::<T>()
}
/// Returns a `Some` of a new metadata for an array of `U`s, with the same
/// byte length of this slice, or `None` if this cannot be done exactly.
///
/// This mirrors array transmutation.
const fn transmute<U>(&self) -> Option<SliceMetadata<U>> {
let src_width = core::mem::size_of::<T>();
let dest_width = core::mem::size_of::<U>();
if src_width == dest_width {
// Easy path, works for everything
Some(SliceMetadata::new(self.len))
} else if self.len == 0 {
// Easy path, works for everything
Some(SliceMetadata::new(0))
} else if src_width == 0 {
// Easy path, works for everything
Some(SliceMetadata::new(if dest_width == 0 { self.len } else { 0 }))
} else if dest_width == 0 {
// can't transmute from non-zero-sized `[A; N]` to zero-sized `[B; M]`
None
} else {
// Time to transmute
let bytes = self.len * src_width;
if bytes % dest_width != 0 {
None
} else {
Some(SliceMetadata::new(bytes / dest_width))
}
}
}
}
Also, part of the idea here is to replace slice::from_raw_parts{,_mut} and ptr::slice_from_raw_parts{,_mut} with a less error-prone alternative similarly to how mem::MaybeUninit replaced mem::uninit. So, instead of this:
// Create a slice
let slice = std::slice::from_raw_parts(ptr, len);
// Create a slice pointer
let slice_ptr = std::ptr::slice_from_raw_parts(ptr, len);
// Transmute a slice
let transmuted = std::slice::from_raw_parts(
slice.as_ptr().cast::<U>(),
slice.len() / std::mem::size_of::<T>() * std::mem::size_of::<U>()
);
// Cast a slice pointer
let (ptr, len) = std::ptr::metadata(slice_ptr);
let casted_ptr = std::ptr::slice_from_raw_parts(
ptr.cast::<U>(),
len / std::mem::size_of::<T>() * std::mem::size_of::<U>()
);
You'd do this instead:
// Create a slice
let slice: &[T] = &*std::ptr::from_raw_parts(ptr, SliceMetadata::new(len));
// Create a slice pointer
let slice_ptr: *const [T] = std::ptr::from_raw_parts(ptr, SliceMetadata::new(len));
// Transmute a slice
let (ptr, metadata) = (slice as *const _).to_raw_parts();
let transmuted: &[U] = &*std::ptr::from_raw_parts(ptr, metadata.transmute().unwrap());
// Cast a slice pointer
let (ptr, metadata) = slice_ptr.to_raw_parts();
let transmuted_ptr: *const [U] = std::ptr::from_raw_parts(ptr, metadata.transmute().unwrap());
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing std::ptr::metadata, slice::from_raw_parts, and ptr::slice_from_raw_parts APIs, then compare their constraints with the proposed SliceMetadata design. Done would require a settled API design and maintainer consensus on dedicated slice metadata, helper methods, and replacement construction APIs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100