flutter_rust_bridge v2: Officially Flutter Favorite, setup in one command, use arbitrary types automatically, support async Rust, allow Rust call Dart, and full folder support. Write UI using Flutter, a cross-6-platform hot-reload toolkit, seamlessly with Rust

81 points fzyzcjy

Link: https://github.com/fzyzcjy/flutter\_rust\_bridge

Hi friends, I am excited to announce the v2!

Your suggestions

Nothing is perfect (though v2 has many new features), thus I want to keep it in beta for a while (to allow publishing API breaking changes) and hear your thoughts and suggestions about it!

Advantages

​

https://preview.redd.it/49eb554tyj7c1.png?width=400&format=png&auto=webp&s=c4ce4f3845975fa4078a257733fb1feafd399fb9

  • In one sentence
    • Just write down some normal Rust code (possibly with unencodable types, closure, &mut, async, etc), and call it from Flutter as if it is normal Dart(Flutter) code; the bridge will generate all needed glues in between.
  • Officially Flutter Favorite
  • Simpleness
    • Rapid setup: Only a one-liner command to integrate into your project.
    • Write your code naturally: Use your intuition and write the code you want. The bridge understands many advanced grammars (see below), allowing seamless calling Rust from Dart.
    • Use libraries/tools in Flutter/Rust: All existing libraries, Flutter debuggers, ... Nothing to stop you from using them.
  • Powerfulness
    • Arbitrary types: Use arbitrary Rust and Dart types, even if they are not serializable or non-clone.
    • Async & sync x Rust & Dart: Multi modes for various needs - Async Dart to avoid blocking the main thread, sync Dart for places needed (e.g. Widget.build); async Rust for IO bound tasks, thread pools for CPU-heavy computations.
    • Two-way road: Not only can Dart call Rust - Rust can also call Dart.
    • Auto-translatable types: Lots of types can be further translated to Dart native types, e.g. complex enums and structs, zero-copy big arrays, errors (Result), and Streams (iterator).
    • Auto safety: Focus on your code, and forget memory safety, malloc/free, or undefined behavior completely.
    • Customizable & bare-metal mode: Provide sensible defaults, but everything (loader, handler, ...) can be customized. You can even throw all away and only use the bare minimum calling.
    • Cross-platform: Support Android, iOS, Windows, Linux, MacOS, and Web.
    • Other features, e.g. support whole folders as input, pure-Dart compatible, instance and static methods, ...
  • Reliability
    • Solid CI: Valgrind & sanitizers (ASAN/MSAN/LSAN) for memory/UB-related bugs, testing per platform per mode, benchmarking, codecov, etc, all guaranteed by CI.
    • Used by many people: See here for an incomplete list.
    • Easy to code-review & convince yourself: This package simply simulates how humans write boilerplate code. If you want to convince yourself (or your team) that it is safe, there is not much code to track.
    • Fast: It is only a thin (though feature-rich) wrapper, benchmarked on CI, and even has multiple codecs for best performance under different workloads.

Show me the code, Quickstart, Doc

(Please visit the homepage)

Why Flutter

Flutter is a modern, cross-platform, hot-reload, rapid-development, flexible UI toolkit. It is "the most popular cross-platform mobile SDK" (by StackOverflow 1, 2).

Typical scenarios to combine them include:

  • UI framework for Rust: When you want a UI framework for your Rust system.
  • Use arbitrary Rust libraries in Flutter: When the desired functionality only has a library in Rust, not Dart (Flutter).
  • Need high-performance code for Flutter: Rust makes it easy and performant to write multi-thread code, algorithms, data-intensive operations, SIMD code, etc.
  • ...