Struct Registry

Source
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>

Source

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);
Examples found in repository?
examples/example.rs (line 7)
6
7
8
9
10
11
12
13
14
15
fn main() {
    Registry::register(ID, vec![1, 2, 3]);

    Registry::<Vec<i32>>::with(ID, |v| {
        v.push(4);
    });

    let v = Registry::<Vec<i32>>::remove(ID);
    println!("{:?}", v);
}
Source

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));
Source

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);
Examples found in repository?
examples/example.rs (line 13)
6
7
8
9
10
11
12
13
14
15
fn main() {
    Registry::register(ID, vec![1, 2, 3]);

    Registry::<Vec<i32>>::with(ID, |v| {
        v.push(4);
    });

    let v = Registry::<Vec<i32>>::remove(ID);
    println!("{:?}", v);
}
Source

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));
Examples found in repository?
examples/example.rs (lines 9-11)
6
7
8
9
10
11
12
13
14
15
fn main() {
    Registry::register(ID, vec![1, 2, 3]);

    Registry::<Vec<i32>>::with(ID, |v| {
        v.push(4);
    });

    let v = Registry::<Vec<i32>>::remove(ID);
    println!("{:?}", v);
}

Auto Trait Implementations§

§

impl<T> Freeze for Registry<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Registry<T>
where T: RefUnwindSafe,

§

impl<T> Send for Registry<T>
where T: Send,

§

impl<T> Sync for Registry<T>
where T: Sync,

§

impl<T> Unpin for Registry<T>
where T: Unpin,

§

impl<T> UnwindSafe for Registry<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.