Skip to content

Commit 8f24543

Browse files
authored
Rollup merge of #111665 - est31:offset_of_tests, r=WaffleLapkin
Add more tests for the offset_of macro Implements what I [suggested in the tracking issue](rust-lang/rust#106655 (comment)), plus some further improvements: * ensuring that offset_of!(Self, ...) works iff inside an impl block * ensuring that the output type is usize and doesn't coerce. this can be changed in the future, but if it is done, it should be a conscious decision * improving the privacy checking test * ensuring that generics don't let you escape the unsized check r? `````@WaffleLapkin`````
2 parents 06cbe6b + 9f614c1 commit 8f24543

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

core/tests/mem.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,21 @@ fn offset_of() {
386386
// Layout of tuples is unstable
387387
assert!(offset_of!((u8, u16), 0) <= size_of::<(u8, u16)>() - 1);
388388
assert!(offset_of!((u8, u16), 1) <= size_of::<(u8, u16)>() - 2);
389+
390+
#[repr(C)]
391+
struct Generic<T> {
392+
x: u8,
393+
y: u32,
394+
z: T
395+
}
396+
397+
// Ensure that this type of generics works
398+
fn offs_of_z<T>() -> usize {
399+
offset_of!(Generic<T>, z)
400+
}
401+
402+
assert_eq!(offset_of!(Generic<u8>, z), 8);
403+
assert_eq!(offs_of_z::<u8>(), 8);
389404
}
390405

391406
#[test]

0 commit comments

Comments
 (0)