@@ -118,7 +118,8 @@ These types are _generally_ found in struct fields, but they may be found elsewh
118
118
119
119
## ` Cell<T> `
120
120
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.
122
123
Since the compiler knows that all the data owned by the contained value is on the stack, there's
123
124
no worry of leaking any data behind references (or worse!) by simply replacing the data.
124
125
@@ -160,24 +161,25 @@ This relaxes the “no aliasing with mutability” restriction in places
160
161
unnecessary. However, this also relaxes the guarantees that the restriction provides; so if your
161
162
invariants depend on data stored within ` Cell ` , you should be careful.
162
163
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
164
165
doing it in line with the static rules of ` & ` and ` &mut ` .
165
166
166
167
` Cell ` does not let you obtain interior references to the data, which makes it safe to freely
167
168
mutate.
168
169
169
170
#### Cost
170
171
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
172
173
structs, it might be worthwhile to instead wrap individual fields in ` Cell<T> ` since each write is
173
174
otherwise a full copy of the struct.
174
175
175
176
176
177
## ` RefCell<T> `
177
178
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.
179
181
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
181
183
like a single-threaded mutex), unlike ` &T ` /` &mut T ` which do so at compile time. This is done by the
182
184
` borrow() ` and ` borrow_mut() ` functions, which modify an internal reference count and return smart
183
185
pointers which can be dereferenced immutably and mutably respectively. The refcount is restored when
0 commit comments