Skip to content

Suggest an enum as a possible option when a trait object can’t be created due to object safety #80194

Closed
@alilleybrinker

Description

When you can't create a trait object due to a trait not being object safe, you have a few options:

  • if you control the trait, try to make it object safe (medium difficulty)
  • if you don't control the trait, try to get the upstream owner to make it object safe or find an alternative (hard)
  • if the set of types you're trying to trait-objectify is closed, use an enum (easy)

Unfortunately, the error message for a trait not being object safe doesn't point you in the direction of the enum option, and unless you're pretty familiar with Rust you may not consider it.

Adjusting the error message for failure to create a trait object due to object safety issues to mention an enum as a possibility could help people resolve these issues. I have no data, but in my experience the enum path is often workable because I'm working with a finite set of variants that I know in advance.

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions