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-19 09:51:00
最后一次提交2025-03-17 11:04:07
发布数17
最新版本名称0.8.0 (发布于 2024-11-11 19:05:27)
第一版名称0.0.4 (发布于 2017-08-02 04:43:44)
用户参与
星数789
关注者数16
派生数60
提交数1.4k
已启用问题?
问题数179
打开的问题数62
拉请求数280
打开的拉请求数4
关闭的拉请求数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! ?