-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2056 from fzyzcjy/feat/12108
Add documentations
- Loading branch information
Showing
31 changed files
with
679 additions
and
158 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
3 changes: 2 additions & 1 deletion
3
frb_codegen/assets/integration_template/flutter_rust_bridge.yaml
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,2 +1,3 @@ | ||
rust_input: REPLACE_ME_RUST_CRATE_DIR/src/api/**/*.rs | ||
rust_input: crate::api | ||
rust_root: REPLACE_ME_RUST_CRATE_DIR/ | ||
dart_output: lib/src/rust |
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,4 @@ | ||
rust_input: rust/src/api/**/*.rs | ||
rust_input: crate::api | ||
rust_root: rust/ | ||
dart_output: lib/src/rust | ||
local: true |
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,4 @@ | ||
rust_input: rust/src/api/**/*.rs | ||
rust_input: crate::api | ||
rust_root: rust/ | ||
dart_output: lib/src/rust | ||
local: true |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Rename Dart types | ||
|
||
The automatically generated type names can be customized. | ||
For example, `Box<dyn Any + Send + Sync + 'static>` has corresponding Dart name as `BoxAny` by default. | ||
Suppose we want to change it to `MyFancyName`, then we can configure as follows in `flutter_rust_bridge.yaml`: | ||
|
||
```yaml | ||
dart_type_rename: | ||
Box<dyn Any + Send + Sync + 'static>: MyFancyName | ||
``` |
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,13 @@ | ||
# Multiple input folders | ||
|
||
The `rust_input` configuration key supports multiple entries separated by commas. | ||
For example, consider the following configuration: | ||
|
||
```yaml | ||
rust_input: crate::api,crate::hello::world | ||
``` | ||
Roughly speaking, it will scan `src/api/**/*.rs` and `src/hello/world/**/*.rs`. | ||
|
||
More strictly speaking, it scans Rust modules instead of real files, thus complex scenarios such as multiple modules | ||
inside one file are supported. |
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,18 @@ | ||
# `frb_override_*` prefix | ||
|
||
If a method name starts with `frb_override_`, | ||
then it will be automatically recognized as if it does not have that prefix, | ||
and it gains privilege to override other existing methods (i.e. remove methods with same name). | ||
|
||
This is helpful when wanting to override existing methods, | ||
and also helpful when the name conflicts (such as in `#[ext]`). | ||
|
||
## Example | ||
|
||
```rust | ||
impl MyStruct { | ||
pub fn frb_override_hello(&self, a: i32) -> i32 {} | ||
} | ||
``` | ||
|
||
Then it is equivalent to `fn hello(...)` with privilege. |
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,71 @@ | ||
# Proxy | ||
|
||
When annotating a method with `#[frb(proxy)]`, | ||
the method will be allowed to return a reference type, | ||
and the behavior will be explained below. | ||
|
||
Typically, this can be used to expose struct or enum fields. | ||
|
||
## Example | ||
|
||
### Scenario | ||
|
||
Consider the following code: | ||
|
||
```rust | ||
#[frb(opaque)] | ||
pub struct BiquadFilterNode { | ||
frequency: AudioParam, | ||
} | ||
|
||
impl BiquadFilterNode { | ||
pub fn frequency(&self) -> &AudioParam { | ||
&self.frequency | ||
} | ||
} | ||
|
||
pub struct AudioParam { ... } | ||
|
||
impl AudioParam { | ||
pub fn my_method_one(&self, value: f32) { ... } | ||
pub fn my_method_two(&self, value: f32) { ... } | ||
} | ||
``` | ||
|
||
`flutter_rust_bridge` will not be able to generate code for it, since the return type being a reference type | ||
is not supported yet (and if implemented, may have problems such as "borrowing for too long"). | ||
However, if we add `#[frb(proxy)]` to the `fn`, then it will work well. | ||
|
||
### Remark: Alternative solutions | ||
|
||
As is mentioned in [this page](../how-to/borrowed), one alternative solution is to use `clone`: | ||
|
||
```rust | ||
pub fn get_my_sub_struct(&self) -> MySubStruct { | ||
self.frequency.clone() | ||
} | ||
``` | ||
|
||
Another solution is that, we can also utilize `Arc` or `RustAutoOpaque` (which is essentially an `Arc` with something else): | ||
|
||
```rust | ||
frequency: RustAutoOpaque<AudioParam>, | ||
``` | ||
|
||
### (Optional) Under the hood | ||
|
||
Shortly speaking, | ||
the generated code has similar idea to the code below, but the exact details is better. | ||
I will elaborate more if you are interested in it. | ||
|
||
```rust | ||
impl BiquadFilterNode { | ||
pub fn frequency_my_method_one(&self, value: f32) { | ||
self.frequency.my_method_one(value) | ||
} | ||
|
||
pub fn frequency_my_method_two(&self, value: f32) { | ||
self.frequency.my_method_two(value) | ||
} | ||
} | ||
``` |
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,7 @@ | ||
# Automatic | ||
|
||
```mdx-code-block | ||
import DocCardList from '@theme/DocCardList'; | ||
<DocCardList /> | ||
``` |
44 changes: 44 additions & 0 deletions
44
website/docs/guides/third-party/automatic/override-attributes.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,44 @@ | ||
# Override attributes | ||
|
||
If the implementations of an external component (function/method/struct/enum/etc) is good, | ||
and we only want to add some *attributes* to it, | ||
then we only need to write down component name and new attributes inside our first-party package like below. | ||
|
||
The rule is that: | ||
|
||
* Put the code inside `src/third_party/{third-party-crate-name}/{path-to-the-module}`. | ||
* Only write down function name, and no need to specify function arguments and return types. | ||
|
||
## Examples | ||
|
||
Suppose we are interested in the `some_crate::hello::world::SomeStruct::method` method in third-party crate: | ||
|
||
```rust | ||
// This is code in third-party crate, we cannot modify it | ||
pub struct SomeStruct { | ||
... | ||
} | ||
|
||
impl SomeStruct { | ||
pub fn method(&self, a: String, b: Vec<i32>) -> f32 { | ||
... | ||
} | ||
} | ||
``` | ||
|
||
Then, we can write down the following lines to make that method has synchronous Dart code: | ||
|
||
```rust | ||
// src/third_party/some_crate/hello/world/mod.rs | ||
#[frb(external)] | ||
impl SomeStruct { | ||
#[frb(sync)] // <-- This attribute will be auto merged to third-party code | ||
pub fn method() {} | ||
} | ||
``` | ||
|
||
## Remark on `pub use` | ||
|
||
`flutter_rust_bridge` understands syntax like `pub use something::*` and `pub use another::Thing`. | ||
Therefore, if a struct in third party code is defined non-publicly but then re-exported as public using such `pub use` grammar, | ||
`flutter_rust_bridge` will consider that type to be in the latter module. |
47 changes: 47 additions & 0 deletions
47
website/docs/guides/third-party/automatic/override-methods.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,47 @@ | ||
# Override/add methods | ||
|
||
In this page, we show how to do these in the example below: | ||
|
||
* Change the function signature / method implementation / ... of something in a third-party crate | ||
* Add arbitrary methods to existing structs/enums in third party crates | ||
|
||
## (Optional) How it is done | ||
|
||
How is the example below implemented? | ||
Shortly speaking, | ||
the `#[ext]` macro (which implements the "extension trait pattern") automatically generates a trait and an implementation, | ||
which `flutter_rust_bridge` picks up. | ||
Then, the [`frb_override_` prefix](../../miscellaneous/override-prefix) is recognized to automatically rename and override the original function. | ||
|
||
## Example | ||
|
||
### Override existing methods | ||
|
||
Suppose the third party crate has code like: | ||
|
||
```rust | ||
pub struct S { ... } | ||
impl S { | ||
pub fn greet(&self, name: &str) { ... } | ||
} | ||
``` | ||
|
||
Then we can override the `greet` function like: | ||
|
||
```rust | ||
use extend::ext; // or, for example, easy_ext's | ||
use the_external_crate::path::to::S; | ||
|
||
#[ext] | ||
pub impl S { | ||
pub fn frb_override_greet(&self, age: i32, first_name: String, last_name: Vec<u8>) { | ||
// We can have arbitrary implementation. | ||
// Here, we demonstrate how to call the original implementation with modified arguments. | ||
self.greet(format!("{age}-{first_name}-{last_name:?}")) | ||
} | ||
} | ||
``` | ||
|
||
### Add new methods | ||
|
||
It is very similar to the approach above, except that we do not need to prefix with `frb_override_`. |
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,14 @@ | ||
# Scanning | ||
|
||
The first step is to configure to scan the third-party crate. | ||
This is fairly simple - just modify `flutter_rust_bridge.yaml` and change to something like: | ||
|
||
```yaml | ||
rust_input: crate::api,interesting_third_party_crate_name | ||
``` | ||
The line above means we want to both scan `src/api` folder in our crate and scan the `interesting_third_party_crate_name` crate. | ||
|
||
For crate with `-` in the name, we can write `interesting-third-party-crate-name` | ||
|
||
Please refer to [this page](../../miscellaneous/multi-input) for more details of the configuration. |
Oops, something went wrong.