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.29k stars 301 forks source link

Generate Box<Fn()> error #2409

Closed lbhfuture closed 6 days ago

lbhfuture commented 6 days ago

Describe the bug

  1. Write Rust code with a Fn running in tokio::spawn, as below:
    pub async fn start(&self, callback: Box<dyn Fn() -> DartFnFuture<bool> + Send>) {
        tokio::spawn(async move {
            loop {
                callback().await;
            }
        });
    }
  2. Then the FRB generated codes as below:
    
    Future<void> start({required BoxFnDartFnFutureBool callback});

abstract class BoxFnDartFnFutureBool implements RustOpaqueInterface {}

3. When I use the `start` function in Dart, I cannot return a bool value in the `callback`. Is it a bug, or how can I use the `start` function correctly? The analyzer error report is `The argument type 'Future<bool> Function()' can't be assigned to the parameter type 'BoxFnDartFnFutureBool'.`.

### Steps to reproduce

Just do as the description above.

### Logs

```shell
No logs.

Expected behavior

No response

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

No response

Flutter info

No response

Version of clang++

No response

Additional context

No response

xuxiaocheng0201 commented 6 days ago

Try callback: impl Fn() -> DartFnFuture<bool> + Send?

fzyzcjy commented 6 days ago

Totally agree with @xuxiaocheng0201, maybe have a try about that :)

lbhfuture commented 6 days ago

I use callback: impl Fn() -> DartFnFuture<bool> + Send + 'static works, thanks.