Attribute Macro prebindgen

Source
#[prebindgen]
Expand description

Attribute macro that exports FFI definitions for use in language-specific binding crates.

All types and functions marked with this attribute can be made available in dependent crates as Rust source code for both binding generator processing (cbindgen, csbindgen, etc.) and for including into projects to make the compiler generate #[no_mangle] FFI exports for cdylib/staticlib targets.

§Usage

// Use with explicit group name
#[prebindgen("group_name")]
#[repr(C)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

// Use with default group name "default"
#[prebindgen]
pub fn calculate_distance(p1: &Point, p2: &Point) -> f64 {
    ((p2.x - p1.x).powi(2) + (p2.y - p1.y).powi(2)).sqrt()
}

// Generate all the output for further processing but do not pass code to the compiler
#[prebindgen(skip = true)]
pub fn internal_function() -> i32 {
    42
}

// Combine group name with skip
#[prebindgen("functions", skip = true)]
pub fn another_function() -> i32 {
    42
}

§Requirements

  • Must call prebindgen::init_prebindgen_out_dir() in your crate’s build.rs
  • Optionally takes a string literal group name for organization (defaults to “default”)
  • Optionally takes skip = true to remove the item from compilation output