pub struct Registry { /* private fields */ }Expand description
Registry for all types that can be constructed or otherwise injected.
Implementations§
source§impl Registry
impl Registry
sourcepub fn empty() -> Self
pub fn empty() -> Self
Create a new, empty, registry. This registry contains no pre-registered types.
Types that are auto-registered are also not included in this registry.
To get access to the auto-registered types (types that are annotated by
the derive macro), the global registry Registry::global needs to
be used.
sourcepub fn autoregistered() -> Self
pub fn autoregistered() -> Self
Create an empty registry, and add all autoregistered types into it.
This is the constructor for the global registry that can be acquired
with Registry::global.
sourcepub fn transient<T>(&self, ctor: fn() -> T)where
T: Registerable,
pub fn transient<T>(&self, ctor: fn() -> T)where
T: Registerable,
Register a new transient object, without dependencies.
To register a type with dependencies, use the builder returned from
Registry::with_deps.
§Parameters
ctor: A constructor function returning the newly constructedT. This constructor will be called for everyTthat is requested.
sourcepub fn singleton<T>(&self, ctor: fn() -> T)where
T: Registerable,
pub fn singleton<T>(&self, ctor: fn() -> T)where
T: Registerable,
Register a new singleton object, without dependencies.
To register a type with dependencies, use the builder returned from
Registry::with_deps.
§Parameters
ctor: A constructor function returning the newly constructedT. This constructor will be called once, lazily, when the first instance ofTis requested.
sourcepub fn get_transient<T>(&self) -> Option<T>where
T: Registerable,
pub fn get_transient<T>(&self) -> Option<T>where
T: Registerable,
Retrieves a newly constructed T from this registry.
Returns None if T wasn’t registered or failed to construct.
sourcepub fn get_singleton<T>(&self) -> Option<Ref<T>>where
T: Registerable,
pub fn get_singleton<T>(&self) -> Option<Ref<T>>where
T: Registerable,
Retrieves the singleton T from this registry.
Returns None if T wasn’t registered or failed to construct. The
singleton is a ref-counted pointer object (either Arc or Rc).
sourcepub fn with_deps<T, Deps>(&self) -> Builder<'_, T, Deps>where
Deps: DepBuilder<T>,
pub fn with_deps<T, Deps>(&self) -> Builder<'_, T, Deps>where
Deps: DepBuilder<T>,
Register a new transient or singleton with dependencies.
sourcepub fn validate_all(&self) -> bool
pub fn validate_all(&self) -> bool
Check whether all registered types have the required dependencies.
Returns true if for all registered types all of it’s dependencies can be constructed, false otherwise.
This is a potentially expensive call since it needs to go through the entire dependency tree for each registered type.
Nontheless, it’s recommended to call this before using the Registry.
sourcepub fn validate<T>(&self) -> boolwhere
T: Registerable,
pub fn validate<T>(&self) -> boolwhere
T: Registerable,
Check whether the type T is registered in this registry, and all
dependencies of the type T are also registered.
Returns true if the type and it’s dependencies can be constructed, false otherwise.
sourcepub fn global() -> &'static Self
pub fn global() -> &'static Self
Access the global registry.
This registry contains the types that are marked for auto-registration via the derive macro.
sourcepub unsafe fn reset_global()
pub unsafe fn reset_global()
Reset the global registry, removing all previously registered types, and re-running the auto-registration routines.
§Safety
Ensure that no other thread is currently using Registry::global().