pub struct Storage<T: Send + Sync + 'static> { /* fields omitted */ }A single storage location for global access to a value.
A Storage instance can hold a single value in a global context. A
Storage instance begins without a value and must be initialized via the
set method. Once a value has been set, it can be retrieved at
any time and in any thread via the get method. The
try_get can be used to determine whether the Storage
has been initialized before attempting to retrieve the value.
For safety reasons, values stored in Storage must be Send + Sync + 'static.
The following example uses Storage to hold a global instance of a
HashMap which can be modified at will:
use std::collections::HashMap;
use std::sync::Mutex;
use std::thread;
use state::Storage;
static GLOBAL_MAP: Storage<Mutex<HashMap<String, String>>> = Storage::new();
fn run_program() {
let mut map = GLOBAL_MAP.get().lock().unwrap();
map.insert("another_key".into(), "another_value".into());
}
fn main() {
let mut initial_map = HashMap::new();
initial_map.insert("key".into(), "value".into());
GLOBAL_MAP.set(Mutex::new(initial_map));
thread::spawn(|| run_program()).join().expect("thread");
let map = GLOBAL_MAP.get().lock().unwrap();
assert_eq!(map.get("another_key").unwrap(), "another_value");
}
Create a new, uninitialized storage location.
use state::Storage;
static MY_GLOBAL: Storage<String> = Storage::new();
Sets the value for this storage unit to value if it has not already
been set before.
If a value has previously been set, self is unchanged and false is
returned. Otherwise true is returned.
static MY_GLOBAL: Storage<&'static str> = Storage::new();
assert_eq!(MY_GLOBAL.set("Hello, world!"), true);
assert_eq!(MY_GLOBAL.set("Goodbye, world!"), false);
Attempts to borrow the value in this storage location.
Returns Some if the state has previously been set.
Otherwise returns None.
static MY_GLOBAL: Storage<&'static str> = Storage::new();
assert_eq!(MY_GLOBAL.try_get(), None);
MY_GLOBAL.set("Hello, world!");
assert_eq!(MY_GLOBAL.try_get(), Some(&"Hello, world!"));
Borrows the value in this storage location.
Panics if a value has not previously been
set. Use try_get for a non-panicking
version.
static MY_GLOBAL: Storage<&'static str> = Storage::new();
MY_GLOBAL.set("Hello, world!");
assert_eq!(*MY_GLOBAL.get(), "Hello, world!");
If the storage location has not yet been set, it is set to the return
value of from. Returns a borrow to the value in this storage location.
static MY_GLOBAL: Storage<&'static str> = Storage::new();
assert_eq!(*MY_GLOBAL.get_or_set(|| "Hello, world!"), "Hello, world!");
Performs copy-assignment from source. Read more
Executes the destructor for this type. Read more
Formats the value using the given formatter. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id)
this method will likely be replaced by an associated static
🔬 This is a nightly-only experimental API. (try_from)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from)
Mutably borrows from an owned value. Read more