Crates.io | dart-sys |
lib.rs | dart-sys |
version | 4.1.5 |
source | src |
created_at | 2019-09-30 01:00:20.09481 |
updated_at | 2024-03-09 02:35:13.296197 |
description | Statically generated, Opt-in style bindings to the Dart SDK |
homepage | |
repository | https://github.com/dart-sys/dart-sys |
max_upload_size | |
id | 168722 |
size | 401,620 |
Rust bindings to the Dart ffi api
You will need the following tools available on your system:
No additional requirements :)
No additional requirements :)
On Windows platforms, dynamic libraries are linked against the executable, not into the executable as is the case o Unix platforms.
⚠️ Important ⚠️
This means that (on Windows) you will Need to have the Dart SDK installed and available on your system path to be able to compile Dart-sys.
Run the following Cargo command in your project directory:
cargo add dart-sys
Or add the following line to your Cargo.toml:
dart-sys = "4.1.5"
An extremely straightforward example of using dart-sys
would be like such:
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
}
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 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 one or both of the following licenses: