flapigen

用于将用 Rust 编写的程序或库与其他语言连接起来。「Tool for connecting programs or libraries written in Rust with other languages.」

  • 所有者: Dushistov/flapigen-rs
  • 平台: Linux, Mac, Windows
  • 許可證: BSD 3-Clause "New" or "Revised" License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

flapigen

用于连接用 Rust 编写的程序或库与其他语言的工具。外语 api 生成器 -- flapigen。以前的名字 rust_swig 是为了不与 swig 混淆而改的。目前实现了对 C++ 和 Java 的支持,但你可以为你选择的任何语言编写支持。关于如何将 flapigen 集成到你的项目中的指导,请看 这里

假设你有以下的 Rust 代码:

struct Foo {
    data: i32
}
impl Foo {
    fn new(val: i32) -> Foo {
        Foo{data: val}
    }
    fn f(&self, a: i32, b: i32) -> i32 {
        self.data + a + b
    }
    fn set_field(&mut self, v: i32) {
        self.data = v;
    }
}
fn f2(a: i32) -> i32 {
    a * 2
}

而你想在 Java 中写这样的东西:

Foo foo = new Foo(5);
int res = foo.f(1, 2);
assert res == 8;

或者在 C++ 中这样的内容:

Foo foo(5);
int res = foo.f(1, 2);
assert(res == 8);

为了实现它,flapigen 建议以下功能,在 Rust 项目中,你写(用 Rust 语言):

foreign_class!(class Foo {
    self_type Foo;
    constructor Foo::new(_: i32) -> Foo;
    fn Foo::set_field(&mut self, _: i32);
    fn Foo::f(&self, _: i32, _: i32) -> i32;
    fn f2(_: i32) -> i32;
});

就这样,flapigen 为 Rust 函数生成 JNI 包装器,并生成调用这些 JNI 函数的 Java 代码;如果是 C++,则生成 C 兼容包装器,并生成调用这些 C 函数的 C++ 代码。

用户指南

books 阅读flapigen的用户指南!books

主要指標

概覽
名稱與所有者Dushistov/flapigen-rs
主編程語言Rust
編程語言Rust (語言數: 6)
平台Linux, Mac, Windows
許可證BSD 3-Clause "New" or "Revised" License
所有者活动
創建於2016-10-26 09:32:33
推送於2025-04-20 09:01:34
最后一次提交2025-04-20 11:46:10
發布數18
最新版本名稱0.9.0 (發布於 2025-04-20 12:01:21)
第一版名稱0.0.4 (發布於 2017-08-02 04:43:44)
用户参与
星數788
關注者數16
派生數60
提交數1.4k
已啟用問題?
問題數179
打開的問題數62
拉請求數284
打開的拉請求數3
關閉的拉請求數9
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

rust-swig Build Status License Rust Documentation

Tool for connecting programs or libraries written in Rust with other languages.
Currently implemented support for C++ and Java, but you can write support
for any language of your choice. For an instruction how to integrate rust_swig with your
project look here.

Suppose you have the following Rust code:

struct Foo {
    data: i32
}

impl Foo {
    fn new(val: i32) -> Foo {
        Foo{data: val}
    }

    fn f(&self, a: i32, b: i32) -> i32 {
        self.data + a + b
    }

    fn set_field(&mut self, v: i32) {
        self.data = v;
    }
}

fn f2(a: i32) -> i32 {
    a * 2
}

and you want to write in Java something like this:

Foo foo = new Foo(5);
int res = foo.f(1, 2);
assert res == 8;

or in C++ something like this:

Foo foo(5);
int res = foo.f(1, 2);
assert(res == 8);

In order to implement it rust_swig suggests the following functionality,
in Rust project you write (in Rust language):

foreign_class!(class Foo {
    self_type Foo;
    constructor Foo::new(_: i32) -> Foo;
    fn Foo::set_field(&mut self, _: i32);
    fn Foo::f(&self, _: i32, _: i32) -> i32;
    fn f2(_: i32) -> i32;
});

and that's all, as a result rust_swig generates JNI wrappers for Rust functions
and Java code to call these JNI functions
or generates C compatible wrappers in case of C++ and
C++ code to call these C functions.

Users Guide

? Read the rust_swig users guide here! ?