Pointer casting to `?Sized`

Currently, pointer casting methods doesn't allow casting to a ?Sized type, see cast signature

impl<T: ?Sized> *const T {
    pub const fn cast<U>(self) -> *const U // instead of fn cast<U: ?Sized>(self) -> *const U
}

Why such a limitation?

How would you build the metadata of *const U?

For example, when casting *const i32 to *const [i32], you would need to know the size of this slice.

Indeed, you cannot cast a thin pointer to a fat pointer. In my case, I was trying to cast a fat pointer to another fat pointer, and was surprised by the limitation.

I guess it's not possible to have different signature for <*const T>::cast<U> depending on T being ?Sized or not, and that would be the reason of the ilimitation.

There are different kinds of fat pointers, too. Casting a slice pointer to a trait object pointer would be pretty bad.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.