Skip to content

Create assert_field_offsets! macro #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/assert_field_offsets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/// Asserts that the type has the given field offsets.
///
/// Due to implementation details, this only works for [`Sized`] types.
///
/// # Safety
///
/// This is implemented by creating a dangling reference to an uninitialized
/// memory. Currently, this works on supported Rust versions (as of this
/// writing: 1.37 and 1.38). However, a test that is expected to compile today
/// may not in the future.
///
/// # Examples
///
/// Given some type whose fields can't be re-ordered because of `#[repr(C)]`:
///
/// ```
/// #[repr(C)]
/// struct Foo {
/// start: u32,
/// end: u64,
/// }
/// ```
///
/// The following example fails to compile because, despite `start` being 4
/// bytes, `end` is aligned to 8 bytes and thus requires an offset of 8.
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions;
/// # #[repr(C)] struct Foo { start: u32, end: u64 }
/// assert_field_offsets!(Foo { end: == 4 });
/// ```
///
/// However, the correct assumptions are upheld below:
///
/// ```
/// # #[macro_use] extern crate static_assertions;
/// # #[repr(C)] struct Foo { start: u32, end: u64 }
/// assert_field_offsets!(Foo {
/// start: == 0,
/// end: == 8,
/// });
/// ```
///
/// This macro even works with tuple `struct`s:
///
/// ```
/// # #[macro_use] extern crate static_assertions;
/// struct Bar(u32, u32);
///
/// assert_field_offsets!(Bar { 1: == 4 });
/// ```
///
/// [`Sized`]: https://doc.rust-lang.org/std/marker/trait.Sized.html
#[macro_export]
macro_rules! assert_field_offsets {
($t:ty { $($f:tt: == $o:expr),+ $(,)? }) => {
$(
const _: [(); $o] = [(); unsafe {
// FIXME: Creating a reference to uninitialized memory is
// undefined behavior, but does that apply at compile time or
// only at run time?
// TODO: Make this work with unsized types
union Cast<T: 'static> {
ptr: &'static T,
addr: usize,
}

let align = $crate::_core::mem::align_of::<$t>();
let ptr: &$t = Cast { addr: align }.ptr;

Cast { ptr: &ptr.$f }.addr - align
}];
)+
};
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ mod assert_cfg;
mod assert_eq_align;
mod assert_eq_size;
mod assert_fields;
mod assert_field_offsets;
mod assert_impl;
mod assert_obj_safe;
mod assert_type;
Expand Down
14 changes: 14 additions & 0 deletions tests/field_offsets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_std]
#![allow(dead_code)]
#![deny(unsafe_code)]

#[macro_use]
extern crate static_assertions;

#[repr(C)]
struct Foo(u32, u64);

assert_field_offsets!(Foo {
0: == 0,
1: == 8,
});