pub struct PoolRegistry<P: PoolProvider = DbPools> { /* private fields */ }Expand description
A registry of database pool providers keyed by name.
PoolRegistry is available with the with-named-pools feature. It stores
one homogeneous provider type and defaults to DbPools. Selecting a name
returns the provider for that name; the registry itself does not keep a
stateful “current” pool and does not implement PoolProvider.
§Examples
use sqlx_pool_registry::sqlx::{self, postgres::PgPoolOptions};
use sqlx_pool_registry::{DbPools, PoolProvider, PoolRegistry};
let auth_primary = PgPoolOptions::new()
.connect_lazy("postgresql://localhost/auth")?;
let auth_replica = PgPoolOptions::new()
.connect_lazy("postgresql://localhost/auth_replica")?;
let analytics = PgPoolOptions::new()
.connect_lazy("postgresql://localhost/analytics")?;
let mut pools = PoolRegistry::new();
pools.insert("auth", DbPools::with_replica(auth_primary, auth_replica));
pools.insert("analytics", DbPools::new(analytics));
let auth = pools.try_get("auth")?;
let _primary = auth.primary();
let _replica = auth.replica(); // Option<&PgPool>, with no fallback
let _read = auth.read(); // Replica, or primary when no replica is configuredImplementations§
Source§impl<P: PoolProvider> PoolRegistry<P>
impl<P: PoolProvider> PoolRegistry<P>
Sourcepub fn insert(&mut self, name: impl Into<String>, provider: P) -> Option<P>
pub fn insert(&mut self, name: impl Into<String>, provider: P) -> Option<P>
Insert a provider under name.
If the name was already registered, the previous provider is replaced and returned.
Sourcepub fn get(&self, name: &str) -> Option<&P>
pub fn get(&self, name: &str) -> Option<&P>
Get a provider by name.
Names are matched exactly and case-sensitively.
Sourcepub fn try_get(&self, name: &str) -> Result<&P, UnknownPool>
pub fn try_get(&self, name: &str) -> Result<&P, UnknownPool>
Get a provider by name, returning UnknownPool when it is absent.
Sourcepub fn contains_key(&self, name: &str) -> bool
pub fn contains_key(&self, name: &str) -> bool
Return whether a provider is registered under name.
Trait Implementations§
Source§impl<P: Clone + PoolProvider> Clone for PoolRegistry<P>
impl<P: Clone + PoolProvider> Clone for PoolRegistry<P>
Source§fn clone(&self) -> PoolRegistry<P>
fn clone(&self) -> PoolRegistry<P>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<P: Debug + PoolProvider> Debug for PoolRegistry<P>
impl<P: Debug + PoolProvider> Debug for PoolRegistry<P>
Source§impl<P: PoolProvider> Default for PoolRegistry<P>
impl<P: PoolProvider> Default for PoolRegistry<P>
Source§impl<K, P> Extend<(K, P)> for PoolRegistry<P>
impl<K, P> Extend<(K, P)> for PoolRegistry<P>
Source§fn extend<T: IntoIterator<Item = (K, P)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, P)>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<K, P> FromIterator<(K, P)> for PoolRegistry<P>
impl<K, P> FromIterator<(K, P)> for PoolRegistry<P>
Auto Trait Implementations§
impl<P> Freeze for PoolRegistry<P>
impl<P> RefUnwindSafe for PoolRegistry<P>where
P: RefUnwindSafe,
impl<P> Send for PoolRegistry<P>
impl<P> Sync for PoolRegistry<P>
impl<P> Unpin for PoolRegistry<P>where
P: Unpin,
impl<P> UnsafeUnpin for PoolRegistry<P>
impl<P> UnwindSafe for PoolRegistry<P>where
P: UnwindSafe,
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more