[src]

Module std::local_data

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 appropriately 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.map(|x| *x), Some(3)));

local_data::set(key_vector, ~[4]);
local_data::get(key_vector, |opt| assert_eq!(*opt.unwrap(), ~[4]));
KeyValue
LocalData
get

Retrieves a value from TLS. The closure provided is yielded Some of a reference to the value located in TLS if one exists, or None if the key provided is not present in TLS currently.

get_mut

Retrieves a mutable value from TLS. The closure provided is yielded Some of a reference to the mutable value located in TLS if one exists, or None if the key provided is not present in TLS currently.

modify

Modifies a task-local value by temporarily removing it from task-local storage and then re-inserting if Some is returned from the closure.

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 T is not used anywhere as a member of this type, except that it is parameterized with it to define the type of each key's value.