Skip to content

Commit 2c7cda5

Browse files
authored
Merge pull request #499 from camelid/input-mut
book: Creating inputs no longer requires `&mut dyn Db`
2 parents 56eb9c2 + 5b99535 commit 2c7cda5

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

book/src/overview.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@ pub struct ProgramFile {
5656
```
5757

5858
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:
6060

6161
```rust
6262
let file: ProgramFile = ProgramFile::new(
63-
&mut db,
63+
&db,
6464
PathBuf::from("some_path.txt"),
6565
String::from("fn foo() { }"),
6666
);
6767
```
6868

69+
Mutable access is not needed since creating a new input cannot affect existing tracked data in the database.
70+
6971
### Salsa structs are just integers
7072

7173
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)
111113
### Writing input fields
112114

113115
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:
115118

116119
```rust
117120
file.set_contents(&mut db).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
140143
Tracked functions have to follow a particular structure:
141144

142145
- 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!
144147
- 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.
145148
- They _can_ take additional arguments, but it's faster and better if they don't.
146149

book/src/plumbing/db_lifetime.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ The reason we use a raw pointer in the struct is because instances of this struc
169169

170170
```rust
171171
let mut db = MyDatabase::default();
172-
let input = MyInput::new(&mut db, ...);
172+
let input = MyInput::new(&db, ...);
173173

174174
// Revision 1:
175175
let result1 = tracked_fn(&db, input);
@@ -235,4 +235,4 @@ However in some later revision R2, how
235235
### Freeing the memory while a tracked struct remains live
236236

237237

238-
### Aliases of a tracked struct
238+
### Aliases of a tracked struct

book/src/tutorial/ir.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ pub struct SourceProgram(salsa::Id);
5252
```
5353

5454
It will also generate a method `new` that lets you create a `SourceProgram` in the database.
55-
For an input, a `&mut db` reference is required, along with the values for each field:
55+
For an input, a `&db` reference is required, along with the values for each field:
5656

5757
```rust
58-
let source = SourceProgram::new(&mut db, "print 11 + 11".to_string());
58+
let source = SourceProgram::new(&db, "print 11 + 11".to_string());
5959
```
6060

6161
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.
9090

9191
Apart from the fields being immutable, the API for working with a tracked struct is quite similar to an input:
9292

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)`
9494
* 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).
9595
* 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.
9696

@@ -123,7 +123,7 @@ This would mean that we have to re-execute those parts of the code that depended
123123

124124
Apart from the fields being immutable, the API for working with a tracked struct is quite similar to an input:
125125

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)`
127127
* 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).
128128

129129
### id fields

0 commit comments

Comments
 (0)