Skip to content

Commit 6ce0ab5

Browse files
committed
Incorporate feedback.
1 parent 6646d85 commit 6ce0ab5

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

rust/kernel/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,34 @@ fn panic(_info: &PanicInfo) -> ! {
141141
}
142142

143143
/// Calculates the offset of a field from the beginning of the struct it belongs to.
144+
///
145+
/// # Example
146+
///
147+
///```
148+
/// struct Test {
149+
/// a: u64,
150+
/// b: u32,
151+
/// }
152+
///
153+
/// fn test() {
154+
/// // This prints `8`.
155+
/// println!("{}", offset_of!(Test, b));
156+
/// }
157+
///```
144158
#[macro_export]
145159
macro_rules! offset_of {
146160
($type:ty, $($f:tt)*) => {{
147161
let tmp = core::mem::MaybeUninit::<$type>::uninit();
148-
let ptr = tmp.as_ptr();
162+
let outer = tmp.as_ptr();
163+
// To avoid warnings when nesting `unsafe` blocks.
149164
#[allow(unused_unsafe)]
150-
let dev = unsafe { core::ptr::addr_of!((*ptr).$($f)*) as *const u8 };
165+
// SAFETY: The pointer is valid and aligned, just not initialised; `addr_of` ensures that
166+
// we don't actually dereference it (which would be UB).
167+
let inner = unsafe { core::ptr::addr_of!((*outer).$($f)*) } as *const u8;
168+
// To avoid warnings when nesting `unsafe` blocks.
151169
#[allow(unused_unsafe)]
152-
unsafe { dev.offset_from(ptr as *const u8) }
170+
// SAFETY: The two pointers are within the same allocation block.
171+
unsafe { inner.offset_from(outer as *const u8) }
153172
}}
154173
}
155174

0 commit comments

Comments
 (0)