You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/src/overview.md
+7-4
Original file line number
Diff line number
Diff line change
@@ -56,16 +56,18 @@ pub struct ProgramFile {
56
56
```
57
57
58
58
You create an input by using the `new` method.
59
-
Because the values of input fields are stored in the database, you also give an `&mut`-reference to the database:
59
+
Because the values of input fields are stored in the database, you also give an `&`-reference to the database:
60
60
61
61
```rust
62
62
letfile:ProgramFile=ProgramFile::new(
63
-
&mutdb,
63
+
&db,
64
64
PathBuf::from("some_path.txt"),
65
65
String::from("fn foo() { }"),
66
66
);
67
67
```
68
68
69
+
Mutable access is not needed since creating a new input cannot affect existing tracked data in the database.
70
+
69
71
### Salsa structs are just integers
70
72
71
73
The `ProgramFile` struct generated by the `salsa::input` macro doesn't actually store any data. It's just a newtyped integer id:
@@ -111,7 +113,8 @@ file.data(&db)
111
113
### Writing input fields
112
114
113
115
Finally, you can also modify the value of an input field by using the setter method.
114
-
Since this is modifying the input, the setter takes an `&mut`-reference to the database:
116
+
Since this is modifying the input, and potentially invalidating data derived from it,
117
+
the setter takes an `&mut`-reference to the database:
115
118
116
119
```rust
117
120
file.set_contents(&mutdb).to(String::from("fn foo() { /* add a comment */ }"));
@@ -140,7 +143,7 @@ The algorithm Salsa uses to decide when a tracked function needs to be re-execut
140
143
Tracked functions have to follow a particular structure:
141
144
142
145
- They must take a `&`-reference to the database as their first argument.
143
-
- Note that because this is an `&`-reference, it is not possible to create or modify inputs during a tracked function!
146
+
- Note that because this is an `&`-reference, it is not possible to modify inputs during a tracked function!
144
147
- They must take a "Salsa struct" as the second argument -- in our example, this is an input struct, but there are other kinds of Salsa structs we'll describe shortly.
145
148
- They _can_ take additional arguments, but it's faster and better if they don't.
You can read the value of the field with `source.text(&db)`,
@@ -90,7 +90,7 @@ then subsequent parts of the computation won't need to re-execute.
90
90
91
91
Apart from the fields being immutable, the API for working with a tracked struct is quite similar to an input:
92
92
93
-
* You can create a new value by using `new`, but with a tracked struct, you only need an `&dyn` database, not `&mut` (e.g., `Program::new(&db, some_staements)`)
93
+
* You can create a new value by using `new`: e.g., `Program::new(&db, some_statements)`
94
94
* You use a getter to read the value of a field, just like with an input (e.g., `my_func.statements(db)` to read the `statements` field).
95
95
* In this case, the field is tagged as `#[return_ref]`, which means that the getter will return a `&Vec<Statement>`, instead of cloning the vector.
96
96
@@ -123,7 +123,7 @@ This would mean that we have to re-execute those parts of the code that depended
123
123
124
124
Apart from the fields being immutable, the API for working with a tracked struct is quite similar to an input:
125
125
126
-
* You can create a new value by using `new`, but with a tracked struct, you only need an `&dyn` database, not `&mut` (e.g., `Function::new(&db, some_name, some_args, some_body)`)
126
+
* You can create a new value by using `new`: e.g., `Function::new(&db, some_name, some_args, some_body)`
127
127
* You use a getter to read the value of a field, just like with an input (e.g., `my_func.args(db)` to read the `args` field).
0 commit comments