| Crates.io | ffi_struct |
| lib.rs | ffi_struct |
| version | 0.3.1 |
| created_at | 2025-10-28 04:23:41.903322+00 |
| updated_at | 2025-10-28 06:10:34.730528+00 |
| description | The proc-macro crate for the Rust programming language to create structs with FFI compatibility. Also allows the members of the FFI structs to be iterable for better reflection than the crate `struct_iterable`. |
| homepage | |
| repository | https://github.com/0xAA55/ffi_struct |
| max_upload_size | |
| id | 1904131 |
| size | 34,905 |
An attribute macro designed to create Rust structs used for FFI directly with adjusted paddings inserted, and reflection support for the struct to be able to iterate through its member info, such as member name, type ID, size, and offset.
Chinglish | 简体中文
The code below shows the typical usage of the crate:
use ffi_struct::*;
fn test() {
type Vec3 = (f32, f32, f32);
type TVec3<FT> = (FT, FT, FT);
#[ffi_struct]
#[derive(Default, Debug)]
#[size_of_type(Vec3 = 12, Vec4 = 16)]
#[align_of_type(Vec3 = 12, Vec4 = 16)]
struct TestStructRust<FT>
where
FT: Default + Clone + Copy + Sized + Any {
field1: bool,
#[align = 4]
field2: u32,
field3: Vec3,
#[size = 24]#[align = 16]
field4: TVec3<FT>,
#[size = 8]
field5: FT,
}
let ffi = TestStruct::<f64>::default();
println!("======== members ========");
for (name, member) in ffi.iter_members() {
println!("{name}: {member:?}");
}
println!("======== all members ========");
for (name, member) in ffi.iter_all_members() {
println!("{name}: {member:?}");
}
}
After the #[ffi_struct] has been applied to TestStructRust, a NEW struct is created and named TestStruct, which had the Rust suffix removed.
The struct TestStruct derives traits that are originally derived to TestStructRust, e. g. Default and Debug.
After specifying the member's alignments, the paddings were added to the new struct TestStruct.
struct_iterable