24 个稳定版本 (3 个主要版本)
4.1.5 | 2024 年 3 月 9 日 |
---|---|
4.0.2 | 2023 年 2 月 2 日 |
3.1.12 | 2023 年 1 月 26 日 |
2.0.1 | 2020 年 3 月 25 日 |
0.1.2 | 2019 年 9 月 30 日 |
#52 in FFI
31,427 每月下载量
用于 4 crates
210KB
2K SLoC
在上游发布之前 fork 的 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.5"
使用 💻
示例 📚
使用 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 本地扩展 - 一种在 C/C++ 中编写本地代码并在 Dart 中调用它的机制。
- bindgen - 一个用于生成C和C++ API绑定的Rust库。
贡献 ✏️
请阅读CONTRIBUTING.md,了解我们的行为准则和提交pull请求的过程。如果您有任何问题,请打开一个issue,或直接联系管理员[email protected]。
版本控制 🪧
我们使用SemVer进行版本控制。有关可用版本,请参阅此存储库的标签。
许可证 📜
Dart-sys是开源的,并且根据以下一个或两个许可证的条款和条件发布:
鸣谢 🙏
无运行时依赖
~185KB