From fe2fbfd513a6f0eb28c15f052884fb241c70171c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 31 May 2024 13:56:22 +0200 Subject: [PATCH] bb8: move State into api module --- bb8/src/api.rs | 21 +++++++++++++++------ bb8/src/inner.rs | 6 +++--- bb8/src/internals.rs | 17 +++++------------ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/bb8/src/api.rs b/bb8/src/api.rs index 8b99798..efbd869 100644 --- a/bb8/src/api.rs +++ b/bb8/src/api.rs @@ -9,7 +9,6 @@ use async_trait::async_trait; use crate::inner::PoolInner; use crate::internals::Conn; -pub use crate::internals::State; /// A generic connection pool. pub struct Pool @@ -45,11 +44,6 @@ impl Pool { Builder::new() } - /// Returns information about the current state of the pool. - pub fn state(&self) -> State { - self.inner.state() - } - /// Retrieves a connection from the pool. pub async fn get(&self) -> Result, RunError> { self.inner.get().await @@ -76,6 +70,21 @@ impl Pool { pub async fn dedicated_connection(&self) -> Result { self.inner.connect().await } + + /// Returns information about the current state of the pool. + pub fn state(&self) -> State { + self.inner.state() + } +} + +/// Information about the state of a `Pool`. +#[derive(Debug)] +#[non_exhaustive] +pub struct State { + /// The number of connections currently being managed by the pool. + pub connections: u32, + /// The number of idle connections. + pub idle_connections: u32, } /// A builder for a connection pool. diff --git a/bb8/src/inner.rs b/bb8/src/inner.rs index 4e73ddd..7ca6e20 100644 --- a/bb8/src/inner.rs +++ b/bb8/src/inner.rs @@ -9,8 +9,8 @@ use futures_util::TryFutureExt; use tokio::spawn; use tokio::time::{interval_at, sleep, timeout, Interval}; -use crate::api::{Builder, ConnectionState, ManageConnection, PooledConnection, RunError}; -use crate::internals::{Approval, ApprovalIter, Conn, SharedPool, State}; +use crate::api::{Builder, ConnectionState, ManageConnection, PooledConnection, RunError, State}; +use crate::internals::{Approval, ApprovalIter, Conn, SharedPool}; pub(crate) struct PoolInner where @@ -148,7 +148,7 @@ where /// Returns information about the current state of the pool. pub(crate) fn state(&self) -> State { - self.inner.internals.lock().state() + (&*self.inner.internals.lock()).into() } // Outside of Pool to avoid borrow splitting issues on self diff --git a/bb8/src/internals.rs b/bb8/src/internals.rs index ed153f0..59dfe63 100644 --- a/bb8/src/internals.rs +++ b/bb8/src/internals.rs @@ -5,7 +5,7 @@ use std::time::Instant; use crate::{api::QueueStrategy, lock::Mutex}; use tokio::sync::Notify; -use crate::api::{Builder, ManageConnection}; +use crate::api::{Builder, ManageConnection, State}; use std::collections::VecDeque; /// The guts of a `Pool`. @@ -153,8 +153,11 @@ where self.dropped((before - self.conns.len()) as u32, config) } +} - pub(crate) fn state(&self) -> State { +#[allow(clippy::from_over_into)] // Keep this more private with the internal type +impl Into for &PoolInternals { + fn into(self) -> State { State { connections: self.num_conns, idle_connections: self.conns.len() as u32, @@ -245,13 +248,3 @@ impl From> for IdleConn { } } } - -/// Information about the state of a `Pool`. -#[derive(Debug)] -#[non_exhaustive] -pub struct State { - /// The number of connections currently being managed by the pool. - pub connections: u32, - /// The number of idle connections. - pub idle_connections: u32, -}