Hi,
LocalKey has two fundamental limitations:
-
with
acceptsFnOnce
-
with
provides inner value as&T
instead of&mut T
. RequiresRefCell
to 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&'static
reference leakage into other thread. But why not mutable?
Thanks
ANSWER
Sigh. I still need to learn a lot.
I'll answer to my claims myself
-
FnOnce
is pretty much enough, it can access outer context nicely and without trouble - since it's not'static
- As fellow @gkoz pointed out, we need explicit
RefCell
to guard against nested calls - which can violate "no mutable aliasing" rule.