assert-offset

Crates.ioassert-offset
lib.rsassert-offset
version
sourcesrc
created_at2025-01-14 21:29:10.468751+00
updated_at2025-01-14 21:40:35.733827+00
descriptionDerive macro for asserting the memory offset of fields in a struct.
homepage
repositoryhttps://github.com/cohaereo/assert-offset
max_upload_size
id1516704
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
cohae (cohaereo)

documentation

README

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.

Commit count: 3

cargo fmt