fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.61k stars 254 forks source link
bindgen dart ffi flutter rust

flutter_rust_bridge v2: Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Rust Package Flutter Package Stars CI Post-Release codecov All Contributors Codacy Badge

Logo

What's new in V2

Click to expand * **Rapid setup**: Only a one-liner command to integrate into your project. * **Arbitrary types**: Use arbitrary Rust and Dart types without manual intervention, even if they are not serializable or non-clone (previously need some manual intervention). * **Async Rust**: Support asynchronous Rust (`async fn`), in addition to sync Rust / async Dart / sync Dart. * **Rust call Dart**: Allow Rust to call Dart functions (previously only allow Dart to call Rust). * **Support whole folders as inputs**: Previously only support one single file (e.g. `api.rs`). * **Use libraries/tools in Flutter/Rust**: All existing libraries, Flutter debuggers, ... Nothing to stop you from using them. * **New codec**: A new codec, `SSE`, which is several times faster under typical workload. Please visit [this page](https://fzyzcjy.github.io/flutter_rust_bridge/guides/miscellaneous/whats-new) for more information and update guide. I want to keep it in beta for a while (though CI has all passed), to allow publishing API breaking changes, and hear your thoughts and suggestions about it!

๐Ÿ€ What's this?

๐Ÿ“š Quickstart

Create a working Flutter + Rust app and see it live, by running:

cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0' && \
    flutter_rust_bridge_codegen create my_app && cd my_app && flutter run

(Optional) Edit rust/src/api/simple.rs (e.g. Hello -> Hi), then see the change by:

flutter_rust_bridge_codegen generate && flutter run

For more elaborated quickstart, please visit this page.

๐Ÿš€ Advantages

1. Officially Flutter Favorite

This package is officially Flutter Favorite, and is in the first batch of 7 packages at its rebooting.

2. Simplicity

3. Powerfulness

4. Reliability

Why Flutter + Rust?

Click to expand Firstly, super briefly introduce each component (you can find much more in a lot of blogs and posts): * **[Flutter](https://flutter.dev/)**: Cross-platform, hot-reload, rapid-development, flexible UI toolkit. * "The most popular cross-platform mobile SDK" (by StackOverflow [[1]](https://stackoverflow.blog/2022/02/21/why-flutter-is-the-most-popular-cross-platform-mobile-sdk/)[[2]](https://survey.stackoverflow.co/2023/#technology-most-popular-technologies)). * **[Rust](https://www.rust-lang.org/)**: Highly efficient and performant, reliable, productive. * "The most desired programming language" for 8 years (by StackOverflow and GitHub [[1]](https://github.blog/2023-08-30-why-rust-is-the-most-admired-language-among-developers/)[[2]](https://survey.stackoverflow.co/2023/#section-admired-and-desired-programming-scripting-and-markup-languages)). 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. * ...

๐Ÿงญ Show me the code

Example 1: Simple

Simple Rust...

fn f(a: String, b: Vec<String>) -> MyStruct { ... }

...called from Dart, without manual intervention.

print(f(a: 'Hello', b: ['Tom']));

Example 2: Show off skills ;)

Let's see how fancy we can support:

// โ†ฑ Arbitrarily fancy Rust types
struct Garden { land: whatever::fancy::Land }

// โ†ฑ Complex but auto-translatable
enum Tree { A { name: (String, i32), children: Option<Vec<Tree>> }, B }

// โ†ฑ Support functions & methods
impl Garden {
    // โ†ฑ Allow async & sync Rust
    async fn plant(
        // โ†ฑ Support T/&T/&mut T
        &mut self,
        tree: Tree,
        // โ†ฑ Rust can also call Dart
        chooser: impl Fn(String) -> bool,
        // โ†ฑ Error translation ; zero copy
    ) -> Result<Vec<u8>, FancyError> {
        ...
    }
}

Still seamlessly call in Dart:

var tree = Tree.a(('x', 42), [Tree.b()]);
// โ†ฑ Async & sync Dart
print(await garden.plant(tree, (a) => true));

๐Ÿ’ก Documentation

Check out the documentation for quickstart, full guides and more.

๐Ÿ“Ž P.S. Achieve ~60 FPS, no matter how janky the Flutter app was due to build/layout

Here is my another open-source library :) https://github.com/fzyzcjy/flutter_smooth.

