Tool for connecting programs or libraries written in Rust with other languages.
Foreign language api generator - flapigen. Former name rust_swig was changed to not confuse
withswig.
Currently implemented support forC++
andJava
,but you can write support
for any language of your choice. For an instruction how to integrate flapigen with your
project lookhere.
Suppose you have the following Rust code:
structFoo{
data:i32
}
implFoo{
fnnew(val:i32)->Foo{
Foo{data:val}
}
fnf(&self,a:i32,b:i32)->i32{
self.data+ a + b
}
fnset_field(&mutself,v:i32){
self.data= v;
}
}
fnf2(a:i32)->i32{
a*2
}
and you want to write in Java something like this:
Foofoo=newFoo(5);
intres=foo.f(1,2);
assertres==8;
or in C++ something like this:
Foofoo(5);
intres = foo.f(1,2);
assert(res ==8);
In order to implement it flapigen suggests the following functionality, in Rust project you write (in Rust language):
foreign_class!(classFoo{
self_typeFoo;
constructorFoo::new(_:i32)->Foo;
fnFoo::set_field(&mutself,_:i32);
fnFoo::f(&self,_:i32,_:i32)->i32;
fnf2(_:i32)->i32;
});
and that's all, as a result flapigen 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.
If you want the interface file (the file containingforeign_class!
and so on)
to be automatically generated for you, checkoutrifgen.