Pre-RFC: sum-enums

I think this is a very interesting idea and addresses many current pain points with enums, especially the problem where the variants don’t have a first-class type.

In regard to that, one thing I would find helpful is the ability to derive traits for “sum-enums” so long as that trait is implemented by all of the variants, e.g.:

struct Foobar { ... }
impl MyTrait for Foobar { ... }

struct Baz { ... }
impl MyTrait for Baz { ... }

#[derive(MyTrait)]
enum Quux(Foobar, Baz)

derive_utils probably can help that.

1 Like

Wouldn't that render the jumptable optimization impossible?


Does this only refer to object-safe traits? I can not imagine how this would be possible for traits with associated types that are used anywhere but return position.

trait Foo {
    type Input;
    fn construct(input: Input) -> Self;
}

But also prominently, the std::str::FromString. Implementing this, even under the restriction that the Self::Input of all implementations is the same, would require choosing one canonical type whose implementation is chosen.

1 Like

I think you are correct, yet another reason to not go that path.

Yes, I should've mentioned it.

i guess it could be relaxed? for eg. traits with generic functions (my main usecase for sum-enums).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.