macro_rules! offsetable_struct {
(
$( #[$attribute: meta] )*
$struct_vis: vis struct $name: ident {
$(
$field_vis: vis $field: ident: $ftype: ty
),*
} repr(C) as $offsets_name: ident
) => { ... };
}Expand description
Creates a repr(C) struct and a companion offsets struct which represents byte offsets of the fields.
offsetable_struct! {
#[derive(Debug)]
pub struct Name {
pub a: f32,
pub b: [f32; 4],
c: u8
} repr(C) as NameOffsets
}expands to:
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct NameOffsets {
pub a: usize,
pub b: usize,
c: usize
}
#[derive(Debug)]
#[repr(C)]
pub struct Name {
pub a: f32,
pub b: [f32; 4],
c: u8
}
impl Name {
#[allow(unused_variables)]
pub const fn offsets() -> NameOffsets {
let current_offset: usize = 0;
let a = vulkayes_core::util::align_up(current_offset, std::mem::align_of::<f32>());
let current_offset = a + std::mem::size_of::<f32>();
let b = vulkayes_core::util::align_up(current_offset, std::mem::align_of::<[f32; 4]>());
let current_offset = b + std::mem::size_of::<[f32; 4]>();
let c = vulkayes_core::util::align_up(current_offset, std::mem::align_of::<u8>());
let current_offset = c + std::mem::size_of::<u8>();
NameOffsets {
a,
b,
c
}
}
}