Task local data management
Allows storing arbitrary types inside task-local-storage (TLS), to be accessed anywhere within a task, keyed by a global pointer parameterized over the type of the TLS slot. Useful for dynamic variables, singletons, and interfacing with foreign code with bad callback interfaces.
To declare a new key for storing local data of a particular type, use the
local_data_key!
macro. This macro will expand to a static
item apppriately
named and annotated. This name is then passed to the functions in this module to
modify/read the slot specified by the key.
use std::local_data;
local_data_key!(key_int: int)
local_data_key!(key_vector: ~[int])
local_data::set(key_int, 3);
local_data::get(key_int, |opt| assert_eq!(opt, Some(&3)));
local_data::set(key_vector, ~[4]);
local_data::get(key_vector, |opt| assert_eq!(opt, Some(&~[4])));
KeyValue |
get | Retrieves a value from TLS. The closure provided is yielded |
get_mut | Retrieves a mutable value from TLS. The closure provided is yielded |
modify | Modifies a task-local value by temporarily removing it from task-local
storage and then re-inserting if |
pop | Removes a task-local value from task-local storage. This will return Some(value) if the key was present in TLS, otherwise it will return None. |
set | Inserts a value into task local storage. If the key is already present in TLS, then the previous value is removed and replaced with the provided data. |
Key | Indexes a task-local data slot. This pointer is used for comparison to
differentiate keys from one another. The actual type |
Prefix searches with a type followed by a colon (e.g.
fn:
) to restrict the search to a given type.
Accepted types are: fn
, mod
,
struct
(or str
), enum
,
trait
, typedef
(or
tdef
).