pub struct StaticInstancePerThreadSync<T>{ /* private fields */ }Expand description
This is the real type of variables wrapped in the linked::thread_local_arc! macro.
See macro documentation for more details.
Instances of this type are created by the linked::thread_local_arc! macro,
never directly by user code, which can call .with() or .to_arc()
to work with or obtain a linked instance of T.
Implementations§
Source§impl<T> StaticInstancePerThreadSync<T>
impl<T> StaticInstancePerThreadSync<T>
Sourcepub fn with<F, R>(&self, f: F) -> R
pub fn with<F, R>(&self, f: F) -> R
Executes a closure with the current thread’s linked instance from the object family referenced by the static variable.
§Performance
For repeated access to the current thread’s linked instance, prefer reusing an Arc<T>
obtained from .to_arc().
If your code is not in a situation where it can reuse an existing Arc<T>, this method is
the optimal way to access the current thread’s linked instance of T.
Sourcepub fn to_arc(&self) -> Arc<T>
pub fn to_arc(&self) -> Arc<T>
Gets an Arc<T> to the current thread’s linked instance from
the object family referenced by the static variable.
The instance behind this Arc is the same one accessed by all other calls through the static
variable on this thread. Note that it is still possible to create multiple instances on a
single thread, e.g. by cloning the T within. The “one instance per thread” logic only
applies when the instances are accessed through the static variable.
§Performance
This function merely clones an Arc, which is relatively fast but still more work than
doing nothing. The most efficient way to access the current thread’s linked instance is
to reuse the Arc<T> returned from this method.
If you are not in a situation where you can reuse the Arc<T> and a shared reference is
satisfactory, prefer calling .with() instead, which does not create an
Arc and thereby saves a few nanoseconds.