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 new() -> ProviderRegistry
pub fn new() -> ProviderRegistry
Create a new empty registry.
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>, Error> + Send + Sync + 'static,
)
pub fn register_factory( &self, name: &str, factory: impl Fn() -> Result<Arc<dyn Provider>, Error> + 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)))
});Trait Implementations§
Source§impl Default for ProviderRegistry
impl Default for ProviderRegistry
Source§fn default() -> ProviderRegistry
fn default() -> ProviderRegistry
Auto Trait Implementations§
impl !Freeze for ProviderRegistry
impl !RefUnwindSafe for ProviderRegistry
impl !UnwindSafe for ProviderRegistry
impl Send for ProviderRegistry
impl Sync for ProviderRegistry
impl Unpin for ProviderRegistry
impl UnsafeUnpin for ProviderRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.