diff options
Diffstat (limited to 'rust/kernel/lib.rs')
-rw-r--r-- | rust/kernel/lib.rs | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 909d305d0be8..7e227b52b4d8 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -213,12 +213,19 @@ 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)*) => {{ + let offset: usize = ::core::mem::offset_of!($Container, $($fields)*); + let field_ptr = $field_ptr; + let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>(); + $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut()); + container_ptr }} } +/// Helper for [`container_of!`]. +#[doc(hidden)] +pub fn assert_same_type<T>(_: T, _: T) {} + /// Helper for `.rs.S` files. #[doc(hidden)] #[macro_export] |