Skip to content

Advanced Usage

Sometimes you need to expose types that don’t map directly to standard primitives.

Since you handle FFI boundaries, generic types are often limited. The Newtype Pattern is your best friend when you need to expose a complex internal type (like an Arc<Mutex<T>>).

pub struct Wrapper(pub(crate) Arc<InternalType>);
#[bridgerust::export]
impl Wrapper {
// Expose methods that delegate to the inner type
}

You can write code specific to one binding using standard Rust cfg attributes, or let the macro handle simple cases.

#[bridgerust::export]
pub fn platform_specific() {
#[cfg(feature = "python")]
println!("Running in Python");
#[cfg(feature = "nodejs")]
println!("Running in Node.js");
}

Your Cargo.toml should effectively forward features to the bridgerust crate.

[features]
default = []
python = ["bridgerust/python"]
nodejs = ["bridgerust/nodejs"]

This allows independent building:

  • cargo build --features python -> Optimized for Python extension
  • cargo build --features nodejs -> Optimized for Node.js addon