Home

Awesome

Latest Version docs MIT Rust Rust

sample_image

Interoptopus 🐙

The polyglot bindings generator for your library.

Write a robust library in Rust, easily access it from your second-favorite language:

We strive to make our generated bindings zero cost. They should be as idiomatic as you could have reasonably written them yourself, but never magic or hiding the interface you actually wanted to expose.

Code you write ...

use interoptopus::{ffi_function, ffi_type, Inventory, InventoryBuilder, function};

#[ffi_type]
pub struct Vec2 {
    pub x: f32,
    pub y: f32,
}

#[ffi_function]
pub fn my_function(input: Vec2) {
    println!("{}", input.x);
}

// Define our FFI interface as `ffi_inventory` containing
// a single function `my_function`. Types are inferred.
pub fn ffi_inventory() -> Inventory {
    InventoryBuilder::new()
        .register(function!(my_function))
        .validate()
        .inventory()
}

... Interoptopus generates

LanguageCrateSample Output<sup>1</sup>
C#interoptopus_backend_csharpInterop.cs
Cinteroptopus_backend_cmy_header.h
Pythoninteroptopus_backend_cpythonreference.py
OtherWrite your own backend<sup>2</sup>-

<sup>1</sup> For the reference project. <br/> <sup>2</sup> Add support for a new language in just a few hours. No pull request needed. Pinkie promise.

Getting Started 🍼

If you want to ...

Supported Rust Constructs

See the reference project for an overview:

Performance 🏁

Generated low-level bindings are zero cost w.r.t. hand-crafted bindings for that language.

That said, even hand-crafted bindings encounter some target-specific overhead at the FFI boundary (e.g., marshalling or pinning in managed languages). For C# that cost is often nanoseconds, for Python CFFI it can be microseconds.

While ultimately there is nothing you can do about a language's FFI performance, being aware of call costs can help you design better APIs.

Detailed call cost tables can be found here: <sup>🔥</sup>

For a quick overview, this table lists the most common call types in ns / call:

ConstructC#Python
primitive_void()7272
primitive_u32(0)8392
many_args_5(0, 0, 0, 0, 0)10786
callback(x => x, 0)431168
<br/>

Feature Flags

Gated behind feature flags, these enable:

Changelog

Also see our upgrade instructions.

FAQ

Contributing

PRs are welcome.