pub struct Expando { /* private fields */ }
Expand description
A lazy collection of values of every type.
An Expando contains a single instance of every type. The values are instantiated lazily when accessed. Useful for letting modules add their own state to context objects without requiring the context object itself to know about the types in every module.
Typically the type a module uses in the Expando will be private to that module, which lets the module know that no other code is accessing its slot on the expando.
Implementations§
Source§impl Expando
impl Expando
Sourcepub fn get<T: Any + Send + Sync + Default + 'static>(&self) -> Arc<T>
pub fn get<T: Any + Send + Sync + Default + 'static>(&self) -> Arc<T>
Get the slot in the expando associated with the given type.
The slot is added to the expando lazily but the same instance is returned every time the expando is queried for the same type.
Sourcepub fn get_or_init<T: Any + Send + Sync + 'static>(
&self,
init: impl FnOnce() -> T,
) -> Arc<T>
pub fn get_or_init<T: Any + Send + Sync + 'static>( &self, init: impl FnOnce() -> T, ) -> Arc<T>
Get the slot in the expando associated with the given type, running init
to initialize
the slot if needed.
The slot is added to the expando lazily but the same instance is returned every time the expando is queried for the same type.
Sourcepub fn get_or_try_init<T: Any + Send + Sync + 'static, E>(
&self,
try_init: impl FnOnce() -> Result<T, E>,
) -> Result<Arc<T>, E>
pub fn get_or_try_init<T: Any + Send + Sync + 'static, E>( &self, try_init: impl FnOnce() -> Result<T, E>, ) -> Result<Arc<T>, E>
Get the slot in the expando associated with the given type, running try_init
to initialize
the slot if needed. Returns an error only if try_init
returns an error.
The slot is added to the expando lazily but the same instance is returned every time the expando is queried for the same type.