Skip to main content

ProviderRegistry

Struct ProviderRegistry 

Source
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

Source

pub fn new() -> Self

Create a new empty registry.

Source

pub fn register<T: Send + Sync + 'static>( &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()) }));
Source

pub fn register_or_replace<T: Send + Sync + 'static>( &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.

Source

pub fn duplicates(&self) -> &[String]

Return duplicate "TypeName[token]" labels recorded so far.

Source

pub fn has<T: 'static>(&self) -> bool

Check if the registry has a provider for type T with the default token.

Equivalent to has_with_token::<T>(DEFAULT_TOKEN).

Source

pub fn has_with_token<T: 'static>(&self, token: &str) -> bool

Check if the registry has a provider for type T with the given token.

Source

pub fn len(&self) -> usize

Returns the total number of registered providers (across all tokens).

Source

pub fn is_empty(&self) -> bool

Returns true if no providers are registered.

Trait Implementations§

Source§

impl Debug for ProviderRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ProviderRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.