Re: [PATCH v3] rust: check type of `$ptr` in `container_of!`
From: Alice Ryhl
Date: Fri Apr 25 2025 - 05:51:39 EST
On Thu, Apr 24, 2025 at 09:47:52AM -0400, Tamir Duberstein wrote:
> > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> > > index 1df11156302a..d14ed86efb68 100644
> > > --- a/rust/kernel/lib.rs
> > > +++ b/rust/kernel/lib.rs
> > > @@ -198,9 +198,15 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
> > > /// ```
> > > #[macro_export]
> > > macro_rules! container_of {
> > > - ($ptr:expr, $type:ty, $($f:tt)*) => {{
> > > - let offset: usize = ::core::mem::offset_of!($type, $($f)*);
> > > - $ptr.byte_sub(offset).cast::<$type>()
> > > + ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
> >
> > It's rather unusual to use an uppercase C in the name of this parameter.
>
> I took the parameter name from `offset_of`:
> https://doc.rust-lang.org/stable/std/mem/macro.offset_of.html.
https://github.com/rust-lang/rust/pull/140285