uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

Setting a default for Duration type in proc macros

Open dnaka91 opened this issue 9 months ago • 2 comments

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,
}

dnaka91 avatar Jan 18 '25 12:01 dnaka91