You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
thread_local!{staticX:String = String::from("abc");}/// SAFETY: If this function is called in a certain thread,/// then the returned reference must not be used after/// the "top-level function" of that thread finishes.unsafefnsmuggle() -> &'staticString{&*X.with(|a| a as*constString)}
In other words, is it guaranteed that:
A thread-local's address is stable?
&mut references won't be created to thread-locals until it's time to run thread-local destructors?
The text was updated successfully, but these errors were encountered:
There is no way to get a &mut to thread-locals in safe code. And the address is indeed stable.
However once the destructor of the thread-local gets run, the reference gets invalidated, so the 'static lifetime is a lie. You seem to be aware of that.
The notion of "soundness" of an unsafe function is non-trivial to define so it's not entirely clear what you are asking about.
The answers to your questions are currently yes and yes. Als Ralf pointed out, this isn't documented anywhere though, so it isn't something you should rely on (yet). On the other hand, I don't consider this behaviour likely to change. Thus, if you truly need this, please file a PR adjusting the thread_local! documentation to add the necessary guarantees, and T-libs-api might consider adding them.
Is the following code sound?
In other words, is it guaranteed that:
The text was updated successfully, but these errors were encountered: