File tree 1 file changed +4
-3
lines changed 1 file changed +4
-3
lines changed Original file line number Diff line number Diff line change @@ -197,7 +197,8 @@ macro_rules! offset_of {
197
197
/// # Safety
198
198
///
199
199
/// Callers must ensure that the pointer to the field is in fact a pointer to the specified field,
200
- /// as opposed to a pointer to another object of the same type.
200
+ /// as opposed to a pointer to another object of the same type. If this condition is not met,
201
+ /// any dereference of the resulting pointer is UB.
201
202
///
202
203
/// # Example
203
204
///
@@ -212,7 +213,7 @@ macro_rules! offset_of {
212
213
/// fn test() {
213
214
/// let test = Test { a: 10, b: 20 };
214
215
/// let b_ptr = &test.b;
215
- /// let test_alias = unsafe { container_of!(b_ptr, Test, b) } ;
216
+ /// let test_alias = container_of!(b_ptr, Test, b);
216
217
/// // This prints `true`.
217
218
/// pr_info!("{}\n", core::ptr::eq(&test, test_alias));
218
219
/// }
@@ -222,6 +223,6 @@ macro_rules! container_of {
222
223
( $ptr: expr, $type: ty, $( $f: tt) * ) => { {
223
224
let ptr = $ptr as * const _ as * const u8 ;
224
225
let offset = $crate:: offset_of!( $type, $( $f) * ) ;
225
- unsafe { ptr. offset ( -offset) as * const $type }
226
+ ptr. wrapping_offset ( -offset) as * const $type
226
227
} }
227
228
}
You can’t perform that action at this time.
0 commit comments