uniffi-rs: Unable to conditionally enable uniffi::constructor macro

I have a simple class that im trying to expose with proc macros like below:

#[derive(uniffi::Object)]
pub struct Client {
    api_endpoint: String,
}

#[uniffi::export]
impl Client {
    #[uniffi::constructor]
    pub fn new(api_endpoint: String) -> Self {
        Self { api_endpoint }
    }
}

my problem is that i only want uniffi scaffolding if my target is compiling for mobile - not wasm. In the past I have used conditional compilation for this with a lot of success. But when i try the following:

#[cfg_attr(not(target_family = "wasm"), derive(uniffi::Object))]
pub struct Client {
    api_endpoint: String,
}

#[cfg_attr(not(target_family = "wasm"), uniffi::export)]
impl Client {
    #[cfg_attr(not(target_family = "wasm"), uniffi::constructor)]
    pub fn new(api_endpoint: String) -> Self {
        Self { api_endpoint }
    }
}

I get the error associated functions are not currently supported:

error: associated functions are not currently supported
  --> <redacted>/lib.rs:32:9
   |
32 |     pub fn new(api_endpoint: String) -> Self {

For now I’m just putting a #[cfg(not(target_family = "wasm"))] on the whole block as a workaround, but i think this could be a bug in one of the macros

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 29 (18 by maintainers)

Commits related to this issue

Most upvoted comments

Now that the PR is merged, the public docs are updated, and this is working in production, we can close this issue. Someone arriving here from the link in the docs will assume this is unsolved at first glance.

Hey guys, sorry for my delay. I have moved to other issues and postponed the fix in my original PR, but #2113 represents exactly my original intention, so I closed my draft PR in favor of it.

I’m against introducing new syntax to deal with this problem. This would create the need to build a special parser and document the new behavior. If we keep our API intact and play along with the rest of the ecosystem, there’s no need to document it. It’d favor the principle of least astonishment.

PS: I left a comment in the PR. Let’s continue the discussion there.