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.
Sourcepub fn global() -> Ref<Self>
pub fn global() -> Ref<Self>
Access the global registry.
This registry contains the types that are marked for auto-registration via the derive macro.
Sourcepub fn child(self: &Ref<Self>) -> Ref<Self>
pub fn child(self: &Ref<Self>) -> Ref<Self>
Create a new child registry.
When this registry is used to register and retrieve objects, the objects directly registered on the returned registry are preferred to the objects that have been previously registered with the parent registry.
This allows for sub-registries to override objects from their parent.
Source§impl Registry
impl Registry
Sourcepub fn register_transient<T, C>(&self, ctor: C)
pub fn register_transient<T, C>(&self, ctor: C)
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 try_register_transient<T, C>(&self, ctor: C)
pub fn try_register_transient<T, C>(&self, ctor: C)
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 aResult<T, E>. This constructor will be called for everyTthat is requested.
§Panics
When the type has been registered already.
Sourcepub fn register_singleton<T, F>(&self, ctor: F)where
T: RegisterableSingleton,
F: SingletonCtor<T>,
pub fn register_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 try_register_singleton<T, F>(&self, ctor: F)where
T: RegisterableSingleton,
F: SingletonCtorFallible<T>,
pub fn try_register_singleton<T, F>(&self, ctor: F)where
T: RegisterableSingleton,
F: SingletonCtorFallible<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 aResult<T, E>. This constructor will be called once, lazily, when the first instance ofTis requested.
§Panics
When the type has been registered already.
Sourcepub fn transient<T>(&self) -> Result<T, ResolveError>where
T: Registerable,
pub fn transient<T>(&self) -> Result<T, ResolveError>where
T: Registerable,
Retrieves a newly constructed T from this registry.
§Errors
Returns an error if the registered constructor fails, or the type fails to resolve (i.e is not registered).
Sourcepub fn singleton<T>(&self) -> Result<Ref<T>, ResolveError>where
T: RegisterableSingleton,
pub fn singleton<T>(&self) -> Result<Ref<T>, ResolveError>where
T: RegisterableSingleton,
Retrieves the singleton T from this registry.
The return singleton is a ref-counted pointer object (either Arc or Rc).
§Errors
Returns an error if the registered constructor fails, or the type fails to resolve (i.e is not registered).
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.