Rust bindings to the Dart native extensions api
You will need the following tools available on your system:
Run the following Cargo command in your project directory:
bash
cargo add dart-sys
Or add the following line to your Cargo.toml:
toml
dart-sys = "3.1.11"
Source code releases for each SemVer tag are also available on the GitHub releases page.
An extremely straightforward example of using dart-sys
would be like such:
```rust use dartsys::{DartHandle, Dart_NewIntegerFromI64};
/// Adds two integers together. pub extern "C" fn dartsysexampleextensionsum( a: DartHandle, b: DartHandle, ) -> DartHandle { let a = unsafe { DartNewIntegerFromI64(a) }; let b = unsafe { Dart_NewIntegerFromI64(b) }; a + b }
/// Multiplies two integers together. pub extern "C" fn dartsysexampleextensionproduct( a: DartHandle, b: DartHandle, ) -> DartHandle { let a = unsafe { DartNewIntegerFromI64(a) }; let b = unsafe { Dart_NewIntegerFromI64(b) }; a * b } ```
```dart import 'dart:ffi';
// open and link to the native library final DynamicLibrary nativeLib = DynamicLibrary.open('libdartsysexample_extension.so');
// lookup the sum function in the native library
final int Function(int, int) sum = nativeLib
.lookup
// lookup the product function in the native library
final int Function(int, int) product = nativeLib
.lookup
void main() { print(sum(1, 2)); // 3 print(product(1, 2)); // 2 } ```
While this example is certainly possible, you are not likely to ever use Dart-sys for this purpose. See the examples directory for more in-depth examples of how to use Dart-sys. All of the examples are tested using GitHub Actions and documented verbosely.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests. If you have any questions, please open an issue, or contact admin gutenfries@gmail.com directly.
We use SemVer for versioning. For the versions available, see the tags on this repository.
Dart-sys is open-sourced and released under the terms and conditions of the following three licenses: