-
Notifications
You must be signed in to change notification settings - Fork 155
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
+161
−8
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use super::{Configuration, IngredientImpl}; | ||
use crate::plumbing::Array; | ||
use crate::runtime::Stamp; | ||
use crate::{Durability, Revision}; | ||
|
||
pub struct BuilderImpl<'builder, C> | ||
where | ||
C: Configuration, | ||
{ | ||
stamps: C::Stamps, | ||
|
||
ingredient: &'builder IngredientImpl<C>, | ||
} | ||
|
||
impl<'builder, const N: usize, C> BuilderImpl<'builder, C> | ||
where | ||
C: Configuration<Stamps = Array<Stamp, N>>, | ||
{ | ||
pub fn new(revision: Revision, ingredient: &'builder IngredientImpl<C>) -> Self { | ||
Self { | ||
ingredient, | ||
stamps: Array::new([crate::plumbing::stamp(revision, Durability::default()); N]), | ||
} | ||
} | ||
|
||
/// Sets the durability of a specific field. | ||
pub fn set_field_durability(&mut self, index: usize, durability: Durability) -> &mut Self { | ||
self.stamps[index].durability = durability; | ||
self | ||
} | ||
|
||
pub fn durability(&mut self, durability: Durability) { | ||
for stamp in &mut *self.stamps { | ||
stamp.durability = durability; | ||
} | ||
} | ||
|
||
pub fn build(self, fields: C::Fields) -> C::Struct { | ||
self.ingredient.new_input(fields, self.stamps) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//! Test that a `tracked` fn on a `salsa::input` | ||
//! compiles and executes successfully. | ||
#![allow(warnings)] | ||
|
||
use expect_test::expect; | ||
|
||
use common::{HasLogger, Logger}; | ||
use salsa::plumbing::HasStorage; | ||
use salsa::{Database, Durability, Event, EventKind, Setter}; | ||
|
||
mod common; | ||
#[salsa::input] | ||
struct MyInput { | ||
field: u32, | ||
} | ||
|
||
#[salsa::tracked] | ||
fn tracked_fn(db: &dyn salsa::Database, input: MyInput) -> u32 { | ||
input.field(db) * 2 | ||
} | ||
|
||
#[test] | ||
fn execute() { | ||
#[salsa::db] | ||
#[derive(Default)] | ||
struct Database { | ||
storage: salsa::Storage<Self>, | ||
logger: Logger, | ||
} | ||
|
||
#[salsa::db] | ||
impl salsa::Database for Database { | ||
fn salsa_event(&self, event: Event) { | ||
match event.kind { | ||
EventKind::WillCheckCancellation => {} | ||
_ => { | ||
self.push_log(format!("salsa_event({:?})", event.kind)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl HasLogger for Database { | ||
fn logger(&self) -> &Logger { | ||
&self.logger | ||
} | ||
} | ||
|
||
let mut db = Database::default(); | ||
let input_low = MyInput::new(&db, 22); | ||
let input_high = MyInput::builder(&db).durability(Durability::HIGH).new(2200); | ||
|
||
assert_eq!(tracked_fn(&db, input_low), 44); | ||
assert_eq!(tracked_fn(&db, input_high), 4400); | ||
|
||
db.assert_logs(expect![[r#" | ||
[ | ||
"salsa_event(WillExecute { database_key: tracked_fn(0) })", | ||
"salsa_event(WillExecute { database_key: tracked_fn(1) })", | ||
]"#]]); | ||
|
||
db.synthetic_write(Durability::LOW); | ||
|
||
assert_eq!(tracked_fn(&db, input_low), 44); | ||
assert_eq!(tracked_fn(&db, input_high), 4400); | ||
|
||
// There's currently no good way to verify whether an input was validated using shallow or deep comparison. | ||
// All we can do for now is verify that the values were validated. | ||
// Note: It maybe confusing why it validates `input_high` when the write has `Durability::LOW`. | ||
// This is because all values must be validated whenever a write occurs. It doesn't mean that it | ||
// executed the query. | ||
db.assert_logs(expect![[r#" | ||
[ | ||
"salsa_event(DidValidateMemoizedValue { database_key: tracked_fn(0) })", | ||
"salsa_event(DidValidateMemoizedValue { database_key: tracked_fn(1) })", | ||
]"#]]); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
See #534