blog
blog copied to clipboard
Rust FFI: XXX Call Rust & Rust Call XXX
📖 Abstract:
- ✅ 基于
C FFI
, 把rust 库
binding 给其他语言
调用,以及反向操作。
💯 Related:
- ✅ #432
- ✅ #433
- ✅ #362
- ✅ #384
- ✅ #242
Rust + FFI 绑定 core lib 给其他语言调用:
- Rust 是非常非常适合做 core lib 层的跨平台方案.
- Flutter 适合 UI 层(交互层) 的跨平台方案.
- Rust + Flutter 是非常完美的架构组合方案, 兼顾效率和性能.
- FFI
Rust + FFI 方案:
- https://learnku.com/docs/nomicon/2018/110-ffi/4756
搜索包:
- https://crates.io/keywords/ffi
1. 💎💎💎 主调方(as Caller): Rust call C/C++
- 调用方: 调用其他语言, call others.
tools:
- https://github.com/rust-lang/rust-bindgen
- ⭐⭐⭐⭐⭐
- https://rust-lang.github.io/rust-bindgen/command-line-usage.html
- https://github.com/adetaylor/rust-bindgen
- ⭐⭐⭐⭐⭐
- 有用的 fork
- https://github.com/google/autocxx
- ⭐⭐⭐⭐⭐
- Google 项目
- https://github.com/rust-qt/ritual
- ⭐⭐⭐⭐⭐
示例参考项目:
- https://github.com/rust-bitcoin/rust-secp256k1/blob/master/secp256k1-sys/build.rs#L31
- ⭐⭐⭐⭐⭐
- https://github.com/fitzgen/bindgen-tutorial-bzip2-sys
- 官方示例
- https://github.com/lesterli/rust-practice/tree/master/ffi/secp256k1-sys
👿👿👿👿 Mac M1 兼容性问题:
- rust ffi 在 MacOS M1 下, 有一些兼容性问题.
- https://github.com/rust-lang/rust/issues/73908
- https://github.com/rust-bitcoin/rust-secp256k1/issues/283
教程:
- https://doc.rust-lang.org/nomicon/ffi.html
- https://wiki.jikexueyuan.com/project/rust-primer/ffi/calling-ffi-function.html
- https://stevenbai.top/rustbook/book/interoperability/c-with-rust.html
- https://rustcc.cn/article?id=9219a366-84d3-49c8-b957-dfbade1257fc
2. 💎💎💎 被调方(as Callee): C/C++ call Rust
- 被调方: 被其他语言调用, as core lib for others.
tools:
- https://github.com/eqrion/cbindgen
- ⭐⭐⭐⭐⭐
- https://github.com/getditto/safer_ffi
- ⭐⭐⭐⭐⭐
教程:
- https://rustcc.cn/article?id=9219a366-84d3-49c8-b957-dfbade1257fc
- https://stevenbai.top/rustbook/book/interoperability/rust-with-c.html
- https://github.com/bitcoin-core/secp256k1
- binding: https://github.com/rust-bitcoin/rust-secp256k1
- https://doc.rust-lang.org/nomicon/ffi.html#calling-rust-code-from-c
- https://wiki.jikexueyuan.com/project/rust-primer/ffi/compiling-rust-to-lib.html
- 使用Rust加速你的Python代码
3. 💎💎💎 双向桥: rust vs c++
- https://github.com/dtolnay/cxx
- ⭐⭐⭐⭐⭐
- rust + c++ 双向桥
- https://cxx.rs/index.html
4. 其他:
- https://github.com/shekohex/flutterust
- https://github.com/TimNN/cargo-lipo
- https://github.com/rodrimati1992/abi_stable_crates
Rust + FFI + Flutter:
with Flutter:
- https://github.com/brickpop/flutter-rust-ffi
- 使用方式
- https://github.com/fzyzcjy/flutter_rust_bridge
- ⭐⭐⭐⭐⭐
- 已经支持除 web, 所有平台. web 支持在开发ing.
- 使用示例: https://github.com/fzyzcjy/flutter_rust_bridge/blob/master/frb_example/with_flutter/rust/src/lib.rs
- web 支持进展: https://github.com/fzyzcjy/flutter_rust_bridge/issues/315
Rust + FFI + Flutter 典型案例:
- https://github.com/AppFlowy-IO/AppFlowy
一个漫画阅读器:
- https://github.com/Hentioe/mikack
- https://github.com/Hentioe/mikack-ffi
- https://github.com/Hentioe/mikack-dart
- https://github.com/Hentioe/mikack-mobile
Rust + FFI + Java(JNI):
- https://github.com/jni-rs/jni-rs
Rust FFI (被调方):
工具链: cbindgen
- https://github.com/eqrion/cbindgen
参考:
- Rust FFI 编程 - cbindgen 工具介绍
- Rust FFI 编程 - cbindgen 使用示例
- https://guonaihong.github.io/post/call-c-dynamic-library-in-rust-bingen-2/
- 案例: https://github.com/lesterli/rust-practice/blob/master/ffi/example_03/cbindgen.toml
- ⭐⭐⭐⭐⭐
# 安装:
cargo install --force cbindgen
# 生成:
cbindgen --config cbindgen.toml --crate my_rust_library --output my_header.h
Rust + FFI + Mac M1 问题:
- https://xuanwo.io/2021/10-rust-cross-aarch64/
- 一个可能的解决思路.
- 待尝试.