1 个稳定版本
4.1.1 | 2023年12月19日 |
---|
#179 在 FFI
52,263 每月下载量
用于 flutter_rust_bridge
210KB
2K SLoC
在主分支发布之前对 dart-sys 的分叉
Dart-sys
Rust 对 Dart ffi api 的绑定
先决条件 🔧
您需要在您的系统上提供以下工具
Unix/Linux 🐧
没有其他要求 :)
MacOS 🍎
没有其他要求 :)
Windows 🪟
在 Windows 平台上,动态库是 针对 可执行文件链接的,而不是像 Unix 平台那样 内联 在可执行文件中。
⚠️ 重要 ⚠️
这意味着(在 Windows 上)您将 需要 在您的系统路径上安装 Dart SDK,以便能够编译 Dart-sys。
安装 📦
在您的项目目录中运行以下 Cargo 命令
cargo add dart-sys
或者将以下行添加到您的 Cargo.toml 中
dart-sys = "4.1.0"
用法 💻
示例 📚
使用 dart-sys
的一个极其简单的示例可能如下所示
use dart_sys::{Dart_Handle, Dart_NewIntegerFromI64};
#[no_mangle]
/// Adds two integers together.
pub extern "C" fn dart_sys_example_extension_sum(
a: Dart_Handle,
b: Dart_Handle,
) -> Dart_Handle {
let a = unsafe { Dart_NewIntegerFromI64(a) };
let b = unsafe { Dart_NewIntegerFromI64(b) };
a + b
}
#[no_mangle]
/// Multiplies two integers together.
pub extern "C" fn dart_sys_example_extension_product(
a: Dart_Handle,
b: Dart_Handle,
) -> Dart_Handle {
let a = unsafe { Dart_NewIntegerFromI64(a) };
let b = unsafe { Dart_NewIntegerFromI64(b) };
a * b
}
import 'dart:ffi';
// open and link to the native library
final DynamicLibrary nativeLib = DynamicLibrary.open('libdart_sys_example_extension.so');
// lookup the sum function in the native library
final int Function(int, int) sum = nativeLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>('dart_sys_example_extension_sum')
.asFunction();
// lookup the product function in the native library
final int Function(int, int) product = nativeLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>('dart_sys_example_extension_product')
.asFunction();
void main() {
print(sum(1, 2)); // 3
print(product(1, 2)); // 2
}
虽然这个示例是可能的,但您不太可能为此目的使用 Dart-sys。请参阅 示例 目录,以获取更多关于如何使用 Dart-sys 的深入示例。所有示例都使用 GitHub Actions 进行测试,并进行了详细的文档记录。
构建工具 🛠️
- Rust - 一种运行速度极快、防止段错误并保证线程安全的系统编程语言。
- Dart - 一种适用于任何平台上的快速应用的客户端优化语言。
- Dart Native Extensions - 一种在 C/C++ 中编写原生代码并从 Dart 中调用它的机制。
- bindgen - 一个用于生成 C 和 C++ API 绑定的 Rust 库。
贡献 ✏️
请阅读 CONTRIBUTING.md 了解我们的行为准则和提交拉取请求的流程。如有任何疑问,请提交一个 issue,或直接联系管理员 [email protected]。
版本控制 🪧
我们使用 SemVer 进行版本控制。有关可用的版本,请参阅本仓库的 标签。
许可 📜
Dart-sys 是开源的,并按照以下一个或两个许可协议的条款和条件发布
鸣谢 🙏
无运行时依赖
~180KB