mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: Tamir Duberstein <tamird@gmail.com>, Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: retain pointer mut-ness in `container_of!`
Date: Fri, 11 Apr 2025 12:34:59 +0000	[thread overview]
Message-ID: <D93TAL6PHO34.2WCMTNVGUY522@proton.me> (raw)
In-Reply-To: <CAJ-ks9kEMwpDoys=RJGfWvyLdRco_Lm1984KSmju7mio5_5_WA@mail.gmail.com>

On Fri Apr 11, 2025 at 2:25 PM CEST, Tamir Duberstein wrote:
> On Fri, Apr 11, 2025 at 5:02 AM Alice Ryhl <aliceryhl@google.com> wrote:
>> On Wed, Apr 09, 2025 at 10:43:16AM -0400, Tamir Duberstein wrote:
>> > @@ -199,9 +199,8 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
>> >  #[macro_export]
>> >  macro_rules! container_of {
>> >      ($ptr:expr, $type:ty, $($f:tt)*) => {{
>> > -        let ptr = $ptr as *const _ as *const u8;
>> >          let offset: usize = ::core::mem::offset_of!($type, $($f)*);
>> > -        ptr.sub(offset) as *const $type
>> > +        $ptr.byte_sub(offset).cast::<$type>()
>> >      }}
>> >  }
>>
>> This implementation does not check the type of `ptr`. Would we not want
>> it to have the type of the field?
>
> I might be missing it but ISTM that the current implementation doesn't
> check that either.
>
> It's not obvious to me how you'd implement such a check; given `$ptr`
> and `$f`, how do you get your hands on the type of `$ptr->$($f)*`?

I don't think it's possible with current rust, but maybe with field
projection (:

    ($ptr:expr, $type:ty, $($f:tt)*) => {{
        // do not run this code, only use it for type-checking:
        let _ = || {
            let mut ptr = $ptr;
            ptr = $ptr.cast::<<field_of!($t, $($f)*) as Field>::Type>();
        };
        let offset: usize = ::core::mem::offset_of!($type, $($f)*);
        $ptr.byte_sub(offset).cast::<$type>()
    }}

---
Cheers,
Benno


  reply	other threads:[~2025-04-11 12:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-09 14:43 Tamir Duberstein
2025-04-09 14:48 ` Tamir Duberstein
2025-04-11  9:02 ` Alice Ryhl
2025-04-11 12:25   ` Tamir Duberstein
2025-04-11 12:34     ` Benno Lossin [this message]
2025-04-11 12:37       ` Alice Ryhl
2025-04-11 13:08         ` Tamir Duberstein
2025-04-11 13:29           ` Alice Ryhl
2025-04-11 14:34             ` Tamir Duberstein
2025-05-29 11:59 ` Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=D93TAL6PHO34.2WCMTNVGUY522@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®