From 0eebca4f177acc6d7b14cda355454b9806007370 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Fri, 5 Apr 2024 00:02:58 +0800 Subject: [PATCH] expose Driver enum and GenericDriver to public. --- postgres/src/driver.rs | 101 ++++++++++----------------------- postgres/src/driver/connect.rs | 12 ++-- postgres/src/driver/generic.rs | 2 +- 3 files changed, 36 insertions(+), 79 deletions(-) diff --git a/postgres/src/driver.rs b/postgres/src/driver.rs index da5758b5..d8e1901a 100644 --- a/postgres/src/driver.rs +++ b/postgres/src/driver.rs @@ -80,8 +80,18 @@ where /// tokio::spawn(drv.into_future()); /// } /// ``` -pub struct Driver { - inner: _Driver, +// TODO: use Box when life time GAT is object safe. +pub enum Driver { + Tcp(GenericDriver), + Dynamic(GenericDriver>), + #[cfg(feature = "tls")] + Tls(GenericDriver>), + #[cfg(unix)] + Unix(GenericDriver), + #[cfg(all(unix, feature = "tls"))] + UnixTls(GenericDriver>), + #[cfg(feature = "quic")] + Quic(GenericDriver), } impl Driver { @@ -89,8 +99,8 @@ impl Driver { /// downcast [Driver] to IoUringDriver if it's Tcp variant. /// IoUringDriver can not be a new variant of Dirver as it's !Send. pub fn try_into_io_uring_tcp(self) -> io_uring::IoUringDriver { - match self.inner { - _Driver::Tcp(drv) => { + match self { + Self::Tcp(drv) => { let std = drv.io.into_std().unwrap(); let tcp = xitca_io::net::io_uring::TcpStream::from_std(std); io_uring::IoUringDriver::new( @@ -105,94 +115,40 @@ impl Driver { } } - pub(super) fn tcp(drv: GenericDriver) -> Self { - Self { - inner: _Driver::Tcp(drv), - } - } - - pub(super) fn dynamic(drv: GenericDriver>) -> Self { - Self { - inner: _Driver::Dynamic(drv), - } - } - - #[cfg(feature = "tls")] - pub(super) fn tls(drv: GenericDriver>) -> Self { - Self { - inner: _Driver::Tls(drv), - } - } - - #[cfg(unix)] - pub(super) fn unix(drv: GenericDriver) -> Self { - Self { - inner: _Driver::Unix(drv), - } - } - - #[cfg(all(unix, feature = "tls"))] - pub(super) fn unix_tls(drv: GenericDriver>) -> Self { - Self { - inner: _Driver::UnixTls(drv), - } - } - - #[cfg(feature = "quic")] - pub(super) fn quic(drv: GenericDriver) -> Self { - Self { - inner: _Driver::Quic(drv), - } - } - // run till the connection is closed by Client. async fn run_till_closed(self) { - let _ = match self.inner { - _Driver::Tcp(drv) => drv.run().await, - _Driver::Dynamic(drv) => drv.run().await, + let _ = match self { + Self::Tcp(drv) => drv.run().await, + Self::Dynamic(drv) => drv.run().await, #[cfg(feature = "tls")] - _Driver::Tls(drv) => drv.run().await, + Self::Tls(drv) => drv.run().await, #[cfg(unix)] - _Driver::Unix(drv) => drv.run().await, + Self::Unix(drv) => drv.run().await, #[cfg(all(unix, feature = "tls"))] - _Driver::UnixTls(drv) => drv.run().await, + Self::UnixTls(drv) => drv.run().await, #[cfg(feature = "quic")] - _Driver::Quic(drv) => drv.run().await, + Self::Quic(drv) => drv.run().await, }; } } -// TODO: use Box when life time GAT is object safe. -enum _Driver { - Tcp(GenericDriver), - Dynamic(GenericDriver>), - #[cfg(feature = "tls")] - Tls(GenericDriver>), - #[cfg(unix)] - Unix(GenericDriver), - #[cfg(all(unix, feature = "tls"))] - UnixTls(GenericDriver>), - #[cfg(feature = "quic")] - Quic(GenericDriver), -} - impl AsyncLendingIterator for Driver { type Ok<'i> = backend::Message where Self: 'i; type Err = Error; #[inline] async fn try_next(&mut self) -> Result>, Self::Err> { - match self.inner { - _Driver::Tcp(ref mut drv) => drv.try_next().await, - _Driver::Dynamic(ref mut drv) => drv.try_next().await, + match self { + Self::Tcp(ref mut drv) => drv.try_next().await, + Self::Dynamic(ref mut drv) => drv.try_next().await, #[cfg(feature = "tls")] - _Driver::Tls(ref mut drv) => drv.try_next().await, + Self::Tls(ref mut drv) => drv.try_next().await, #[cfg(unix)] - _Driver::Unix(ref mut drv) => drv.try_next().await, + Self::Unix(ref mut drv) => drv.try_next().await, #[cfg(all(unix, feature = "tls"))] - _Driver::UnixTls(ref mut drv) => drv.try_next().await, + Self::UnixTls(ref mut drv) => drv.try_next().await, #[cfg(feature = "quic")] - _Driver::Quic(ref mut drv) => drv.try_next().await, + Self::Quic(ref mut drv) => drv.try_next().await, } } } @@ -206,6 +162,7 @@ impl IntoFuture for Driver { } } +// helper trait for interacting with io driver directly. pub(crate) trait Drive: Send { fn send(&mut self, msg: BytesMut) -> impl Future> + Send; diff --git a/postgres/src/driver/connect.rs b/postgres/src/driver/connect.rs index c8cb51bf..c2077a6b 100644 --- a/postgres/src/driver/connect.rs +++ b/postgres/src/driver/connect.rs @@ -32,7 +32,7 @@ pub(super) async fn connect(host: Host, cfg: &mut Config) -> Result<(DriverTx, D let io = super::tls::connect_tls(io, host, cfg).await?; let (mut drv, tx) = GenericDriver::new(io); prepare_session(&mut drv, cfg).await?; - Ok((tx, Driver::tls(drv))) + Ok((tx, Driver::Tls(drv))) } #[cfg(not(feature = "tls"))] { @@ -41,7 +41,7 @@ pub(super) async fn connect(host: Host, cfg: &mut Config) -> Result<(DriverTx, D } else { let (mut drv, tx) = GenericDriver::new(io); prepare_session(&mut drv, cfg).await?; - Ok((tx, Driver::tcp(drv))) + Ok((tx, Driver::Tcp(drv))) } } Host::Unix(ref _host) => { @@ -55,7 +55,7 @@ pub(super) async fn connect(host: Host, cfg: &mut Config) -> Result<(DriverTx, D let io = super::tls::connect_tls(io, host.as_ref(), cfg).await?; let (mut drv, tx) = GenericDriver::new(io); prepare_session(&mut drv, cfg).await?; - Ok((tx, Driver::unix_tls(drv))) + Ok((tx, Driver::UnixTls(drv))) } #[cfg(not(feature = "tls"))] { @@ -64,7 +64,7 @@ pub(super) async fn connect(host: Host, cfg: &mut Config) -> Result<(DriverTx, D } else { let (mut drv, tx) = GenericDriver::new(io); prepare_session(&mut drv, cfg).await?; - Ok((tx, Driver::unix(drv))) + Ok((tx, Driver::Unix(drv))) } } @@ -79,7 +79,7 @@ pub(super) async fn connect(host: Host, cfg: &mut Config) -> Result<(DriverTx, D let io = super::quic::connect_quic(_host, cfg.get_ports()).await?; let (mut drv, tx) = GenericDriver::new(io); prepare_session(&mut drv, cfg).await?; - Ok((tx, Driver::quic(drv))) + Ok((tx, Driver::Quic(drv))) } #[cfg(not(feature = "quic"))] { @@ -97,7 +97,7 @@ where { let (mut drv, tx) = GenericDriver::new(Box::new(io) as _); prepare_session(&mut drv, cfg).await?; - Ok((tx, Driver::dynamic(drv))) + Ok((tx, Driver::Dynamic(drv))) } async fn connect_tcp(host: &str, ports: &[u16]) -> Result { diff --git a/postgres/src/driver/generic.rs b/postgres/src/driver/generic.rs index a3d79ee2..924f4031 100644 --- a/postgres/src/driver/generic.rs +++ b/postgres/src/driver/generic.rs @@ -49,7 +49,7 @@ impl DriverTx { } } -pub(crate) struct GenericDriver { +pub struct GenericDriver { pub(crate) io: Io, pub(crate) write_buf: WriteBuf, pub(crate) read_buf: PagedBytesMut,