Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tienisto opened this issue Aug 28, 2024 · 1 comment
Open

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

Tienisto opened this issue Aug 28, 2024 · 1 comment
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc enhancement New feature or request

Comments

@Tienisto
Copy link
Contributor

Tienisto commented Aug 28, 2024

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: Tienisto/rhttp@efec498

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

@Tienisto Tienisto added the bug Something isn't working label Aug 28, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented Aug 29, 2024

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:

  • Option<MyOpaqueType>
  • Option<RustAutoOpaque<MyOpaqueType>>
  • Option<NonOpaqueType> (which does not need to have & since non-opaque types are always copied)

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.

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Aug 29, 2024
@fzyzcjy fzyzcjy changed the title Option<&MyOpaqueType> doesn't compile Support combination of Option + borrowed type + opaque type Aug 29, 2024
@fzyzcjy fzyzcjy added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants