pub struct Registry<T> { /* private fields */ }Expand description
Global Object Registry Visitor
This struct is used to register and access global objects of a specific type.
§Example
use gom::Registry;
Registry::register("key", 123);
let value = Registry::<i32>::apply("key", |v| *v + 1);
assert_eq!(value, Some(124));Implementations§
Source§impl<T: Send + 'static> Registry<T>
impl<T: Send + 'static> Registry<T>
Sourcepub fn register(key: &str, value: T)
pub fn register(key: &str, value: T)
Register a new value with the given key
If the key already exists in the same type, the value will be overwritten.
§Example
use gom::Registry;
Registry::register("key", 123);Sourcepub fn apply<R, F: FnOnce(&mut T) -> R>(key: &str, f: F) -> Option<R>
pub fn apply<R, F: FnOnce(&mut T) -> R>(key: &str, f: F) -> Option<R>
Apply a function to the value with the given key
If this function is nested, it will cause thread deadlock.
If the key does not exist, the function will not be called and None will be returned.
§Returns
Some(R) if the key exists and the function was applied successfully, None otherwise.
§Example
use gom::Registry;
Registry::register("key", 123);
let value = Registry::<i32>::apply("key", |v| *v + 1);
assert_eq!(value, Some(124));Sourcepub fn remove(key: &str) -> Option<T>
pub fn remove(key: &str) -> Option<T>
Get the value with the given key and reomve it from the registry
If the key does not exist, None will be returned.
§Returns
Some(T) if the key exists, None otherwise.
§Example
use gom::Registry;
Registry::register("key", 123);
let value = Registry::<i32>::remove("key");
assert_eq!(value, Some(123));
let value = Registry::<i32>::remove("key");
assert_eq!(value, None);Sourcepub fn with<R, F: FnOnce(&mut T) -> R>(key: &str, f: F) -> Option<R>
pub fn with<R, F: FnOnce(&mut T) -> R>(key: &str, f: F) -> Option<R>
Apply a function to the value with the given key
This function will not cause thread deadlocks.
If the key does not exist, the function will not be called and None will be returned.
In the context of ‘with’, the value cannot be retrieved again.
§Returns
Some(R) if the key exists and the function was applied successfully, None otherwise.
§Example
use gom::Registry;
Registry::register("key", 123);
let value = Registry::<i32>::with("key", |v| *v + 1);
assert_eq!(value, Some(124));