![Latest Version]
![docs]
![MIT]
[](https://github.com/ralfbiedert/interoptopus)
The polyglot bindings generator for your library.
Interoptopus allows you to deliver high-quality system libraries to your users, and enables your users to easily consume those libraries from the language of their choice:
.dll
/ .so
in Rust, consume it from any 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.
```rust use interoptopus::{ffifunction, ffitype, Inventory, InventoryBuilder, function};
pub struct Vec2 { pub x: f32, pub y: f32, }
pub extern "C" 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 ffiinventory() -> Inventory {
InventoryBuilder::new()
.register(function!(myfunction))
.inventory()
}
```
| Language | Crate | Sample Output1 | | --- | --- | --- | | C# | interoptopusbackendcsharp | Interop.cs | | C | interoptopusbackendc | myheader.h | | Python | interoptopusbackend_cpython | reference.py | | Other | Write your own backend2 | - |
1 For the reference project.
2 Create your own backend in just a few hours. No pull request needed. Pinkie promise.
If you want to ... - create a new API see the hello world, - understand what's possible, see the reference project, - support a new language, copy the C backend.
See the reference project for an overview:
- functions (extern "C"
functions and delegates)
- types (composites, enums, opaques, references, ...)
- constants (primitive constants; results of const evaluation)
- patterns (ASCII pointers, options, slices, classes, ...)
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: 🔥
For a quick overview, this table lists the most common call types in ns / call:
| Construct | C# | Python |
| --- | --- | --- |
| primitive_void()
| 7 | 272 |
| primitive_u32(0)
| 8 | 392 |
| many_args_5(0, 0, 0, 0, 0)
| 10 | 786 |
| callback(x => x, 0)
| 43 | 1168 |
Gated behind feature flags, these enable:
derive
- Proc macros such as ffi_type
, ...serde
- Serde attributes on internal types.log
- Invoke log on FFI errors.ctypes
now.#[ffi_service_method]
.DotNet
and Unity
(incl. Burst).Also see our upgrade instructions.
PRs are welcome.