Re: [PATCH v3 09/10] rust: list: support heterogeneous lists

Alice Ryhl
2024-08-01T12:33:32Z

On Thu, Aug 1, 2024 at 12:50 PM Benno Lossin <benno.lossin@proton.me> wrote:
>
> On 01.08.24 11:38, Alice Ryhl wrote:
> > On Thu, Aug 1, 2024 at 11:24 AM Benno Lossin <benno.lossin@proton.me> wrote:
> >>
> >> On 23.07.24 10:22, Alice Ryhl wrote:
> >>> @@ -181,6 +185,47 @@ unsafe fn from_fields(me: *mut ListLinksFields) -> *mut Self {
> >>> }
> >>> }
> >>>
> >>> +/// Similar to [`ListLinks`], but also contains a pointer to the full value.
> >>> +///
> >>> +/// This type can be used instead of [`ListLinks`] to support lists with trait objects.
> >>> +#[repr(C)]
> >>> +pub struct ListLinksSelfPtr<T: ?Sized, const ID: u64 = 0> {
> >>> + /// The `ListLinks` field inside this value.
> >>> + ///
> >>> + /// This is public so that it can be used with `impl_has_list_links!`.
> >>> + pub inner: ListLinks<ID>,
> >>> + self_ptr: UnsafeCell<MaybeUninit<*const T>>,
> >>
> >> Why do you need `MaybeUninit`?
> >
> > Right now the constructor initializes it to MaybeUninit::zeroed().
> > What would you initialize it to without MaybeUninit? Remember that the
> > vtable pointer in a fat pointer has strict validity requirements.
>
> Oh... I forgot about that, can you add a comment about that? Also why
> not use `Opaque` in that case then?

It used to just be UnsafeCell, but then I tried it in miri and found
out about the issue and added MaybeUninit. But I can make it use
Opaque instead.

Alice