pub struct ProviderRegistry { /* private fields */ }Expand description
A registry of dynamically-registered providers for external types.
Every registration is keyed by (TypeId<T>, token). Use DEFAULT_TOKEN
("") when you only need one provider for a type; use distinct token strings
when you need multiple providers of the same type.
§Example
let mut registry = ProviderRegistry::new();
// Unnamed (default) provider
registry.register("", DynProvider::sync(|| Ok(reqwest::Client::new())));
// Named providers — two pools of the same type
registry.register("primary", DynProvider::new(|| async { Ok(primary_pool()) }));
registry.register("replica", DynProvider::new(|| async { Ok(replica_pool()) }));Implementations§
Source§impl ProviderRegistry
impl ProviderRegistry
Sourcepub fn new() -> ProviderRegistry
pub fn new() -> ProviderRegistry
Create a new empty registry.
Sourcepub fn register<T>(
&mut self,
token: impl Into<String>,
provider: DynProvider<T>,
)
pub fn register<T>( &mut self, token: impl Into<String>, provider: DynProvider<T>, )
Register a dynamic provider for type T under the given token.
If the same (type, token) pair is registered more than once, the
duplicate is recorded and surfaced as an error when
ContainerBuilder::build is called. Use
register_or_replace if you intentionally
want to override an existing registration.
§Token conventions
- Use
DEFAULT_TOKEN("") for the canonical, unnamed provider. - Use a descriptive string (
"primary","analytics") for named variants.
§Example
// Default registration
registry.register("", DynProvider::sync(|| Ok(reqwest::Client::new())));
// Named registrations of the same type
registry.register("primary", DynProvider::new(|| async { Ok(primary_pool()) }));
registry.register("replica", DynProvider::new(|| async { Ok(replica_pool()) }));Sourcepub fn register_or_replace<T>(
&mut self,
token: impl Into<String>,
provider: DynProvider<T>,
)
pub fn register_or_replace<T>( &mut self, token: impl Into<String>, provider: DynProvider<T>, )
Register a dynamic provider for type T under the given token,
silently replacing any previously registered provider for the same
(type, token) pair.
Use this in tests or layered-config scenarios where intentional override is expected.
Sourcepub fn duplicates(&self) -> &[String]
pub fn duplicates(&self) -> &[String]
Return duplicate "TypeName[token]" labels recorded so far.
Sourcepub fn has<T>(&self) -> boolwhere
T: 'static,
pub fn has<T>(&self) -> boolwhere
T: 'static,
Check if the registry has a provider for type T with the default token.
Equivalent to has_with_token::<T>(DEFAULT_TOKEN).
Sourcepub fn has_with_token<T>(&self, token: &str) -> boolwhere
T: 'static,
pub fn has_with_token<T>(&self, token: &str) -> boolwhere
T: 'static,
Check if the registry has a provider for type T with the given token.