![Latest Version]
![docs]
![MIT]
[](https://github.com/ralfbiedert/interoptopus)
The polyglot bindings generator for your library (C#, C, Python, ...)
Huh? - Imagine you are writing this cool API and want everyone to use it. - Everyone else, however, is running Unity, C, Python, ... - "Not a problem", you say, "I'll just use Interoptopus".
And you'll live happily* ever after.
*Actual results may depend on other life choices.
```rust use interoptopus::{ffifunction, ffitype, inventory};
pub struct Vec2 { pub x: f32, pub y: f32, }
pub extern "C" fn my_function(input: Vec2) { println!("{}", input.x); }
inventory!(ffiinventory, [], [myfunction], [], []);
```
| Language | Crate | Sample Output | | --- | --- | --- | | C# (incl. Unity) | interoptopusbackendcsharp | Interop.cs | | C | interoptopusbackendc | myheader.h | | Python CFFI | interoptopusbackendcpythoncffi | reference.py | | Your language | Write your own backend1 | - |
1 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 example projects, - need to support a new language or rewrite a backend, copy and adapt the C backend.
cargo build
+ cargo test
can produce and test (if lang installed) generated bindingsGated behind feature flags, these enable:
derive
- Proc macros such as ffi_constant
, ffi_function
, ffi_type
.testing
- Functions to test generated Python, C#, C from Unit tests.serde
- Serde attributes on internal types.log
- Invoke log
on FFI errors (you still need actual logger).See the reference project; it lists all supported constructs including:
- functions (extern "C"
functions and delegates)
- types (primitives, composite, enums (numeric only), opaques, references, pointers, ...)
- constants (primitive constants; results of const evaluation)
- patterns (ASCII pointers, options, slices, classes, ...)
As a rule of thumb we recommend to be slightly conservative with your signatures and always "think C", since other languages don't track lifetimes
well and it's is easy to accidentally pass an outlived pointer or doubly alias a &mut X
on reentrant functions.
Generated low-level bindings should be "zero cost" w.r.t. hand-crafted bindings for that language. However, even hand-crafted bindings have an inherent, language-specific cost. For C# that cost can be almost 0, for Python CFFI it can be high. Patterns and convenience helpers might add additional overhead.
If you need API design guidance the following (wip) C# call-cost table🔥 can help.
PRs are welcome.