Skip to content

TrackedStruct derive enums #584

Closed
Closed
@nikomatsakis

Description

@nikomatsakis

I sometimes find myself declaring tracked structs that effectively just wrap an enum:

#[salsa::tracked]
struct Foo<'db> {
    kind: FooKind<'db>,
}

#[derive(Clone, Update, Debug, ...the usual suspects...)]
enum FooKind<'db> {
    Variant1(...),
}

I wonder if it'd be nice to make a salsa::TrackedStruct derive for this pattern? I am imagining that it generates a tracked struct as above, with additional methods named after each variant that return an Option<(...)> with the data from the variant (the equivalent of match x.kind(db) { Variant1(...) => Some((...)), _ => None }.

The name of the tracked struct would by default be taken from the enum -- we would split the last word (Kind) and the other words (Foo, in this case) and name the struct after the first part and the field after the second (kind). This would be overridable with an attribute.

We would (could?) also add an inherent new method to the enum to permit FooKind(...).new(db) instead of Foo::new(db, FooKind(...)). Not sure about that.

So something like:

#[derive(Clone, salsa::TrackedStruct, Debug)]
#[tracked_struct(name = Foo, accessor = kind)] // explicitly specify what would be the defaults
enum FooKind<'db> {
    Variant1(...),
}

Then you could do Foo::new(db, FooKind::Variant1(...)).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bikeshed 🚴‍♀️Debating API details and the likerfcActive discussion about a possible future feature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions