Skip to content

Display trait not working for Kotlin errors + simple enums #2699

@thunderbiscuit

Description

@thunderbiscuit

After reading the changelog for the 0.30.0 release I tried to clean up a long standing issue of ours in our uniffi codebase, errors. Enabling the Display trait is a great feature because we can then expose both the fields important to the errors as well as provide a clear message back to the developers reading these errors/exceptions (we previously had to choose between having the field or having the relevant message).

However there appears to be a bug on the Kotlin side for exporting Display. I'm opening this issue to report it.

Consider the following 3 examples:

#[derive(uniffi::Error, Debug)]
#[uniffi::export(Display)]
pub enum Example1Enum {
    Blue { id: u32 },
    Green { issue: String },
}

impl Display for Example1Enum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Display for Example1Enum {:?}", self)
    }
}

#[derive(uniffi::Enum, Debug)]
#[uniffi::export(Display)]
pub enum Example2Enum {
    Blue,
    Green,
}

impl Display for Example2Enum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Display for Example2Enum {:?}", self)
    }
}

#[derive(uniffi::Enum, Debug)]
#[uniffi::export(Display)]
pub enum Example3Enum {
    Blue { id: u32 },
    Green { issue: String },
}

impl Display for Example3Enum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Display for Example3Enum {:?}", self)
    }
}

All three enums will produce the description field in Swift with the comment // The local Rust Display implementation.

Only the 3rd example, which is expressed as a Sealed Class in Kotlin, will get a toString() method implemented. The uniffi::Error and the simple Kotlin enum will not.

Note that in Python it's also a bit different than both Kotlin and Swift:

  • Example1Enum (the error) gets a __repr__ method but no __str__
  • Example2Enum (the straight enum) gets nothing (like Kotlin)
  • Example3Enum gets a __str__ method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions