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

Setting a default for Duration type in proc macros #2408

Open
dnaka91 opened this issue Jan 18, 2025 · 2 comments
Open

Setting a default for Duration type in proc macros #2408

dnaka91 opened this issue Jan 18, 2025 · 2 comments

Comments

@dnaka91
Copy link

dnaka91 commented Jan 18, 2025

When deriving a uniffi::Record I have a few fields of type Duration and was hoping to give them a default value. However it seems at the moment the proc macros don't accept literals for it, despite durations being somewhat primitive types from the Built-in types table.

It would be great if the proc macros can be extended to accept expressions for Duration.

I only use Uniffi for Kotlin code, where there should be equivalents to construct the default value in a data class but I'm not sure if this is possible in other languages as well.

For Duration I'd imagine accepting the const constructors:

  • Duration::from_secs
  • Duration::from_millis
  • Duration::from_micros

And eventually:

  • Duration::from_secs_f32
  • Duration::from_secs_f64

For example:

#[uniffi::Record]
struct Config {
    #[uniffi(default = Duration::from_secs(5))]
    max_wait: Duration,
}
@mhammond
Copy link
Member

The defaults are needed by the bindings and currently need to conform to

pub enum LiteralMetadata {
. tbh though, I think we should consider deprecating these types in favour of custom types - I'm not sure what the default support is for them though

@dnaka91
Copy link
Author

dnaka91 commented Jan 23, 2025

Thanks, then I'll better not try to implement support for a default value for those.

IIRC there is currently no support for default values when using custom types. But it's really just a nice to have for me, and I can live with this not having a default value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants