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 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) -> Result<(), ValidationError>
pub fn validate_all(&self) -> Result<(), ValidationError>
Check whether all registered types have the required dependencies.
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.
§Errors
Returns a ValidationError when the dependency graph is missing dependencies or
has cycles.
Sourcepub fn validate_all_full(&self) -> Result<(), FullValidationError>
pub fn validate_all_full(&self) -> Result<(), FullValidationError>
Check whether all registered types have the required dependencies and returns a detailed error about what’s missing or where a cycle was detected.
This is a potentially expensive call since it needs to go through the entire dependency tree for each registered type.
§Errors
Returns a FullValidationError when the dependency graph is missing dependencies or has
cycles.
Sourcepub fn validate<T>(&self) -> Result<(), ValidationError>where
T: Registerable,
pub fn validate<T>(&self) -> Result<(), ValidationError>where
T: Registerable,
Check whether the type T is registered in this registry, and all
dependencies of the type T are also registered.
§Errors
Returns a ValidationError when the dependency graph is missing dependencies or has cycles.
Sourcepub fn dotgraph(&self) -> Result<String, ValidationError>
pub fn dotgraph(&self) -> Result<String, ValidationError>
Return a string of the dependency graph visualized using graphviz’s dot language.
§Errors
Returns a ValidationError when the dependency graph is missing dependencies or has cycles.
Source§impl Registry
impl Registry
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.
§Panics
When the type has been registered already.
Sourcepub fn singleton<T, F>(&self, ctor: F)where
T: RegisterableSingleton,
F: SingletonCtor<T>,
pub fn singleton<T, F>(&self, ctor: F)where
T: RegisterableSingleton,
F: SingletonCtor<T>,
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.
§Panics
When the type has been registered already.
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: RegisterableSingleton,
pub fn get_singleton<T>(&self) -> Option<Ref<T>>where
T: RegisterableSingleton,
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 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().
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.