Skip to content

The Export Macro

The core of BridgeRust is the #[export] attribute macro. Placing this attribute on your Rust items instructs the framework to generate the necessary bindings for Python and Node.js.

Mark public functions with #[export] to make them callable from Python and Node.js.

#[bridgerust::export]
pub fn add(a: i32, b: i32) -> i32 {
a + b
}

Export structs to create classes in the target languages.

#[bridgerust::export]
pub struct Point {
pub x: f64,
pub y: f64,
}

Export enums to represent shared state or options.

#[bridgerust::export]
pub enum Status {
Active,
Inactive,
Pending,
}

The macro automatically detects which features (python, nodejs) are enabled in your Cargo.toml and generates code accordingly.

  • Python: Generates #[pyfunction], #[pyclass], and #[pymethods] using pyo3.
  • Node.js: Generates #[napi] attributes using napi-rs.
  • Items must be pub.
  • Generic types are not directly supported (due to FFI limitations). Use concrete types or wrapper structs.
  • Async functions are supported but require specific runtime configuration (Tokio is recommended).