use core::{future::Future, ops::Deref, pin::Pin};
use std::sync::Arc;
#[cfg(feature = "async")]
use futures_io::{AsyncRead, AsyncWrite};
#[cfg(feature = "tokio")]
use tokio::io::{AsyncRead, AsyncWrite};
use crate::{Result, Url};
pub type BoxedConnection = Box<dyn Connection>;
pub type BoxedConnect = Pin<Box<dyn Connect>>;
pub type BoxedConnector = Arc<dyn Connector>;
pub type BoxedBackend = Box<dyn Backend>;
pub trait Connect: Future<Output = Result<BoxedConnection>> + Send {}
impl<T> Connect for T where T: Future<Output = Result<BoxedConnection>> + Send {}
pub trait Connection: AsyncRead + AsyncWrite + Send + Unpin {}
impl<T> Connection for T where T: AsyncRead + AsyncWrite + Send + Unpin {}
pub trait Connector: Send + Sync {
fn url(&self) -> &Url;
fn connect(&self) -> BoxedConnect;
}
pub trait Backend {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn connector(&self, url: &Url) -> Option<BoxedConnector>;
}
impl<T> Backend for T
where
T: Deref<Target = dyn Backend>,
{
fn name(&self) -> &str {
self.deref().name()
}
fn description(&self) -> &str {
self.deref().description()
}
fn connector(&self, url: &Url) -> Option<BoxedConnector> {
self.deref().connector(url)
}
}