Struct StaticInstancePerThreadSync

Source
pub struct StaticInstancePerThreadSync<T>
where T: Object + Send + Sync,
{ /* 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>
where T: Object + Send + Sync,

Source

pub fn with<F, R>(&self, f: F) -> R
where F: FnOnce(&Arc<T>) -> 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.

Source

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.

Trait Implementations§

Source§

impl<T> Debug for StaticInstancePerThreadSync<T>
where T: Object + Send + Sync + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.