#alignment #packing #proc-macro #padding #layout #struct #graphics

alignment-exporter

Proc macro providing alignment info for a user-defined struct

1 unstable release

0.1.3 Aug 14, 2024
0.1.2 Aug 14, 2024
0.1.1 Aug 14, 2024
0.1.0 Aug 14, 2024

#832 in Data structures

MIT license

4KB

alignment-exporter

This crate provides alignment info for a user-defined struct, though this crate has increased in scope over time to include other information, such as the type name of struct fields.

Using this crate requires an MSRV of 1.80.

use alignment_exporter::{Alignment, AlignmentExporter, export_alignment};

// `export_alignment` already annotates #[repr(C)] to the struct, so adding that yourself is not required. However, it is always better to include it in your code for the sake of explicitness.
#[export_alignment]
struct Example {
    a: u8,
    b: u32,
    c: u16
}

fn main() {
    let alignment = Example::get_alignment();
    assert_eq!(alignment, vec![
        Alignment { size: 1, offset: 0, ty_name: "u8" },
        Alignment { size: 4, offset: 1, ty_name: "u32" },
        Alignment { size: 2, offset: 8, ty_name: "u16" },
    ]);
}

As for why alignment-exporter-derive contains the word "derive" even though it is not a derive macro, I forgot to change the name before publishing it. Oh well 🤷.


lib.rs:

This crate provides alignment info for a user-defined struct, though this crate has increased in scope over time to include other information, such as the type name of struct fields.

use alignment_exporter::{Alignment, AlignmentExporter, export_alignment};

// `export_alignment` already annotates #[repr(C)] to the struct, so adding that yourself is not required. However, it is always better to include it in your code for the sake of explicitness.
#[export_alignment]
struct Example {
    a: u8,
    b: u32,
    c: u16
}

fn main() {
    let alignment = Example::get_alignment();
    assert_eq!(alignment, vec![
        Alignment { size: 1, offset: 0, ty_name: "u8" },
        Alignment { size: 4, offset: 1, ty_name: "u32" },
        Alignment { size: 2, offset: 8, ty_name: "u16" },
    ]);
}

Dependencies

~205–640KB
~15K SLoC