#offset #fields #assert #struct #asserting #memory #macro

macro assert-offset

Derive macro for asserting the memory offset of fields in a struct

2 releases

new 0.1.1 Jan 14, 2025
0.1.0 Jan 14, 2025

#391 in Procedural macros

Download history 158/week @ 2025-01-12

158 downloads per month

MIT license

5KB
51 lines

assert-offset

assert-offset is a simple Rust derive macro for asserting memory offsets within structures. This can be useful for low level FFI and/or embedded development.

⚠ This crate does not change field offsets, it merely asserts that they are at the expected offsets.

Examples

Usage

use assert_offset::AssertOffsets;

#[derive(AssertOffsets)]
#[repr(C)]
pub struct Foo {
    // Try reordering these fields or changing their types
    pub a: u8,
    #[offset(0x2)]
    pub b: u16,
}

Failing Example

use assert_offset::AssertOffsets;

// Note that we're not using #[repr(C)] here
#[derive(AssertOffsets)]
pub struct Foo {
    pub a: u8,

     // Rust (usually) puts this field at the start of the struct,
     // because u16 has a higher alignment requirement than u8
    #[offset(0x2)] // Changing this offset to 0x0 (should) fix the error
    pub b: u16,
}

Compiler Error

error[E0080]: evaluation of constant value failed
 --> src\main.rs:3:10
  |
3 | #[derive(AssertOffsets)]
  |          ^^^^^^^^^^^^^ the evaluated program panicked at 'Field `Foo::b` is not at expected offset 0x2'

License

This project is licensed under the MIT License.

Dependencies

~220–660KB
~16K SLoC