Skip to content

Why don't slices use dedicated metadata structs? #125517

Open
@dead-claudia

Description

@dead-claudia

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());

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions