GithubHelp home page GithubHelp logo

seaeagle1 / interoptopus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ralfbiedert/interoptopus

0.0 0.0 0.0 1.07 MB

The polyglot bindings generator for your library (C#, C, Python, โ€ฆ) ๐Ÿ™

Shell 0.22% Python 7.44% C 1.40% C# 39.36% Rust 51.58%

interoptopus's Introduction

Latest Version docs MIT Rust Rust

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:

  • Design a single .dll / .so in Rust, consume it from any language.
  • Use patterns (e.g., classes, strings) in languages that have them.
  • Always be fully C compatible.
  • Painless workflow, no external tools required.

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]
#[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);
}

// 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))
        .inventory()
}

... Interoptopus generates

Language Crate Sample Output1
C# interoptopus_backend_csharp Interop.cs
C interoptopus_backend_c my_header.h
Python interoptopus_backend_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.

Getting Started ๐Ÿผ

If you want to ...

Supported Rust Constructs

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

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: ๐Ÿ”ฅ

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

Feature Flags

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.

Changelog

  • v0.14 - Better inventory UX.
  • v0.13 - Python backend uses ctypes now.
  • v0.12 - Better compat using #[ffi_service_method].
  • v0.11 - C# switch ctors to static methods.
  • v0.10 - C# flavors DotNet and Unity (incl. Burst).
  • v0.9 - 150x faster C# slices, Python type hints.
  • v0.8 - Moved testing functions to respective backends.
  • v0.7 - Make patterns proc macros for better FFI docs.
  • v0.6 - Renamed and clarified many patterns.
  • v0.5 - More ergonomic slice usage in Rust and FFI.
  • v0.4 - Enable logging support in auto-generated FFI calls.
  • v0.3 - Better compatibility with generics.
  • v0.2 - Introduced "patterns"; working interop for C#.
  • v0.1 - First version.

Also see our upgrade instructions.

FAQ

Contributing

PRs are welcome.

  • Submit small bug fixes directly. Major changes should be issues first.
  • Anything that makes previously working bindings change behavior or stop compiling is a major change;
  • This doesn't mean we're opposed to breaking stuff just that we'd like to talk about it before it happens.
  • New features or patterns must be materialized in the reference project and accompanied by an interop test (i.e., a backend test running C# / Python against a DLL invoking that code) in at least one included backend.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.