| Crates.io | trait-ffi |
| lib.rs | trait-ffi |
| version | 0.2.11 |
| created_at | 2025-07-16 04:43:48.659096+00 |
| updated_at | 2025-09-25 05:18:20.34234+00 |
| description | A Rust procedural macro library for creating and implementing extern fn with Trait. |
| homepage | |
| repository | https://github.com/ZR233/trait-ffi |
| max_upload_size | |
| id | 1754919 |
| size | 20,358 |
A Rust procedural macro library for creating and implementing extern fn with Trait.
This library provides procedural macros to define and implement cross-crate extern func, supporting:
#[def_extern_trait]) - Define a trait that can be implemented across cratesimpl_trait!) - Auto-generated macros for simplified trait implementationDefine an external trait in the interface crate:
use trait_ffi::*;
#[def_extern_trait]
pub trait DemeIf {
fn say_hello(a: usize) -> i32;
}
pub fn if_say_hello(a: usize) -> i32 {
println!("Hello from DemeIf with value: {}", a);
demeif::say_hello(a)
}
Implement the external trait in the implementation crate:
use interface::{DemeIf, impl_trait};
pub struct MyImpl;
impl_trait! {
impl DemeIf for MyImpl {
fn say_hello(a: usize) -> i32 {
println!("Hello from MyImpl with value: {}", a);
(a * 2) as i32
}
}
}
You can specify the ABI for external traits (default is "rust"):
#[def_extern_trait(abi = "c")]
pub trait MyTrait {
fn my_function() -> i32;
}
Supported ABI types:
"rust" - Rust ABI (default)"c" - C ABIThis library uses Rust's procedural macro system to generate cross-crate external function calls:
#[def_extern_trait] macro generates corresponding external function declarations and call wrappers for each function in the trait#[impl_extern_trait] macro generates #[no_mangle] external functions for the implemented functionsGenerated function names use the crate name and version as a prefix, e.g., __mycrate_0_2_function_name, to avoid symbol conflicts and ensure version compatibility.
This library implements automatic version control to ensure compatibility across different versions:
Cargo.toml0.x.y: Uses 0_x as the version prefix (e.g., 0_2 for version 0.2.0)x.y.z where x >= 1: Uses major version x as the prefix (e.g., 1 for version 1.2.3)Example symbol naming:
my-interface, Version: 0.2.0, Function: say_hello__my_interface_0_2_say_helloThis project is licensed under the MIT License.