Re: [PATCH] rust: stop using ptr_metadata feature

From: Alice Ryhl
Date: Thu Feb 08 2024 - 09:31:36 EST


On Mon, Feb 5, 2024 at 10:02 PM Trevor Gross <tmgross@xxxxxxxxx> wrote:
>
> On Mon, Feb 5, 2024 at 3:19 AM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> > // SAFETY: The metadata of `T` and `ArcInner<T>` is the same because `ArcInner` is a struct
> > // with `T` as its last field.
> > //
> > // This is documented at:
> > // <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
>
> The comment should be reworded, no more metadata and no unsafe block
> so it doesn't have to be SAFETY.

How about this?

// Pointer casts leave the metadata unchanged. This is okay because
the metadata of `T` and
// `ArcInner<T>` is the same since `ArcInner` is a struct with `T` as
its last field.
//
// This is documented at:
// <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.

> > - let metadata: <ArcInner<T> as Pointee>::Metadata =
> > - unsafe { core::mem::transmute_copy(&metadata) };
> > + let ptr = ptr as *mut ArcInner<T>;
>
> Nit: this could be `.cast::<ArcInner<T>>().cast_mut()` to make the
> intentional mutability change clear.

The `.cast()` method can't be used here. It only works for sized types.

Alice