Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrackedStruct derive enums #584

Open
nikomatsakis opened this issue Sep 29, 2024 · 0 comments
Open

TrackedStruct derive enums #584

nikomatsakis opened this issue Sep 29, 2024 · 0 comments
Labels
bikeshed 🚴‍♀️ Debating API details and the like rfc Active discussion about a possible future feature
Milestone

Comments

@nikomatsakis
Copy link
Member

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(...)).

@nikomatsakis nikomatsakis added rfc Active discussion about a possible future feature bikeshed 🚴‍♀️ Debating API details and the like labels Sep 29, 2024
@nikomatsakis nikomatsakis added this to the Salsa 3.0 milestone Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bikeshed 🚴‍♀️ Debating API details and the like rfc Active discussion about a possible future feature
Projects
None yet
Development

No branches or pull requests

1 participant