fzyzcjy / flutter_rust_bridge

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

Geneartion of `async` functions when `default_dart_async: false` #2178

Open Larpoux opened 3 days ago

Larpoux commented 3 days ago

Describe the bug

When I set the parameter default_dart_async: false I get after, 10 errors. Most of them are in class Context and ContextOffline.

For example :

fn wire__web_audio_api__context__OfflineAudioContext_suspend_impl(
    ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr,
    rust_vec_len_: i32,
    data_len_: i32,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartSse {
    FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::SseCodec, _>(
        flutter_rust_bridge::for_generated::TaskInfo {
            debug_name: "OfflineAudioContext_suspend",
            port: None,
            mode: flutter_rust_bridge::for_generated::FfiCallMode::Sync,
        },
        move || {
            let message = unsafe {
                flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire(
                    ptr_,
                    rust_vec_len_,
                    data_len_,
                )
            };
            let mut deserializer =
                flutter_rust_bridge::for_generated::SseDeserializer::new(message);
            let api_that = <RustOpaqueMoi<
                flutter_rust_bridge::for_generated::RustAutoOpaqueInner<OfflineAudioContext>,
            >>::sse_decode(&mut deserializer);
            let api_suspend_time = <f64>::sse_decode(&mut deserializer);
            deserializer.end();
            transform_result_sse::<_, ()>((move || {
                let mut api_that_guard = None;
                let decode_indices_ =
                    flutter_rust_bridge::for_generated::lockable_compute_decode_order(vec![
                        flutter_rust_bridge::for_generated::LockableOrderInfo::new(
                            &api_that, 0, false,
                        ),
                    ]);
                for i in decode_indices_ {
                    match i {
                        0 => api_that_guard = Some(api_that.lockable_decode_async_ref().await),
                        _ => unreachable!(),
                    }
                }
                let api_that_guard = api_that_guard.unwrap();
                let output_ok = Result::<_, ()>::Ok({
                    web_audio_api::context::OfflineAudioContext::suspend(
                        &*api_that_guard,
                        api_suspend_time,
                    )
                    .await;
                })?;
                Ok(output_ok)
            })())
        },
    )
}

The await almost at the end of the fn is NOK, because the function is not async. I think the problem is tied with the declaration of suspend() in the WebAudioApi-RS :

    pub async fn suspend(&self, suspend_time: f64) {

Probably that our generated fn should be async too

fn wire__web_audio_api__context__OfflineAudioContext_suspend_impl(

Steps to reproduce

-

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

fzyzcjy commented 3 days ago

Totally agree! We should detect when seeing async fn and mark it as async no matter how users choose default_dart_async=false.

Feel free to PR, or alternatively I will work on it in the next batch!

Larpoux commented 2 days ago

I am going to look to the frb code. Don’t do anything on this issue until I declare that it is too complicated for me and that I need your help.

fzyzcjy commented 2 days ago

Sure! If you need any hints (e.g. where to look at / how to fix) just ping me.

Larpoux commented 2 days ago

Please, how can I start FRB under debugger ? Can I use Rust Rover for example ? Which project must I open ? Do I have to setup a specific configuration to run the debugger ?

fzyzcjy commented 2 days ago

frb code generator (frb_codegen folder) is nothing but a standard Rust program. Thus yes, you can use arbitrary IDE you like (e.g. rustrover) and follow their guides to start debuggers.

fzyzcjy commented 2 days ago

You may need to add several command line flags: generate --config-file /path/to/your/flutter_rust_bridge/frb_example/integrate_third_party/flutter_rust_bridge.yaml (IIRC that may work)

Larpoux commented 2 days ago

I am going to look to the frb code. Don’t do anything on this issue until I declare that it is too complicated for me and that I need your help.

Sure! If you need any hints (e.g. where to look at / how to fix) just ping me.

I did something to fix this issue, but I had many problems because we diverged. I finally was able to get rid of your modification, but now I realize that your modification is better than mine because I don’t handle correctly the case of a function which is not async, the default value is async, but the user specified #[frb(sync)]. I am going to restore your own patch tomorrow, but please, let me alone on this issue. Or tell me explicitly that I must not work any more on this issue.

Larpoux commented 2 days ago

I am going to restore your own patch tomorrow, but please, let me alone on this issue. Or tell me explicitly that I must not work any more on this issue.

This is stupid. Better for me to cancel my PR.

fzyzcjy commented 2 days ago

I guess you meant https://github.com/fzyzcjy/flutter_rust_bridge/pull/2181? No it is unrelated to this issue!

Firstly it was started (i.e. the first code commit time) before you mentioned wanting to work on this issue. Secondly it was done to fix the scenario that someone chooses default_dart_async: false and then want to make a fn (note: this isssue is about async fn) async on Dart side. Thus it is parallal to this issue.

In other words, there are 3 scenarios when default_dart_async:false:

Larpoux commented 1 day ago

No problem, @fzyzcjy . I was very glad to have a preview of the frb code. I am going to check if [#2178] is solved by your commit. If not, I will reopen the issue and will redo a fix. Certainly today.

‘Thanks for your tremendous work. 👍 . Btw : Is it you the guy who created this amazing project ? I am really impressed by how sophisticated this project is. I hope to have some opportunities to work on it, because I can learn much things by working with the FRB team.

fzyzcjy commented 1 day ago

I am going to check if [...] is solved by your commit.

I guess not solved since I do not touch anything related to async fn signature parsing.

Btw : Is it you the guy who created this amazing project ?

Yes - you can have a look at the commit history. But not only me - there are many people who contribute to this project (view https://github.com/fzyzcjy/flutter_rust_bridge#-acknowledgments-and-contributors for a full list and details). Many of them contribute something small like fixing a typo, but there are some people who contribute a lot to the project, making it grow from a tiny project into a much bigger one. On the other hand, v2's overhaul and new features are mostly done by me (again check the list/commit history for details).

Larpoux commented 1 day ago

Fixed by PR [#2187]. (I hope)