2022-10-31 22:38:40 -05:00
|
|
|
// Issue #80127: Passing structs via FFI should work with explicit alignment.
|
|
|
|
|
2023-05-20 00:24:33 -05:00
|
|
|
use std::ffi::{CStr, c_char};
|
2022-10-31 22:38:40 -05:00
|
|
|
use std::ptr::null_mut;
|
|
|
|
|
2023-06-11 12:41:46 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct BoolAndU32 {
|
|
|
|
pub a: bool,
|
|
|
|
pub b: u32,
|
|
|
|
}
|
|
|
|
|
2023-05-20 00:24:33 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2022-10-31 22:38:40 -05:00
|
|
|
#[repr(C)]
|
|
|
|
#[repr(align(16))]
|
|
|
|
pub struct TwoU64s {
|
|
|
|
pub a: u64,
|
|
|
|
pub b: u64,
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:41:46 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2022-10-31 22:38:40 -05:00
|
|
|
#[repr(C)]
|
2023-06-11 12:41:46 -05:00
|
|
|
pub struct WrappedU64s {
|
|
|
|
pub a: TwoU64s
|
|
|
|
}
|
|
|
|
|
2023-05-20 00:24:33 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2023-06-11 12:41:46 -05:00
|
|
|
#[repr(C)]
|
|
|
|
// Even though requesting align 1 can never change the alignment, it still affects the ABI
|
|
|
|
// on some platforms like i686-windows.
|
|
|
|
#[repr(align(1))]
|
|
|
|
pub struct LowerAlign {
|
|
|
|
pub a: u64,
|
|
|
|
pub b: u64,
|
2022-10-31 22:38:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[link(name = "test", kind = "static")]
|
|
|
|
extern "C" {
|
|
|
|
fn many_args(
|
|
|
|
a: *mut (),
|
|
|
|
b: *mut (),
|
2023-05-17 07:31:35 -05:00
|
|
|
c: *const c_char,
|
2022-10-31 22:38:40 -05:00
|
|
|
d: u64,
|
|
|
|
e: bool,
|
|
|
|
f: BoolAndU32,
|
|
|
|
g: *mut (),
|
|
|
|
h: TwoU64s,
|
|
|
|
i: *mut (),
|
2023-06-11 12:41:46 -05:00
|
|
|
j: WrappedU64s,
|
2022-10-31 22:38:40 -05:00
|
|
|
k: *mut (),
|
2023-06-11 12:41:46 -05:00
|
|
|
l: LowerAlign,
|
2023-05-17 07:31:35 -05:00
|
|
|
m: *const c_char,
|
2022-10-31 22:38:40 -05:00
|
|
|
) -> i32;
|
|
|
|
}
|
|
|
|
|
2023-05-20 00:24:33 -05:00
|
|
|
const STRING: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"Hello world\0") };
|
|
|
|
|
2022-10-31 22:38:40 -05:00
|
|
|
fn main() {
|
2023-06-11 12:41:46 -05:00
|
|
|
let bool_and_u32 = BoolAndU32 { a: true, b: 1337 };
|
2022-10-31 22:38:40 -05:00
|
|
|
let two_u64s = TwoU64s { a: 1, b: 2 };
|
2023-06-11 12:41:46 -05:00
|
|
|
let wrapped = WrappedU64s { a: TwoU64s { a: 3, b: 4 } };
|
|
|
|
let lower = LowerAlign { a: 5, b: 6 };
|
2023-05-20 00:24:33 -05:00
|
|
|
let string = STRING;
|
2022-10-31 22:38:40 -05:00
|
|
|
unsafe {
|
|
|
|
many_args(
|
|
|
|
null_mut(),
|
|
|
|
null_mut(),
|
|
|
|
null_mut(),
|
2023-06-11 12:41:46 -05:00
|
|
|
42,
|
2022-10-31 22:38:40 -05:00
|
|
|
true,
|
|
|
|
bool_and_u32,
|
|
|
|
null_mut(),
|
|
|
|
two_u64s,
|
|
|
|
null_mut(),
|
2023-06-11 12:41:46 -05:00
|
|
|
wrapped,
|
2022-10-31 22:38:40 -05:00
|
|
|
null_mut(),
|
2023-06-11 12:41:46 -05:00
|
|
|
lower,
|
2022-10-31 22:38:40 -05:00
|
|
|
string.as_ptr(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|