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 300 forks source link

Support combination of Option + borrowed type + opaque type #2262

Closed Tienisto closed 2 weeks ago

Tienisto commented 2 months ago

Describe the bug

I followed the docs in https://cjycode.com/flutter_rust_bridge/guides/types/arbitrary/rust-auto-opaque/overview and created an optional argument:

#[frb(opaque)]
pub struct MyOpaqueType {
    message: String,
}

pub fn create_opaque(message: String) -> MyOpaqueType {
    MyOpaqueType { message }
}

pub async fn use_opaque(object: Option<&MyOpaqueType>) -> String {
    format!("Message: {}", object.unwrap().message)
}

However, the generated code doesn't compile in Rust:

error[E0308]: mismatched types
   --> src/frb_generated.rs:149:80
    |
149 | ...                   Result::<_, ()>::Ok(crate::api::simple::use_opaque(api_object).await)?;
    |                                           ------------------------------ ^^^^^^^^^^ expected `Option<&MyOpaqueType>`, found `Option<RustOpaqueBase<..., ...>>`
    |                                           |
    |                                           arguments to this function are incorrect
    |
    = note: expected enum `Option<&simple::MyOpaqueType>`
               found enum `Option<RustOpaqueBase<RustAutoOpaqueInner<simple::MyOpaqueType>, frb_generated::MoiArc<RustAutoOpaqueInner<simple::MyOpaqueType>>>>`

In a real world example where I came across this issue, I've found a workaround by writing a method for the struct that calls the actual function: https://github.com/Tienisto/rhttp/commit/efec498ae0342d2540b1c1959bb0b2f9a02f2785

Steps to reproduce

  1. Create opaque type
  2. Add function that optionally borrows the opaque type
  3. See compile error

Logs

Not needed

Expected behavior

No response

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

2.3.0

Flutter info

No response

Version of clang++

No response

Additional context

I've created https://github.com/Tienisto/frb-optional-opaque-argument to reproduce the error but the example above should also work

fzyzcjy commented 2 months ago

Yes, looks like this combination of features is not supported yet (feel free to PR! alternatively I may implement this feature later). But the following are supported:

Anyway, &MyOpaqueType is just syntax sugar of "RustAutoOpaque<MyOpaqueType> + (fancily) borrow it", so I guess using Option<RustAutoOpaque<MyOpaqueType>> may not be a big problem.

stale[bot] commented 3 weeks ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 3 days ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.