โœจ Acknowledgments and contributors

Firstly, I want to sincerely thank Dart, Flutter and Rust (alphabetical order). Dart provides a solid foundation for productive UI development, Flutter enables developers to make cross-platform apps with ease, and Rust empowers everyone to build reliable and efficient software. Without the languages and frameworks, this bridge connects absolutely nothing. Besides, I also want to express my thanks for conferring the official Flutter Favorite honor to the package. In addition, I also want to say thanks to the Dart, Flutter and Rust team members as well as community members, who have helped me during the development of flutter_rust_bridge by valuable discussions, insights, and actions.

Secondly, thanks goes to these wonderful contributors (emoji key following all-contributors specification):

fzyzcjy
fzyzcjy

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿค” ๐Ÿšง
Viet Dinh
Viet Dinh

๐Ÿ’ป โš ๏ธ ๐Ÿ“–
rogurotus
rogurotus

๐Ÿ’ป ๐Ÿ“–
Nicolas Gasull
Nicolas Gasull

๐Ÿ’ป
Joshua Wade
Joshua Wade

๐Ÿ’ป
Lattice 0
Lattice 0

๐Ÿ’ป ๐Ÿ“–
Unoqwy
Unoqwy

๐Ÿ’ป
Anton Lazarev
Anton Lazarev

๐Ÿ’ป
sagu
sagu

๐Ÿ’ป ๐Ÿ“–
Sebastian Urban
Sebastian Urban

๐Ÿ’ป
Rom's
Rom's

๐Ÿ’ป ๐Ÿ“–
่€่‘ฃ
่€่‘ฃ

๐Ÿ’ป ๐Ÿ“–
Gregory Conrad
Gregory Conrad

๐Ÿ“– ๐Ÿ’ป
huang12zheng
huang12zheng

๐Ÿ’ป ๐Ÿ“–
Daniel
Daniel

๐Ÿ’ป
Manuel Philipp
Manuel Philipp

๐Ÿ’ป ๐Ÿ“–
SoLongAnd...
SoLongAnd...

๐Ÿ’ป ๐Ÿ“–
hsfzxjy
hsfzxjy

๐Ÿ’ป
Cupnfish
Cupnfish

๐Ÿ’ป
alanlzhang
alanlzhang

๐Ÿ’ป ๐Ÿ“–
Erikas Taroza
Erikas Taroza

๐Ÿ’ป
่˜่˜
่˜่˜

๐Ÿ’ป
SimplyKyle!
SimplyKyle!

๐Ÿ’ป
Zaitam
Zaitam

๐Ÿ’ป
Brent Lewis
Brent Lewis

๐Ÿ’ป ๐Ÿ“–
nitn3lav
nitn3lav

๐Ÿ’ป ๐Ÿ“–
Kevin Li
Kevin Li

๐Ÿ’ป ๐Ÿ“–
Alex Procelewski
Alex Procelewski

๐Ÿ“– ๐Ÿ’ป
Daniel Porteous (dport)
Daniel Porteous (dport)

๐Ÿ“–
Andreas Monitzer
Andreas Monitzer

๐Ÿ’ป
Kim Dong-Hyun
Kim Dong-Hyun

๐Ÿ’ป ๐Ÿ“–
NightFeather
NightFeather

๐Ÿ’ป
ไนๆœˆ
ไนๆœˆ

๐Ÿ’ป
Krysl
Krysl

๐Ÿ’ป
Wouter Ensink
Wouter Ensink

๐Ÿ“–
Marcel
Marcel

๐Ÿ’ป
Aidan
Aidan

