-
Notifications
You must be signed in to change notification settings - Fork 146
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
#543
Conversation
✅ Deploy Preview for salsa-rs canceled.
|
CodSpeed Performance ReportMerging #543 will not alter performanceComparing Summary
|
12572b2
to
8e646ec
Compare
8e646ec
to
f2245c0
Compare
Self::builder($($required_field_id,)*).new(db) | ||
} | ||
|
||
pub fn builder($($required_field_id: $required_field_ty),*) -> <Self as $zalsa_struct::HasBuilder>::Builder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we could avoid the extra required_fields
macro argument but I couldn't figure out how to use macro_if
in a function declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, you'd have to wrap the whole function. seems fine this way.
79a4128
to
d5ca1c7
Compare
7aa6e9d
to
805760d
Compare
I plan to rebase this PR tomorrow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I'd be happy to merge.
Self::builder($($required_field_id,)*).new(db) | ||
} | ||
|
||
pub fn builder($($required_field_id: $required_field_ty),*) -> <Self as $zalsa_struct::HasBuilder>::Builder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, you'd have to wrap the whole function. seems fine this way.
766341f
to
fbc7b8c
Compare
This PR introduces a new
Input::builder
API.It also adds support for the new input-field-attribute
#[default]
. It specifies that the value should be initialized with$ty::default()
unless it is specified when using thebuilder
API. Optional fields don't need to be specified when callingInput::new
. I intentionally kept the default attribute simple for now. We can add support for calling a function, or initializing with a const value in the future.One downside of the macro-generated builder is that at least RustRover (IntelliJ) completely breaks down and fails to provide any auto-completion. I yet have to try if rust analyzer does any better.
Edit: Ra does better! It does suggest the different builder methods, except
new
.new
doesn't show up for some reason.Fixes #385
Fixes #476 (not in its entirety but a start)
Design
This API inverts the original design where
db
was passed tobuilder
and the required fields tonew
. There are a few reasons for this:db
innew
requires that thebuilder
stores a reference toIngredientImpl<C>
.Configuration
isn't referenceable in thebuilder
module. That means, the generatedBuilder
would need to be generic overConfiguration
(or use some other trickery to avoid namingConfiguration
builder(required_fields).optional(some_query(db)).new(&mut db)
without having to store theoptional
value in a temporary variable (once we support optional fields).builder
should simplify implementing support for optional fields where the macro code can generate $fields` using either the default or provided value.Alternative Designs
I'm not too fond of the
*_durability
methods. An alternative could be introducing a#[durability=Durability::HIGH]
field and the builders and setters would respect that durability. The downside of this is that it is less flexible and it is unclear whetherbuilder.durability
should override the durability for those fields (I see reasons for and against it).Why not use type-states?
I thought about it but it felt overly-complicated to me and goes beyond Salsa's scope. If someone wants more advanced builders than using any other builder crate on top of salsa should be possible (create a single function that takes all input values and generate a builder for that function).
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 withDurability::HIGH
.