Macro instance_per_access

Source
macro_rules! instance_per_access {
    () => { ... };
    ($(#[$attr:meta])* $vis:vis static $NAME:ident: $t:ty = $e:expr; $($rest:tt)*) => { ... };
    ($(#[$attr:meta])* $vis:vis static $NAME:ident: $t:ty = $e:expr) => { ... };
}
Expand description

Declares that all static variables within the macro body contain linked objects, with each access to this variable returning a new instance from the same family.

Each .get() on an included static variable returns a new linked object instance, with all instances obtained from the same static variable being part of the same family.

§Dynamic family relationships

If you need Arc-style dynamic multithreaded storage (i.e. not a single static variable), pass instances of Handle<T> between threads instead of (or in addition to) using this macro. You can obtain a Handle<T> from any linked object via the .handle() method of the linked::Object trait, even if the instance of the linked object originally came from a static variable.

§Example

linked::instance_per_access!(static TOKEN_CACHE: TokenCache = TokenCache::with_capacity(1000));

fn do_something() {
    // `.get()` returns a unique instance of the linked object on every call.
    let token_cache = TOKEN_CACHE.get();

    let token = token_cache.get_token();
}