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

Introduce Input::builder API #535

Closed
wants to merge 2 commits into from
Closed

Introduce Input::builder API #535

wants to merge 2 commits into from

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Jul 26, 2024

This PR introduces a new Input::builder API.

let input_high = MyInput::builder(&db).durability(Durability::HIGH).new(2200);

It's intentionally limited for now but we could extend it in the future to e.g. allow setting per-field durability.

Design

I'm not super happy with the implementation. Mainly because it requires a private module to hide most implementation details of the Builder.
But even then, we don't get perfect encapsulation because the Builder::create method is still visible in the input's module. But I'm not sure what else we could do.

I had to revert implementing Configuration directly on the $Struct because self-referential types then stopped compiling. Example:

#[salsa::input]
struct List {
value: u32,
next: Option<List>,
}

What I dislike about the current solution is that Builder::inner is accessible from the input's module. But not sure what we should do about it. I tried using a trait but that's a bit annoying to use because it must be explicitly imported from the ingredient's scope.

Tests

I did add a test but I haven't figured out a way to automatically verify if a tracked function was validated using deep or shallow comparison.
I did do some manual testing by adding dbg statements that show that Salsa does use the shallow comparison for inputs with Durability::HIGH.

@MichaReiser MichaReiser requested a review from nikomatsakis July 26, 2024 15:16
Copy link

netlify bot commented Jul 26, 2024

Deploy Preview for salsa-rs canceled.

Name Link
🔨 Latest commit 2104b0f
🔍 Latest deploy log https://app.netlify.com/sites/salsa-rs/deploys/66a3c06030e8fa00084eccfd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #534

Copy link
Member

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a setup like this one?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=11175c08bb02381f293d7558dcdae52f

The idea is to have a trait Builder { type Builder; } in Salsa and then to create a module inside the const. You can still name the builder type then by doing <Self as Builder>::Builder. One thing I like about this is that it also means others can name the builder type if they want to.

@MichaReiser
Copy link
Contributor Author

That sounds interesting. I'll give it a try

@MichaReiser
Copy link
Contributor Author

I'll re-open this PR from my own fork.

@MichaReiser MichaReiser deleted the new-input-builder branch July 29, 2024 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants