-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add examples to documentation; especially for Stream (the Stream part…
… relates to #347)
- Loading branch information
Showing
10 changed files
with
335 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,60 @@ | ||
# `enum`s | ||
|
||
Rust's `enum` are known to be very expressive and powerful - it allows each enum variant to have different associated data. Dart does not have such things in built-in enums, but no worries - we will automatically translate it into the equivalent using the `freezed` Dart library. | ||
Rust's `enum` are known to be very expressive and powerful - it allows each enum variant to have different associated data. Dart does not have such things in built-in enums, but no worries - we will automatically translate it into the equivalent using the `freezed` Dart library. The syntax for `freezed` may look a bit strange at the first glance, but please look at [its doc](https://pub.dev/packages/freezed) and see its powerfulness. | ||
|
||
## Example | ||
|
||
```rust,noplayground | ||
pub enum KitchenSink { | ||
Empty, | ||
Primitives { | ||
/// Dart field comment | ||
int32: i32, | ||
float64: f64, | ||
boolean: bool, | ||
}, | ||
Nested(Box<KitchenSink>), | ||
Optional( | ||
/// Comment on anonymous field | ||
Option<i32>, | ||
Option<i32>, | ||
), | ||
Buffer(ZeroCopyBuffer<Vec<u8>>), | ||
Enums(Weekdays), | ||
} | ||
``` | ||
|
||
Becomes: | ||
|
||
```Dart | ||
@freezed | ||
class KitchenSink with _$KitchenSink { | ||
/// Comment on variant | ||
const factory KitchenSink.empty() = Empty; | ||
const factory KitchenSink.primitives({ | ||
/// Dart field comment | ||
required int int32, | ||
required double float64, | ||
required bool boolean, | ||
}) = Primitives; | ||
const factory KitchenSink.nested( | ||
KitchenSink field0, | ||
) = Nested; | ||
const factory KitchenSink.optional([ | ||
/// Comment on anonymous field | ||
int? field0, | ||
int? field1, | ||
]) = Optional; | ||
const factory KitchenSink.buffer( | ||
Uint8List field0, | ||
) = Buffer; | ||
const factory KitchenSink.enums( | ||
Weekdays field0, | ||
) = Enums; | ||
} | ||
``` | ||
|
||
And they are powered with [all functionalities](https://pub.dev/packages/freezed) of `freezed`. | ||
|
||
Remark: If you are curious about `Future`, have a look at [this](async_dart.md). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# `Option`s | ||
|
||
Dart has special syntaxs for nullable variables - the `?` symbol, and we translate `Option` into `?` automatically. You may refer to [the official doc](https://dart.dev/null-safety) for more information. | ||
|
||
In addition, `flutter_rust_bridge` also understands the `required` keyword in Dart: If an argument is not-null, it is marked as `required` since you have to provide a value. On the other hand, if it is nullable, no `required` is needed since by Dart's convention a null is there in absence of manually providing a value. | ||
|
||
## Example | ||
|
||
```rust,noplayground | ||
pub struct Element { | ||
pub tag: Option<String>, | ||
pub text: Option<String>, | ||
pub attributes: Option<Vec<Attribute>>, | ||
pub children: Option<Vec<Element>>, | ||
} | ||
pub fn parse(mode: String, document: Option<String>) -> Option<Element> { ... } | ||
``` | ||
|
||
Becomes: | ||
|
||
```Dart | ||
Future<Element?> handleOptionalStruct({required String mode, String? document}); | ||
class Element { | ||
final String? tag; | ||
final String? text; | ||
final List<Attribute>? attributes; | ||
final List<Element>? children; | ||
Element({this.tag, this.text, this.attributes, this.children}); | ||
} | ||
``` | ||
|
||
Remark: If you are curious about `Future`, have a look at [this](async_dart.md). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
# `use` | ||
|
||
Imported symbols can be used normally. For example, with `use crate::data::{MyEnum, MyStruct};`, you can use `MyEnum` or `MyStruct` in your code normally. | ||
Imported symbols can be used normally. For example, with `use crate::data::{MyEnum, MyStruct};`, you can use `MyEnum` or `MyStruct` in your code normally. | ||
|
||
## Example | ||
|
||
```rust,noplayground | ||
use crate::data::{MyEnum, MyStruct}; | ||
pub fn use_imported_things(my_struct: MyStruct, my_enum: MyEnum) { ... } | ||
``` | ||
|
||
Becomes: | ||
|
||
```Dart | ||
// Well it just behaves normally as you expect | ||
Future<void> useImportedThings({required MyStruct myStruct, required MyEnum myEnum}); | ||
``` | ||
|
||
Remark: If you are curious about `Future`, have a look at [this](async_dart.md). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
# `Vec<u8>` and `Vec<T>` | ||
|
||
In Dart, when you want to express a long byte array such as a big image or some binary blob, people normally use `Uint8List` instead of `List<int>` since the former is much performant. `flutter_rust_bridge` takes this into consideration for you. When you have `Vec<u8>` (or `Vec<i8>`, or `Vec<i32>`, etc), it will be translated it into `Uint8List` or its friends; but when you have normal `Vec<T>` for other `T` types, it will be normal `List<T>`. | ||
In Dart, when you want to express a long byte array such as a big image or some binary blob, people normally use `Uint8List` instead of `List<int>` since the former is much performant. `flutter_rust_bridge` takes this into consideration for you. When you have `Vec<u8>` (or `Vec<i8>`, or `Vec<i32>`, etc), it will be translated it into `Uint8List` or its friends; but when you have normal `Vec<T>` for other `T` types, it will be normal `List<T>`. | ||
|
||
## Example | ||
|
||
```rust,noplayground | ||
pub fn draw_tree(tree: Vec<TreeNode>) -> Vec<u8> { ... } | ||
``` | ||
|
||
Becomes: | ||
|
||
```Dart | ||
Future<Uint8List> drawTree({required List<TreeNode> tree}); | ||
``` | ||
|
||
Remark: If you are curious about `Future`, have a look at [this](async_dart.md). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
# Zero copy | ||
|
||
`ZeroCopyBuffer<Vec<u8>>` (and its friends like `ZeroCopyBuffer<Vec<i8>>`) sends the data from Rust to Dart without making copies. Thus, you save the time of copying data, which can be large if your data is big (such as a high-resolution image). | ||
`ZeroCopyBuffer<Vec<u8>>` (and its friends like `ZeroCopyBuffer<Vec<i8>>`) sends the data from Rust to Dart without making copies. Thus, you save the time of copying data, which can be large if your data is big (such as a high-resolution image). | ||
|
||
## Example | ||
|
||
```rust,noplayground | ||
pub fn draw_tree(tree: Vec<TreeNode>) -> ZeroCopyBuffer<Vec<u8>> { ... } | ||
``` | ||
|
||
Becomes: | ||
|
||
```Dart | ||
Future<Uint8List> drawTree({required List<TreeNode> tree}); | ||
``` | ||
|
||
The generated Dart code looks exactly the same as the case without `ZeroCopyBuffer`. However, the internal implementation changes and there is no memory copy at all! | ||
|
||
Remark: If you are curious about `Future`, have a look at [this](async_dart.md). |