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,
impl<T> StaticInstances<T>where
T: Object,
Sourcepub fn get(&self) -> T
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more