StaticInstances

Struct StaticInstances 

Source
pub struct StaticInstances<T>
where T: Object,
{ /* private fields */ }
Expand description

This is the real type of variables wrapped in the linked::instances! macro. See macro documentation for more details.

Instances of this type are created by the linked::instances! macro, never directly by user code, which can use .get() to obtain a linked instance of T.

Implementations§

Source§

impl<T> StaticInstances<T>
where T: Object,

Source

pub fn get(&self) -> T

Creates a new linked instance of T from the same family of linked objects as other instances created via the same static variable.

§Example
use std::thread;

linked::instances!(static COUNTER: Counter = Counter::new());

// Each call to get() creates a new linked instance
let mut counter1 = COUNTER.get();
let mut counter2 = COUNTER.get();

counter1.increment();
counter2.increment();

// Local counts are independent per instance
assert_eq!(counter1.local_count(), 1);
assert_eq!(counter2.local_count(), 1);

// But global state is shared across the family
assert_eq!(counter1.global_count(), 2);
assert_eq!(counter2.global_count(), 2);

// Works across threads too
thread::spawn(|| {
    let mut counter3 = COUNTER.get();
    counter3.increment();
    assert_eq!(counter3.global_count(), 3);
}).join().unwrap();
§Performance

This creates a new instance of T on every call so caching the return value is performance-critical.

If you only need one instance per thread, consider using the linked::thread_local_rc! or linked::thread_local_arc! macro for a more efficient solution.

Trait Implementations§

Source§

impl<T> Debug for StaticInstances<T>
where T: Object + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for StaticInstances<T>

§

impl<T> RefUnwindSafe for StaticInstances<T>

§

impl<T> Send for StaticInstances<T>

§

impl<T> Sync for StaticInstances<T>

§

impl<T> Unpin for StaticInstances<T>

§

impl<T> UnwindSafe for StaticInstances<T>

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.