impl_extern_trait

Attribute Macro impl_extern_trait 

Source
#[impl_extern_trait]
Expand description

Helper macro to implement the extern trait for a type. Implements an extern trait for a type and generates corresponding C function exports.

This macro takes a trait implementation and generates extern “C” functions that can be called from other languages. Each method in the trait implementation gets a corresponding extern function with a mangled name based on the crate name and version.

§Arguments

  • name: The name of the crate that defines the extern trait
  • abi: The ABI to use for the extern functions (“c” or “rust”), defaults to “c”

§Example

struct Calculator;

#[impl_extern_trait(name = "calculator_crate", abi = "c")]
impl MyTrait for Calculator {
    fn add(&self, a: i32, b: i32) -> i32 {
        a + b
    }
}

This will generate extern “C” functions that can be called from other languages.