How to set alignment for individual field in struct

is there any option to set alignment for individual field in struct..

if not how to set

If you need a value to be over-aligned, use a wrapper type for the field type.

#[repr(C, align(4))]
struct AlignedU8(u8);

struct Foo {
    a: AlignedU8,
    b: AlignedU8,
}

If you want to precisely control the layout of a struct, use #[repr(C)] types and add explicit placeholder values (e.g. [u8; 10]) to insert additional padding where needed. Test the results using offset_of().

If that doesn't help, tell us more about your use case.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.