# ffi_struct **Repository Path**: a5k3rn3l/ffi_struct ## Basic Information - **Project Name**: ffi_struct - **Description**: Rust 编程语言的 proc-macro 包,用于创建兼容 FFI 的结构体。此外,它还允许 FFI 结构体的成员可迭代,从而实现比 `struct_iterable` 包更好的反射性​能力。 - **Primary Language**: Unknown - **License**: LGPL-2.1 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-27 - **Last Updated**: 2025-10-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FFI Struct 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. ## Language|语言 Chinglish | [简体中文](Readme-CN.md) ## Example The code below shows the typical usage of the crate: ```Rust use ffi_struct::*; fn test() { type Vec3 = (f32, f32, f32); type TVec3 = (FT, FT, FT); #[ffi_struct] #[derive(Default, Debug)] #[size_of_type(Vec3 = 12, Vec4 = 16)] #[align_of_type(Vec3 = 12, Vec4 = 16)] struct TestStructRust where FT: Default + Clone + Copy + Sized + Any { field1: bool, #[align = 4] field2: u32, field3: Vec3, #[size = 24]#[align = 16] field4: TVec3, #[size = 8] field5: FT, } let ffi = TestStruct::::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`. ## Improvements compared to another crate `struct_iterable` * Generic type parameters were allowed in the structure. * Struct member iteration doesn't have to access the struct members, so the tightly packed struct is allowed. * When specifying alignments, the paddings were added to the new struct.