4 ms·
It's mostly straightforward, you just define with #[no_mangle] + extern fn ... The Rust docs cover it in small detail[1]. FWIW you can do the same with JNI and
by vvanders 9y ago
It's mostly straightforward, you just define with #[no_mangle] + extern fn ...
The Rust docs cover it in small detail[1]. FWIW you can do the same with JNI and have direct Java->Rust bindings. If you want to get really clever you can even parse the Rust AST(using syntex) to auto-generate java/headers(there may be libs to do this now, I haven't looked in a while).
What's nice about the C FFI is you can pretty much embed Rust anywhere, I've used it in C/C++, Java, C# and Python pretty easily.
[1] https://doc.rust-lang.org/book/second-edition/ch19-01-unsafe-rust.html#calling-rust-functions-from-other-languages https://doc.rust-lang.org/book/second-edition/ch19-01-unsafe...
- Manishearth 9y agoThere's a tool called cbindgen (https://github.com/eqrion/cbindgen https://github.com/eqrion/cbindgen) that generates header files from rust code . It's a bit experimental. The reverse tool, bindgen (https://github.com/rust-lang-nursery/rust-bindgen/ https://github.com/rust-lang-nursery/rust-bindgen/) is more mature -- generates rust bindings from header files.
- vvanders 9y agoOh yeah, I should have mentioned bindgen, it's fantastic!