Skip to content

Commit

Permalink
Merge pull request #1581 from fzyzcjy/feat/11653
Browse files Browse the repository at this point in the history
Provide setup for logging, backtrace, etc, by default, but allow easy customization
  • Loading branch information
fzyzcjy authored Jan 2, 2024
2 parents 9e31674 + 83b26a4 commit 8eeafe9
Show file tree
Hide file tree
Showing 59 changed files with 1,123 additions and 31 deletions.
53 changes: 52 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ This package is [officially Flutter Favorite](https://docs.flutter.dev/packages-
* **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.
* **Battery included**: Even small things like logging and enable backtraces are configured in the starter kit.

### 3. Powerfulness

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
pub fn greet(name: String) -> String {
format!("Hello, {name}!")
}

#[flutter_rust_bridge::frb(init)]
pub fn init_app() {
// Default utilities - feel free to customize
flutter_rust_bridge::setup_default_user_utils();
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ pub extern "C" fn frbgen_REPLACE_ME_DART_PACKAGE_NAME_wire_greet(
wire_greet_impl(name)
}

#[no_mangle]
pub extern "C" fn frbgen_REPLACE_ME_DART_PACKAGE_NAME_wire_init_app(port_: i64) {
wire_init_app_impl(port_)
}

#[no_mangle]
pub extern "C" fn frbgen_REPLACE_ME_DART_PACKAGE_NAME_cst_new_list_prim_u_8(
len: i32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ fn wire_greet_impl(
},
)
}
fn wire_init_app_impl(port_: flutter_rust_bridge::for_generated::MessagePort) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "init_app",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
move |context| {
transform_result_dco(
(move || Result::<_, ()>::Ok(crate::api::simple::init_app()))(),
)
}
},
)
}

// Section: dart2rust

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ pub fn dart_fn_deliver_output(
pub fn wire_greet(name: String) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
wire_greet_impl(name)
}

#[wasm_bindgen]
pub fn wire_init_app(port_: flutter_rust_bridge::for_generated::MessagePort) {
wire_init_app_impl(port_)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,26 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
RustLibWire.fromExternalLibrary;

@override
Future<void> executeRustInitializers() async {}
Future<void> executeRustInitializers() async {
await api.initApp();
}

@override
ExternalLibraryLoaderConfig get defaultExternalLibraryLoaderConfig =>
kDefaultExternalLibraryLoaderConfig;

static const kDefaultExternalLibraryLoaderConfig =
ExternalLibraryLoaderConfig(
stem: 'REPLACE_ME_RUST_CRATE_NAME',
stem: 'rust_lib',
ioDirectory: 'rust/target/release/',
webPrefix: 'pkg/',
);
}

abstract class RustLibApi extends BaseApi {
String greet({required String name, dynamic hint});

Future<void> initApp({dynamic hint});
}

class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
Expand Down Expand Up @@ -93,6 +97,28 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["name"],
);

@override
Future<void> initApp({dynamic hint}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
return wire.wire_init_app(port_);
},
codec: DcoCodec(
decodeSuccessData: dco_decode_unit,
decodeErrorData: null,
),
constMeta: kInitAppConstMeta,
argValues: [],
apiImpl: this,
hint: hint,
));
}

TaskConstMeta get kInitAppConstMeta => const TaskConstMeta(
debugName: "init_app",
argNames: [],
);

@protected
String dco_decode_String(dynamic raw) {
return raw as String;
Expand Down
Loading

0 comments on commit 8eeafe9

Please sign in to comment.