trait-ffi
A Rust procedural macro library for creating and implementing extern fn with Trait.
Features
This library provides procedural macros to define and implement cross-crate extern func, supporting:
- Define external traits (
#[def_extern_trait]) - Define a trait that can be implemented across crates - Generated implementation macros (
impl_trait!) - Auto-generated macros for simplified trait implementation - Multiple ABI support - Support for both C and Rust ABIs
Usage
Define external traits
Define an external trait in the interface crate:
use *;
Implement external traits
Implement the external trait in the implementation crate:
use ;
;
impl_trait!
Specify ABI
You can specify the ABI for external traits (default is "rust"):
Supported ABI types:
"rust"- Rust ABI (default)"c"- C ABI
How it works
This library uses Rust's procedural macro system to generate cross-crate external function calls:
- Definition stage: The
#[def_extern_trait]macro generates corresponding external function declarations and call wrappers for each function in the trait - Implementation stage: The
#[impl_extern_trait]macro generates#[no_mangle]external functions for the implemented functions - Linking stage: Through function name prefix conventions, ensures that the interface crate can correctly call functions in the implementation crate
Generated function names use the crate name as a prefix, e.g., __mycrate_function_name, to avoid symbol conflicts.
Key Features
- Type safety: Maintains complete Rust type system safety
- Zero runtime overhead: All processing is done at compile time
- Multiple ABI support: Supports both C and Rust ABIs
- Automatic symbol management: Automatically handles function name prefixes to avoid symbol conflicts
- Easy to use: Provides clean macro interfaces
Limitations
- Only supports function-type trait items
- Requires linking implementation crates at compile time
- Function parameters and return values must be FFI-safe types (when using C ABI)
License
This project is licensed under the MIT License.