๐Ÿ“–
Debanjan Basu
Debanjan Basu

๐Ÿ“–
Patrick Auernig
Patrick Auernig

๐Ÿ’ป
Sai Chaitanya
Sai Chaitanya

๐Ÿ’ป
Xidorn Quan
Xidorn Quan

๐Ÿ’ป
jsonmona
jsonmona

๐Ÿ’ป
mtz
mtz

๐Ÿ’ป
codercengiz
codercengiz

๐Ÿ’ป
Michael Bryan
Michael Bryan

๐Ÿ’ป
Patrick Mukherjee
Patrick Mukherjee

๐Ÿ’ป
Aran Donohue
Aran Donohue

๐Ÿ’ป
Philip Kannegaard Hayes
Philip Kannegaard Hayes

๐Ÿ’ป
SilverMira
SilverMira

๐Ÿ’ป
Sander in 't Hout
Sander in 't Hout

๐Ÿ’ป
Haled Odat
Haled Odat

๐Ÿ’ป
็Ž‹ๅฎ‡้€ธ
็Ž‹ๅฎ‡้€ธ

๐Ÿ’ป
bus710
bus710

๐Ÿ“–
._.
._.

๐Ÿ“–
Marc Gutenberger
Marc Gutenberger

๐Ÿ’ป
Andrii Stadnik
Andrii Stadnik

๐Ÿ’ป
syndim
syndim

๐Ÿ’ป
Rhian Moraes
Rhian Moraes

๐Ÿ“–
Ares Andrew
Ares Andrew

๐Ÿ“–
polypixeldev
polypixeldev

๐Ÿ“–
CicadaCinema
CicadaCinema

๐Ÿ’ป ๐Ÿ“–
CosmicHorror
CosmicHorror

๐Ÿ’ป
Akash Gurava
Akash Gurava

๐Ÿ’ป
Fabian Lรถschner
Fabian Lรถschner

๐Ÿ’ป
Vincent Herlemont
Vincent Herlemont

๐Ÿ’ป
wxitcode
wxitcode

๐Ÿ“–
canxin
canxin

๐Ÿ’ป
pixelshot91
pixelshot91

๐Ÿ“–
TrackerSB
TrackerSB

๐Ÿ’ป
Dampfwalze
Dampfwalze

๐Ÿ“–
Samuel Cavalcanti
Samuel Cavalcanti

๐Ÿ“–
Roman Zaynetdinov
Roman Zaynetdinov

๐Ÿ“–
raphaelrobert
raphaelrobert

๐Ÿ“–
Mouayad Alhamwi
Mouayad Alhamwi

๐Ÿ“–
elliotsayes
elliotsayes

๐Ÿ“–
muji
muji

๐Ÿ“–
thomas725
thomas725

๐Ÿ“–
orange soeur
orange soeur

๐Ÿ“–
Alex Gorichev
Alex Gorichev

๐Ÿ“–
Sven-Hendrik Haase
Sven-Hendrik Haase

๐Ÿ“–
Chris Ohk
Chris Ohk

๐Ÿ“–
Vitalii Hurianov
Vitalii Hurianov

๐Ÿ“–
Sam Nystrom
Sam Nystrom

๐Ÿ“–
mattiasgronlund
mattiasgronlund

๐Ÿ’ป
Antonio D'souza
Antonio D'souza

๐Ÿ“–
max
max

๐Ÿ“–
Jonathan
Jonathan

๐Ÿ“–
Akash Jaiswal
Akash Jaiswal

๐Ÿ“–
Febrian Setianto
Febrian Setianto

๐Ÿ“–
Satvik Pendem
Satvik Pendem

๐Ÿ’ป
Damien Wise
Damien Wise

๐Ÿ“–
rustui
rustui

๐Ÿ“–
J
J

๐Ÿ“–
Ikko Ashimine
Ikko Ashimine

๐Ÿ“–
thesimplekid
thesimplekid

๐Ÿ“–

More specifically, thanks for all these contributions: