cbindgen/tests/rust/layout_aligned_opaque.rs
Aleksa Sarai f23d4ee6e8 tests: add tests for #[repr(packed)] and #[repr(align(...))]
The tests are very straightforward, and effecitvely are just ensuring
that the formatting works correctly and is included in all of the
important cases.

It's also very important to ensure we do not generate laid-out structs
for layouts which we cannot reasonably represent in C (such as in cases
where we weren't told what annotation to use for packed and
specifically-aligned structures). Thus, add some tests to verify that
this is the case.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2019-12-15 11:52:23 +01:00

68 lines
1.3 KiB
Rust

#[repr(packed, C)]
pub struct PackedStruct {
pub arg1: usize,
pub arg2: *mut u8,
}
#[repr(packed, C)]
pub union PackedUnion {
pub variant1: usize,
pub variant2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(1), C)]
pub union OpaqueAlign1Union {
pub variant1: usize,
pub variant2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(4), C)]
pub union OpaqueAlign4Union {
pub variant1: usize,
pub variant2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(16), C)]
pub union OpaqueAlign16Union {
pub variant1: usize,
pub variant2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(1), C)]
pub struct OpaqueAlign1Struct {
pub arg1: usize,
pub arg2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(2), C)]
pub struct OpaqueAlign2Struct {
pub arg1: usize,
pub arg2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(4), C)]
pub struct OpaqueAlign4Struct {
pub arg1: usize,
pub arg2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(8), C)]
pub struct OpaqueAlign8Struct {
pub arg1: usize,
pub arg2: *mut u8,
}
// Opaque because aligned_n is not defined.
#[repr(align(32), C)]
pub struct OpaqueAlign32Struct {
pub arg1: usize,
pub arg2: *mut u8,
}