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

RustOpaqueInterface on Enums #2498

Open
quetool opened this issue Jan 15, 2025 · 4 comments
Open

RustOpaqueInterface on Enums #2498

quetool opened this issue Jan 15, 2025 · 4 comments
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc enhancement New feature or request

Comments

@quetool
Copy link

quetool commented Jan 15, 2025

Hello! I have the following rust code inside a crate:

use {
    relay_rpc::domain::ProjectId,
    serde::{Deserialize, Serialize},
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[cfg_attr(feature = "frb", frb)]
#[serde(rename_all = "camelCase")]
pub struct StatusResponsePending {
    pub created_at: u64,
    /// Polling interval in ms for the client
    pub check_in: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[cfg_attr(feature = "frb", frb)]
#[serde(rename_all = "camelCase")]
pub struct StatusResponseCompleted {
    pub created_at: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[cfg_attr(feature = "frb", frb)]
#[serde(rename_all = "camelCase")]
pub struct StatusResponseError {
    pub created_at: u64,
    pub error: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Enum))]
#[cfg_attr(feature = "frb", frb)]
#[serde(rename_all = "UPPERCASE", tag = "status")]
pub enum StatusResponse {
    Pending(StatusResponsePending),
    Completed(StatusResponseCompleted),
    Error(StatusResponseError),
}

And I'm depending from this crate to create a fluter package, but not matter what I do, when I generate the code I get these enums translated to Rust opaque trypes

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<StatusResponse>>
abstract class StatusResponse implements RustOpaqueInterface {}

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<StatusResponseCompleted>>
abstract class StatusResponseCompleted implements RustOpaqueInterface {}

Can you please advise on how to overcome this?

@quetool quetool added the enhancement New feature or request label Jan 15, 2025
Copy link

welcome bot commented Jan 15, 2025

Hi! Thanks for opening your first issue here! 😄

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 15, 2025

Hi, could you please try #[frb(non_opaque)]? Also maybe try to make a minimal reproducible sample to see which part is going wrong.

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Jan 15, 2025
@quetool
Copy link
Author

quetool commented Jan 16, 2025

Hello, thanks for the reply!
So...
This is our Cargo.toml file (part of it, not the whole) for the first crate, which is called yttrium:

[features]
full = ["frb"]
frb = ["dep:flutter_rust_bridge_codegen", "dep:flutter_rust_bridge"]

[build-dependencies]
flutter_rust_bridge_codegen = { workspace = true, optional = true }

[dependencies]
flutter_rust_bridge = { workspace = true, optional = true }

Then we have this Cargo.toml file (again, hot the whole) on the second crate which is also a flutter package:

[features]
frb_expand = []

[build-dependencies]
flutter_rust_bridge_codegen.workspace = true

[dependencies]
yttrium = { path = "../../yttrium" }
flutter_rust_bridge.workspace = true

So, in the previous rust file I shared, which is inside yttrium crate, I added the macro as this:
#[cfg_attr(feature = "frb", frb(non_opaque))]

So this is the resulted file:

use {
    relay_rpc::domain::ProjectId,
    serde::{Deserialize, Serialize},
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[cfg_attr(feature = "frb", frb(non_opaque))] // <--- frb macro
#[serde(rename_all = "camelCase")]
pub struct StatusResponsePending {
    pub created_at: u64,
    /// Polling interval in ms for the client
    pub check_in: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[cfg_attr(feature = "frb", frb(non_opaque))] // <--- frb macro
#[serde(rename_all = "camelCase")]
pub struct StatusResponseCompleted {
    pub created_at: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Record))]
#[cfg_attr(feature = "frb", frb(non_opaque))] // <--- frb macro
#[serde(rename_all = "camelCase")]
pub struct StatusResponseError {
    pub created_at: u64,
    pub error: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi_macros::Enum))]
#[cfg_attr(feature = "frb", frb(non_opaque))] // <--- frb macro
#[serde(rename_all = "UPPERCASE", tag = "status")]
pub enum StatusResponse {
    Pending(StatusResponsePending),
    Completed(StatusResponseCompleted),
    Error(StatusResponseError),
}

But still after running flutter_rust_bridge_codegen generate --config-file flutter_rust_bridge.yaml everything gets translated as RustOpaqueInterface

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 16, 2025

Oh I see. It would be great if things are kept in a single crate. When using multi crates, i.e. some structs are "third party structs", you may need https://cjycode.com/flutter_rust_bridge/guides/third-party

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

3 participants
@fzyzcjy @quetool and others