-
Notifications
You must be signed in to change notification settings - Fork 18
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
add OnceCell to shared-mutability + splitting burrows speaker notes #107
base: main
Are you sure you want to change the base?
Changes from all commits
f7f6cc0
0f021e5
f829e3f
d33f8e6
69771f0
2ed8a60
eab19e2
b97efc1
5757c8d
ef20229
bd1d774
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,6 +179,29 @@ impl Post { | |
} | ||
``` | ||
|
||
Note: | ||
|
||
As an in-depth example of the borrowchecker's limitations, consider the [Splitting Borrows](https://doc.rust-lang.org/nomicon/borrow-splitting.html) idiom, which allows one to borrow different fields of the same struct with different mutability semantics: | ||
|
||
```rust | ||
struct Foo { | ||
a: i32, | ||
b: i32, | ||
c: i32, | ||
} | ||
|
||
let mut x = Foo {a: 0, b: 0, c: 0}; | ||
let a = &mut x.a; | ||
let b = &mut x.b; | ||
let c = &x.c; | ||
*b += 1; | ||
let c2 = &x.c; | ||
*a += 10; | ||
println!("{} {} {} {}", a, b, c, c2); | ||
``` | ||
|
||
The code works, but you *have* to use shadowing with `let a = &mut x.a;` or else the compiler will error. The borrowchecker is particularly frail here - replacing `Foo` with `x = [1,2,3]` and trying to borrow indexes will make it error out. | ||
|
||
## `RefCell` | ||
|
||
A `RefCell` is also safe, but lets you *borrow* or *mutably borrow* the contents. | ||
|
@@ -252,3 +275,35 @@ To get *shared ownership* and *mutability* you need two things: | |
|
||
* `Rc<RefCell<T>>` | ||
* (Multi-threaded programs might use `Arc<Mutex<T>>`) | ||
|
||
## `OnceCell` for special cases | ||
|
||
If you only need to modify a field *once*, a `OnceCell` can help you keep the ownership system checks at compile-time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we link to the std-library docs on |
||
|
||
```rust | ||
use std::time::Instant; | ||
|
||
fn main() { | ||
let post = Post { | ||
content: String::from("Blah"), | ||
..Post::default() | ||
}; | ||
assert!(post.first_viewed_at.get().is_none()); | ||
println!("{:?}", post.date_of_first_view()); | ||
assert!(post.first_viewed_at.get().is_some()); | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
struct Post { | ||
content: String, | ||
first_viewed_at: std::cell::OnceCell<Instant>, | ||
} | ||
|
||
impl Post { | ||
fn date_of_first_view(&self) -> Instant { | ||
*self.first_viewed_at.get_or_init(Instant::now) | ||
} | ||
} | ||
``` | ||
|
||
A [LazyCell](https://doc.rust-lang.org/std/cell/struct.LazyCell.html) will do this initialization lazily, and a [LazyLock](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) will do it in a threadsafe way. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid I didn't understand the difference between OnceCell and LazyCell after reading this note. I also observe that OnceLock exists. The primary difference is that a LazyCell owns its initialisation function - it is passed when the cell is constructed, not when the cell is accessed. See https://doc.rust-lang.org/std/cell/index.html#oncecellt and https://doc.rust-lang.org/std/cell/index.html#lazycellt-f. However both are equally 'lazy' in that the function is not called at construction time - it is only called on first access (either explicitly or implicitly). |
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.
I don't know that "You have to use shadowing" is true here. This code works too:
The main take-away I think we want here is that the borrow checker is special-cased for borrowing fields in structs and tuples, and the magic there does not apply to borrowing elements using the indexing operator, like with arrays or hashmaps:
This also works:
This does not: