Skip to content

Commit

Permalink
updates from review
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Jan 16, 2024
1 parent eae282d commit e73cc0f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/borrowing/interior-mutability.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and wish to update that cache from read-only methods.

The "interior mutability" pattern allows exclusive (mutable) access behind a
shared reference. The standard library provides several ways to do this, all
ensuring safety by performing a runtime check.
while still ensuring safety, typically by performing a runtime check.

## `RefCell`

Expand Down Expand Up @@ -55,6 +55,10 @@ value. Since there are no references, borrowing rules cannot be broken.

<details>

The main thing to take away from this slide is that Rust provides _safe_ ways to
modify data behind a shared reference. There are a variety of ways to ensure
that safety, and `RefCell` and `Cell` are two of them.

- `RefCell` enforces Rust's usual borrowing rules (either multiple shared
references or a single exclusive reference) with a runtime check. In this
case, all borrows are very short and never overlap, so the checks always
Expand All @@ -64,8 +68,12 @@ value. Since there are no references, borrowing rules cannot be broken.
is to allow (and count) many references. But we want to modify the value, so
we need interior mutability.

- `Cell` is a simpler means to ensure safety: it has a `set` method that takes
`&self`. This needs no runtime check, but requires moving values, which can
have its own cost.

- Demonstrate that reference loops can be created by adding `root` to
`subtree.children` (don't try to print it!).
`subtree.children`.

- To demonstrate a runtime panic, add a `fn inc(&mut self)` that increments
`self.value` and calls the same method on its children. This will panic in the
Expand Down

0 comments on commit e73cc0f

Please sign in to comment.