LocalKey has two fundamental limitations:
withprovides inner value as
&mut T. Requires
RefCellto have normal mutable value.
Both limit interaction of thread-local value and the scope which references it.
Since value is thread-local, and lives nicely while thread is alive, those restrictions look a bit strange to me. Scoping access to function is perfectly fine, to avoid
&'staticreference leakage into other thread. But why not mutable?
Sigh. I still need to learn a lot.
I’ll answer to my claims myself
FnOnceis pretty much enough, it can access outer context nicely and without trouble - since it’s not
- As fellow @gkoz pointed out, we need explicit
RefCellto guard against nested calls - which can violate “no mutable aliasing” rule.