Crate scoped_tls[][src]

Scoped thread-local storage

This module provides the ability to generate scoped thread-local variables. In this sense, scoped indicates that thread local storage actually stores a reference to a value, and this reference is only placed in storage for a scoped amount of time.

There are no restrictions on what types can be placed into a scoped variable, but all scoped variables are initialized to the equivalent of null. Scoped thread local storage is useful when a value is present for a known period of time and it is not required to relinquish ownership of the contents.

Examples

#[macro_use]
extern crate scoped_tls;

scoped_thread_local!(static FOO: u32);

// Initially each scoped slot is empty.
assert!(!FOO.is_set());

// When inserting a value, the value is only in place for the duration
// of the closure specified.
FOO.set(&1, || {
    FOO.with(|slot| {
        assert_eq!(*slot, 1);
    });
});

Macros

scoped_thread_local

The macro. See the module level documentation for the description and examples.

Structs

ScopedKey

Type representing a thread local storage key corresponding to a reference to the type parameter T.