Suggest an enum as a possible option when a trait object can’t be created due to object safety #80194
Closed
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
Labels
Area: Messages for errors, warnings, and lintsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Trait systemCategory: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: An error or lint that needs small tweaks.Relevant to the compiler team, which will review and decide on the PR/issue.