macro_rules! container_of {
($field_ptr:expr, $Container:ty, $($fields:tt)*) => { ... };
}
Expand description
Produces a pointer to an object from a pointer to one of its fields.
If you encounter a type mismatch due to the Opaque
type, then use Opaque::cast_into
or
Opaque::cast_from
to resolve the mismatch.
§Safety
The pointer passed to this macro, and the pointer returned by this macro, must both be in bounds of the same allocation.
§Examples
struct Test {
a: u64,
b: u32,
}
let test = Test { a: 10, b: 20 };
let b_ptr: *const _ = &test.b;
// SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
// in-bounds of the same allocation as `b_ptr`.
let test_alias = unsafe { container_of!(b_ptr, Test, b) };
assert!(core::ptr::eq(&test, test_alias));