fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.12k stars 282 forks source link

Docs/examples/fixes to make rust-async, threadpool optional #2253

Closed aran closed 3 days ago

aran commented 3 weeks ago

Is your feature request related to a problem? Please describe. From website/docs/guides/custom/rust/feature-flags.md it may be possible to remove unused features from the FRB import.

In my project at least, in disabling threadpool, the default generated executor seems to break compilation:

error[E0599]: the method `wrap_sync` exists for struct `FLUTTER_RUST_BRIDGE_HANDLER`, but its trait bounds were not satisfied
    --> .../frb_generated.rs:4454:33
     |
45   | flutter_rust_bridge::frb_generated_default_handler!();
     | ----------------------------------------------------- method `wrap_sync` not found for this struct
...
4454 |     FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::SseCodec, _>(
     |     ----------------------------^^^^^^^^^ method cannot be called on `FLUTTER_RUST_BRIDGE_HANDLER` due to unsatisfied trait bounds
     |
    ::: /build/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge-2.3.0/src/handler/implementation/handler.rs:42:1
     |
42   | pub struct SimpleHandler<E: Executor, EL: ErrorListener> {
     | -------------------------------------------------------- doesn't satisfy `_: Handler`
     |
    ::: /build/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge-2.3.0/src/handler/implementation/executor.rs:23:1
     |
23   | pub struct SimpleExecutor<EL: ErrorListener, TP: BaseThreadPool, AR: BaseAsyncRuntime> {
     | -------------------------------------------------------------------------------------- doesn't satisfy `_: Executor`
     |
     = note: the following trait bounds were not satisfied:
             `SimpleExecutor<NoOpErrorListener, &LocalKey<SimpleThreadPool>, SimpleAsyncRuntime>: Executor`
             which is required by `SimpleHandler<SimpleExecutor<NoOpErrorListener, &LocalKey<SimpleThreadPool>, SimpleAsyncRuntime>, NoOpErrorListener>: Handler`
     = help: items from traits can only be used if the trait is implemented and in scope
     = note: the following trait defines an item `wrap_sync`, perhaps you need to implement it:
             candidate #1: `Handler`

In disabling rust-async—I am trying to use all methods as #[frb(sync)], but it's possible I've missed something—I see errors due to Lockable, RustAutoOpaqueInner, LockableOrderInfo and a few others. The RustAutoOpaqueInner errors are tied to types that I have pub use imported, frb(mirror(, and #[frb(external)]. E.g.:

error[E0412]: cannot find type `RustAutoOpaqueInner` in module `flutter_rust_bridge::for_generated`
   --> .../rust/src/frb_generated.rs:197:53
    |
197 |                 flutter_rust_bridge::for_generated::RustAutoOpaqueInner<MyType>,
    |                                                     ^^^^^^^^^^^^^^^^^^^ not found in `flutter_rust_bridge::for_generated`
    |
note: found an item that was configured out
   --> /build/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge-2.3.0/src/for_generated/mod.rs:62:42
    |
62  | pub use crate::rust_auto_opaque::{inner::RustAutoOpaqueInner, RustAutoOpaqueBase};
    |                                          ^^^^^^^^^^^^^^^^^^^
note: the item is gated behind the `rust-async` feature
   --> /build/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge-2.3.0/src/for_generated/mod.rs:61:7
    |
61  | #[cfg(feature = "rust-async")]
    |       ^^^^^^^^^^^^^^^^^^^^^^

Describe the solution you'd like Not sure. I am already using, or intending to use, all sync methods for thread safety on wasm platform. I don't think I am using the threadpools. I would be looking for either documentation, example code, or code-generator errors to lead me to what I've done wrong here.

Describe alternatives you've considered N/A

fzyzcjy commented 3 weeks ago

Firstly, I wonder why do you want to disable those features? IIRC Rust does a lot of optimizations and will eliminate dead code if you do not use a feature automatically.

Secondly, for thread pools: https://github.com/fzyzcjy/flutter_rust_bridge/issues/2215 may be related if you want to disable it because of the web things.

aran commented 3 weeks ago

My thinking was, I'm not (supposed to be) using them. If I were to use the threadpool accidentally, that would be a bug. I do see some extra activity in web console in setting up the service workers which I assume is due to the threadpool.

For async, just looking for opportunities to cut down surface area.

fzyzcjy commented 3 weeks ago

I see, then #2215 may be useful for thread pool thing. For async, looks like e.g. opaque types uses tokio rwlock currently, so it requires the tokio dependency, and thus the flag async.

fzyzcjy commented 3 days ago

Close since that issue is closed. But feel free to reopen this issue if you have any questions!