pub struct ProviderRegistry { /* private fields */ }Expand description
This is an instance-based alternative to the global CUSTOM_PROVIDERS static.
It supports register(), get(), remove(), and names(), falling back
to built-in providers from the built-in provider factory when a name isn’t found locally.
Providers can also be registered as factories via Self::register_factory.
A factory is a closure that lazily creates the provider on first access. The
result is cached, so the factory runs at most once per name.
Implementations§
Source§impl ProviderRegistry
impl ProviderRegistry
Sourcepub fn register(&self, name: &str, provider: impl Provider + 'static)
pub fn register(&self, name: &str, provider: impl Provider + 'static)
Register a custom provider.
Sourcepub fn register_arc(&self, name: &str, provider: Arc<dyn Provider>)
pub fn register_arc(&self, name: &str, provider: Arc<dyn Provider>)
Register a pre-boxed provider.
Sourcepub fn names(&self) -> Vec<String>
pub fn names(&self) -> Vec<String>
Return the set of currently registered custom provider names.
Sourcepub fn get(&self, name: &str) -> Option<Arc<dyn Provider>>
pub fn get(&self, name: &str) -> Option<Arc<dyn Provider>>
Get a provider by name.
Checks local custom providers first, then falls back to built-in providers.
Sourcepub fn get_custom(&self, name: &str) -> Option<Arc<dyn Provider>>
pub fn get_custom(&self, name: &str) -> Option<Arc<dyn Provider>>
Get a provider by name, checking only custom providers (no built-in fallback).
If the provider was registered via Self::register_factory and hasn’t
been materialized yet, the factory is invoked and the result cached.
Sourcepub fn register_factory(
&self,
name: &str,
factory: impl Fn() -> Result<Arc<dyn Provider>> + Send + Sync + 'static,
)
pub fn register_factory( &self, name: &str, factory: impl Fn() -> Result<Arc<dyn Provider>> + Send + Sync + 'static, )
Register a factory closure that lazily creates a provider on first access.
When Self::get_custom or Self::get is called and the provider is
not yet in custom, the factory is invoked, the result is cached in
custom, and the factory entry is removed.
§Example
registry.register_factory("my_provider", || {
let key = resolve_api_key("my_provider");
Ok(Arc::new(MyProvider::new(key)))
});