flapigen

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

  • Owner: Dushistov/flapigen-rs
  • Platform: Linux, Mac, Windows
  • License:: BSD 3-Clause "New" or "Revised" License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

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

Overview

Name With OwnerDushistov/flapigen-rs
Primary LanguageRust
Program languageRust (Language Count: 6)
PlatformLinux, Mac, Windows
License:BSD 3-Clause "New" or "Revised" License
Release Count14
Last Release Name0.5.0 (Posted on 2020-05-04 16:58:34)
First Release Name0.0.4 (Posted on 2017-08-02 04:43:44)
Created At2016-10-26 09:32:33
Pushed At2024-04-09 16:00:43
Last Commit At2024-04-09 18:00:53
Stargazers Count755
Watchers Count17
Fork Count59
Commits Count1.4k
Has Issues Enabled
Issues Count171
Issue Open Count57
Pull Requests Count274
Pull Requests Open Count3
Pull Requests Close Count9
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

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! ?

To the top