[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <87y1l69t5l.fsf@metaspace.dk>
Date: Tue, 30 May 2023 09:23:29 +0200
From: Andreas Hindborg <nmi@...aspace.dk>
To: Gary Guo <gary@...yguo.net>
Cc: Alice Ryhl <aliceryhl@...gle.com>, alex.gaynor@...il.com,
benno.lossin@...ton.me, bjorn3_gh@...tonmail.com,
boqun.feng@...il.com, jiangshanlai@...il.com,
linux-kernel@...r.kernel.org, ojeda@...nel.org,
patches@...ts.linux.dev, rust-for-linux@...r.kernel.org,
tj@...nel.org, wedsonaf@...il.com
Subject: Re: [PATCH v1 3/7] rust: sync: add `Arc::{from_raw, into_raw}`
Gary Guo <gary@...yguo.net> writes:
> On Thu, 25 May 2023 09:45:29 +0200
> Andreas Hindborg <nmi@...aspace.dk> wrote:
>
>> Alice Ryhl <aliceryhl@...gle.com> writes:
>>
>> > Andreas Hindborg <nmi@...aspace.dk> writes:
>> >> Alice Ryhl <aliceryhl@...gle.com> writes:
>> >>> + // This preserves the metadata in the pointer, if any.
>> >>> + let metadata = core::ptr::metadata(ptr as *const ArcInner<T>);
>> >>
>> >> I can't follow this. How does this work? `ptr` was for field
>> >> `inner.data: T`, but we are casting to `ArcInner<T>`.
>> >>
>> >>> + let ptr = (ptr as *mut u8).wrapping_sub(val_offset) as *mut ();
>> >>> + let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
>> >>
>> >> Metadata was obtained from a pointer pointing to `inner.data`, we then
>> >> move it back to beginning of `ArcInner<T>` and then reconstruct the
>> >> potentially fat pointer with metadata from the pointer to `T`? How can
>> >> this be right?
>> >
>> > The metadata of a struct is always the metadata of its last field, so
>> > both `*mut T` and `*mut ArcInner<T>` have the same metadata. Because of
>> > that, moving the metadata over from one type to the other is ok.
>> >
>> > The reason that I cast to an `ArcInner<T>` pointer before calling
>> > `metadata` is because I get a type mismatch otherwise for the metadata,
>> > since the compiler doesn't unify the metadata types when the type is
>> > generic.
>>
>> OK, cool. In that case, since this is common knowledge (is it?),
>> could you maybe include a link to the relevant documentation, or a
>> comment indicating why this is OK?
>>
>> BR Andreas
>
> This is documented in the doc of Pointee trait:
>
> https://doc.rust-lang.org/std/ptr/trait.Pointee.html
Nice. I think I forgot a _not_ in my last message. I think it would be
nice to add a comment with a link to this documentation and perhaps a
note as to why this works.
BR Andreas
Powered by blists - more mailing lists