Skip to content

Commit 8b947a3

Browse files
committed
Update Cell references in the book
1 parent daa5091 commit 8b947a3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/doc/book/choosing-your-guarantees.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ These types are _generally_ found in struct fields, but they may be found elsewh
118118

119119
## `Cell<T>`
120120

121-
[`Cell<T>`][cell] is a type that provides zero-cost interior mutability, but only for `Copy` types.
121+
[`Cell<T>`][cell] is a type that provides zero-cost interior mutability by moving data in and
122+
out of the cell.
122123
Since the compiler knows that all the data owned by the contained value is on the stack, there's
123124
no worry of leaking any data behind references (or worse!) by simply replacing the data.
124125

@@ -160,24 +161,25 @@ This relaxes the &ldquo;no aliasing with mutability&rdquo; restriction in places
160161
unnecessary. However, this also relaxes the guarantees that the restriction provides; so if your
161162
invariants depend on data stored within `Cell`, you should be careful.
162163

163-
This is useful for mutating primitives and other `Copy` types when there is no easy way of
164+
This is useful for mutating primitives and other types when there is no easy way of
164165
doing it in line with the static rules of `&` and `&mut`.
165166

166167
`Cell` does not let you obtain interior references to the data, which makes it safe to freely
167168
mutate.
168169

169170
#### Cost
170171

171-
There is no runtime cost to using `Cell<T>`, however if you are using it to wrap larger (`Copy`)
172+
There is no runtime cost to using `Cell<T>`, however if you are using it to wrap larger
172173
structs, it might be worthwhile to instead wrap individual fields in `Cell<T>` since each write is
173174
otherwise a full copy of the struct.
174175

175176

176177
## `RefCell<T>`
177178

178-
[`RefCell<T>`][refcell] also provides interior mutability, but isn't restricted to `Copy` types.
179+
[`RefCell<T>`][refcell] also provides interior mutability, but doesn't move data in and out of the
180+
cell.
179181

180-
Instead, it has a runtime cost. `RefCell<T>` enforces the read-write lock pattern at runtime (it's
182+
However, it has a runtime cost. `RefCell<T>` enforces the read-write lock pattern at runtime (it's
181183
like a single-threaded mutex), unlike `&T`/`&mut T` which do so at compile time. This is done by the
182184
`borrow()` and `borrow_mut()` functions, which modify an internal reference count and return smart
183185
pointers which can be dereferenced immutably and mutably respectively. The refcount is restored when

0 commit comments

Comments
 (0)