#dart #ffi #flutter #bindings #dart-native

sys dart-sys

Statically generated, Opt-in style bindings to the Dart SDK

23 stable releases (3 major)

new 4.0.2 Feb 2, 2023
4.0.1 Feb 1, 2023
3.1.12 Jan 26, 2023
2.0.1 Mar 25, 2020
0.1.2 Sep 30, 2019

#36 in FFI

Download history 123/week @ 2022-10-13 84/week @ 2022-10-20 31/week @ 2022-10-27 83/week @ 2022-11-03 81/week @ 2022-11-10 202/week @ 2022-11-17 455/week @ 2022-11-24 2218/week @ 2022-12-01 6992/week @ 2022-12-08 279/week @ 2022-12-15 125/week @ 2022-12-22 148/week @ 2022-12-29 106/week @ 2023-01-05 189/week @ 2023-01-12 410/week @ 2023-01-19 3028/week @ 2023-01-26

3,755 downloads per month
Used in 3 crates

GPL-3.0-or-later AND MIT AND Apache-2.0

205KB
2K SLoC

Dart-sys brand header

Dart-sys

Stars Forks Crates.io License: MIT License: Apache 2.0 License: GNU GPL v3 Docs.rs CI

Rust bindings to the Dart ffi api

Prerequisites 🔧

You will need the following tools available on your system:

Unix/Linux 🐧

No additional requirements :)

MacOS 🍎

No additional requirements :)

Windows 🪟

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.

Installing 📦

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.0.2"

Usage 💻

Examples 📚

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.

Built With 🛠️

  • Rust - A systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.
  • Dart - A client-optimized language for fast apps on any platform.
  • Dart Native Extensions - A mechanism for writing native code in C/C++ and calling it from Dart.
  • bindgen - A Rust library for generating bindings to C and C++ APIs.

Contributing ✏️

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.

Versioning 🪧

We use SemVer for versioning. For the versions available, see the tags on this repository.

License 📜

Dart-sys is open-sourced and released under the terms and conditions of the following three licenses:

Acknowledgments 🙏

No runtime deps

~150KB