![Latest Version] ![docs] ![MIT] [![Rust](https://img.shields.io/badge/rust-1.53%2B-blue.svg?maxAge=3600)](https://github.com/ralfbiedert/interoptopus) Rust

Interoptopus 🐙

Why export to only a single language when you can have them all? The polyglot bindings generator for your library.


Huh? - Imagine you are writing this cool API and want the world to have it. - The world, however, is running Unity, C, Python, ... all at the same time. - "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.

Code you write ...

```rust use interoptopus::{ffifunction, ffitype, inventory};

[ffi_type]

[repr(C)]

pub struct Vec2 { pub x: f32, pub y: f32, }

[ffi_function]

[no_mangle]

pub extern "C" fn my_function(input: Vec2) { println!("{}", input.x); }

inventory!(ffiinventory, [], [myfunction], []);

```

... Interoptopus generates

| 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.

Getting Started 🍼

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.

Features

Supported Rust Constructs

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.

Performance 🏁

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.

Current Status

FAQ

Contributing

PRs are welcome.