#60601 adds:
impl<T: ?Sized> *mut T {
/// Cast to a pointer to a different type
#[inline]
pub const fn cast<U>(self) -> *mut U {
self as _
}
}
impl<T: ?Sized> *const T {
/// Cast to a pointer to a different type
#[inline]
pub const fn cast<U>(self) -> *const U {
self as _
}
}
Like with NonNull::cast, the input pointed type does not need to be Sized but the output pointed type does, because we wouldn’t know what appropriate pointer metadata (slice length, trait object vtable, …) to insert. The actual requirement is that the pointer is thin, but Sized is the closest approximation we have (until we add something like T: ptr::Pointee<Metadata=()>).
#60601 adds:
Like with
NonNull::cast, the input pointed type does not need to beSizedbut the output pointed type does, because we wouldn’t know what appropriate pointer metadata (slice length, trait object vtable, …) to insert. The actual requirement is that the pointer is thin, butSizedis the closest approximation we have (until we add something likeT: ptr::Pointee<Metadata=()>).