make Connect extend Connection to simplify bounds

This commit is contained in:
Ryan Leckey 2020-02-19 00:52:38 -08:00
parent d3d58ef0cf
commit bb17ebfbbd
8 changed files with 19 additions and 31 deletions

View File

@ -19,11 +19,9 @@ pub trait Connection: Executor + Send + 'static {
}
/// Represents a type that can directly establish a new connection.
pub trait Connect {
type Connection: Connection;
pub trait Connect: Connection {
/// Establish a new database connection.
fn connect<T>(url: T) -> BoxFuture<'static, crate::Result<Self::Connection>>
fn connect<T>(url: T) -> BoxFuture<'static, crate::Result<Self>>
where
T: TryInto<Url, Error = crate::Error>,
Self: Sized;

View File

@ -603,8 +603,6 @@ impl MySqlConnection {
}
impl Connect for MySqlConnection {
type Connection = MySqlConnection;
fn connect<T>(url: T) -> BoxFuture<'static, crate::Result<MySqlConnection>>
where
T: TryInto<Url, Error = crate::Error>,

View File

@ -11,7 +11,7 @@ use super::inner::{DecrementSizeGuard, SharedPool};
/// Will be returned to the pool on-drop.
pub struct PoolConnection<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
live: Option<Live<C>>,
pool: Arc<SharedPool<C>>,
@ -37,7 +37,7 @@ const DEREF_ERR: &str = "(bug) connection already released to pool";
impl<C> Deref for PoolConnection<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
type Target = C;
@ -48,7 +48,7 @@ where
impl<C> DerefMut for PoolConnection<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.live.as_mut().expect(DEREF_ERR).raw
@ -57,7 +57,7 @@ where
impl<C> Connection for PoolConnection<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
/// Detach the connection from the pool and close it nicely.
fn close(mut self) -> BoxFuture<'static, crate::Result<()>> {
@ -71,7 +71,7 @@ where
/// Returns the connection to the [`Pool`][crate::Pool] it was checked-out from.
impl<C> Drop for PoolConnection<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
fn drop(&mut self) {
if let Some(live) = self.live.take() {
@ -130,7 +130,7 @@ impl<'s, C> Floating<'s, Live<C>> {
pub fn attach(self, pool: &Arc<SharedPool<C>>) -> PoolConnection<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
let Floating { inner, guard } = self;

View File

@ -3,19 +3,13 @@ use std::ops::DerefMut;
use futures_core::{future::BoxFuture, stream::BoxStream};
use futures_util::StreamExt;
use crate::{
connection::{Connect, Connection},
describe::Describe,
executor::Executor,
pool::Pool,
Database,
};
use crate::{connection::Connect, describe::Describe, executor::Executor, pool::Pool, Database};
use super::PoolConnection;
impl<C> Executor for Pool<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
type Database = <C as Executor>::Database;
@ -66,7 +60,7 @@ where
impl<C> Executor for &'_ Pool<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
type Database = <C as Executor>::Database;
@ -115,7 +109,7 @@ where
impl<C> Executor for PoolConnection<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
type Database = <C as Executor>::Database;

View File

@ -130,7 +130,7 @@ where
impl<C> SharedPool<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
pub(super) async fn new_arc(url: &str, options: Options) -> crate::Result<Arc<Self>> {
let mut pool = Self {

View File

@ -6,7 +6,7 @@ use std::{
time::{Duration, Instant},
};
use crate::connection::{Connect, Connection};
use crate::connection::Connect;
use crate::transaction::Transaction;
use self::inner::SharedPool;
@ -26,7 +26,7 @@ pub struct Pool<C>(Arc<SharedPool<C>>);
impl<C> Pool<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
/// Creates a connection pool with the default configuration.
///
@ -127,7 +127,7 @@ impl<C> Clone for Pool<C> {
impl<C> fmt::Debug for Pool<C>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Pool")
@ -154,7 +154,7 @@ fn assert_pool_traits() {
fn assert_send_sync<T: Send + Sync>() {}
fn assert_clone<T: Clone>() {}
fn assert_pool<C: Connection + Connect<Connection = C>>() {
fn assert_pool<C: Connect>() {
assert_send_sync::<Pool<C>>();
assert_clone::<Pool<C>>();
}

View File

@ -1,7 +1,7 @@
use std::{marker::PhantomData, time::Duration};
use super::Pool;
use crate::connection::{Connect, Connection};
use crate::connection::Connect;
/// Builder for [Pool].
pub struct Builder<C> {
@ -102,7 +102,7 @@ impl<C> Builder<C> {
/// opened and placed into the pool.
pub async fn build(self, url: &str) -> crate::Result<Pool<C>>
where
C: Connection + Connect<Connection = C>,
C: Connect,
{
Pool::with_options(url, self.options).await
}

View File

@ -409,8 +409,6 @@ impl PgConnection {
}
impl Connect for PgConnection {
type Connection = PgConnection;
fn connect<T>(url: T) -> BoxFuture<'static, Result<PgConnection>>
where
T: TryInto<Url, Error = crate::Error>,