Interior mutability patterns
pitdicker.github.io/Interior-mutability-patte...Posted Dec 28, 2019, 3:45 PM UTC
Posted Dec 28, 2019, 3:45 PM UTC
If you know any other patterns to use interior mutability safely, please let me know ;-)
The section on Cell could use a mention of RFC 1789, which allows indexing operations on Cell<[T]>s to produce places of type Cell<T> or [Cell<T>].
The same thing can be done for structs and their fields, allowing projections from Cell<Struct> to Cell<Field>. There's no way to express this generically in today's type system, the way you can for indexing, but you can do it yourself with unsafe in an ad-hoc way.
Thank you for the suggestion. Maybe creative methods like these deserve a special post, RefMut::map_split is another one. Maybe I'll find the time...
Interior mutability may be accessed through raw pointers too. This is needed to make memory-mapped IO and FFI work correctly.
Pure Rust programs should use UnsafeCell in almost all situations (for clarity if nothing else) but it's not always required.
Nice read! Two minor findings : downgrade(&self) should be downgrade(self), and rw2 and rw3 have TCell parameters, should these not be QCells?
Thank you for the close read!
Thanks for reminding me that OnceCell supports a Lazy<T>, I wasted some time trying to hand-implement that recently.
Thanks, this was a nice overview.