From 5111a3d5749625c3d8e26a24a5a32c4da58f18d3 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Thu, 30 Nov 2023 23:51:18 +0300 Subject: net: Use ToConn and ToListener traits for objects that can be converted to Conn and Listener. --- net/src/connection.rs | 5 +++++ net/src/lib.rs | 4 ++-- net/src/listener.rs | 5 +++++ net/src/transports/tcp.rs | 22 ++++++++++++++++++++-- net/src/transports/tls.rs | 22 ++++++++++++++++++++-- net/src/transports/udp.rs | 14 +++++++++++++- net/src/transports/unix.rs | 25 ++++++++++++++++++++++++- 7 files changed, 89 insertions(+), 8 deletions(-) (limited to 'net/src') diff --git a/net/src/connection.rs b/net/src/connection.rs index b1d7550..3c047a0 100644 --- a/net/src/connection.rs +++ b/net/src/connection.rs @@ -8,6 +8,11 @@ use crate::{ /// Alias for `Box` pub type Conn = Box; +/// A trait for objects which can be converted to [`Conn`]. +pub trait ToConn { + fn to_conn(self) -> Conn; +} + /// Connection is a generic network connection interface for /// [`udp::UdpConn`], [`tcp::TcpConn`], and [`unix::UnixConn`]. /// diff --git a/net/src/lib.rs b/net/src/lib.rs index 813a090..ff466af 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -5,9 +5,9 @@ mod listener; mod transports; pub use { - connection::{dial, Conn, Connection}, + connection::{dial, Conn, Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, - listener::{listen, ConnListener, Listener}, + listener::{listen, ConnListener, Listener, ToListener}, transports::{ tcp::{dial_tcp, listen_tcp, TcpConn}, tls, diff --git a/net/src/listener.rs b/net/src/listener.rs index fcd1175..cab5330 100644 --- a/net/src/listener.rs +++ b/net/src/listener.rs @@ -8,6 +8,11 @@ use crate::{ /// Alias for `Box` pub type Listener = Box; +/// A trait for objects which can be converted to [`Listener`]. +pub trait ToListener { + fn to_listener(self) -> Listener; +} + /// ConnListener is a generic network listener. #[async_trait] pub trait ConnListener: Send + Sync { diff --git a/net/src/transports/tcp.rs b/net/src/transports/tcp.rs index 7cd7127..99243b5 100644 --- a/net/src/transports/tcp.rs +++ b/net/src/transports/tcp.rs @@ -7,9 +7,9 @@ use smol::{ }; use crate::{ - connection::Connection, + connection::{Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, - listener::ConnListener, + listener::{ConnListener, ToListener}, Error, Result, }; @@ -95,3 +95,21 @@ impl From for Box { Box::new(listener) } } + +impl ToConn for TcpStream { + fn to_conn(self) -> Box { + self.into() + } +} + +impl ToConn for TcpConn { + fn to_conn(self) -> Box { + Box::new(self) + } +} + +impl ToListener for TcpListener { + fn to_listener(self) -> Box { + self.into() + } +} diff --git a/net/src/transports/tls.rs b/net/src/transports/tls.rs index bc928b0..8a43d7d 100644 --- a/net/src/transports/tls.rs +++ b/net/src/transports/tls.rs @@ -9,9 +9,9 @@ use smol::{ }; use crate::{ - connection::Connection, + connection::{Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, - listener::ConnListener, + listener::{ConnListener, ToListener}, Error, Result, }; @@ -150,3 +150,21 @@ impl From for Box { Box::new(listener) } } + +impl ToConn for TlsStream { + fn to_conn(self) -> Box { + self.into() + } +} + +impl ToConn for TlsConn { + fn to_conn(self) -> Box { + Box::new(self) + } +} + +impl ToListener for TlsListener { + fn to_listener(self) -> Box { + self.into() + } +} diff --git a/net/src/transports/udp.rs b/net/src/transports/udp.rs index 9576876..a8b505c 100644 --- a/net/src/transports/udp.rs +++ b/net/src/transports/udp.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use smol::net::UdpSocket; use crate::{ - connection::Connection, + connection::{Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, Error, Result, }; @@ -79,3 +79,15 @@ impl From for Box { Box::new(UdpConn::new(conn)) } } + +impl ToConn for UdpSocket { + fn to_conn(self) -> Box { + self.into() + } +} + +impl ToConn for UdpConn { + fn to_conn(self) -> Box { + Box::new(self) + } +} diff --git a/net/src/transports/unix.rs b/net/src/transports/unix.rs index c546333..3867040 100644 --- a/net/src/transports/unix.rs +++ b/net/src/transports/unix.rs @@ -6,7 +6,12 @@ use smol::{ net::unix::{UnixListener, UnixStream}, }; -use crate::{connection::Connection, endpoint::Endpoint, listener::ConnListener, Error, Result}; +use crate::{ + connection::{Connection, ToConn}, + endpoint::Endpoint, + listener::{ConnListener, ToListener}, + Error, Result, +}; /// Unix domain socket implementation of the [`Connection`] trait. pub struct UnixConn { @@ -86,3 +91,21 @@ impl From for Box { Box::new(listener) } } + +impl ToConn for UnixStream { + fn to_conn(self) -> Box { + self.into() + } +} + +impl ToConn for UnixConn { + fn to_conn(self) -> Box { + Box::new(self) + } +} + +impl ToListener for UnixListener { + fn to_listener(self) -> Box { + self.into() + } +} -- cgit v1.2.3