From 196a887f695f5fb9064df22e93abd714c0855537 Mon Sep 17 00:00:00 2001 From: "Dr. Maxim Orlovsky" Date: Fri, 27 Jan 2023 12:40:35 +0100 Subject: [PATCH] AsConnection trait --- src/connection.rs | 7 ++++++- src/lib.rs | 2 +- src/split.rs | 25 +++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index e4ce53c..c74a39d 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -33,8 +33,13 @@ impl Address for T where T: Addr + Send + Clone + Eq + Hash + Debug + Display pub trait NetStream: Send + io::Read + io::Write {} +pub trait AsConnection { + type Connection: NetConnection; + fn as_connection(&self) -> &Self::Connection; +} + /// Network stream is an abstraction of TCP stream object. -pub trait NetConnection: Send + NetStream + AsRawFd + Debug { +pub trait NetConnection: NetStream + AsRawFd + Debug { type Addr: Address; fn connect_blocking(addr: Self::Addr) -> io::Result diff --git a/src/lib.rs b/src/lib.rs index dd655c3..9e85006 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ pub mod resource; pub const READ_BUFFER_SIZE: usize = u16::MAX as usize; -pub use connection::{Address, NetConnection, NetStream}; +pub use connection::{Address, AsConnection, NetConnection, NetStream}; pub use frame::{Frame, Marshaller}; pub use listener::NetListener; #[cfg(feature = "io-reactor")] diff --git a/src/split.rs b/src/split.rs index 62d93ac..b70d1cc 100644 --- a/src/split.rs +++ b/src/split.rs @@ -22,6 +22,7 @@ use std::io; use std::net::TcpStream; +use crate::connection::AsConnection; use crate::{NetConnection, NetSession, NetStateMachine}; #[derive(Debug, Display)] @@ -34,8 +35,8 @@ pub struct SplitIoError { impl std::error::Error for SplitIoError {} pub trait SplitIo: Sized { - type Read: io::Read + Sized; - type Write: io::Write + Sized; + type Read: AsConnection + io::Read + Sized; + type Write: AsConnection + io::Write + Sized; /// # Panics /// @@ -49,6 +50,11 @@ pub struct NetReader { pub(crate) reader: ::Read, } +impl AsConnection for NetReader { + type Connection = <::Read as AsConnection>::Connection; + fn as_connection(&self) -> &Self::Connection { self.reader.as_connection() } +} + impl io::Read for NetReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { self.reader.read(buf) } } @@ -59,6 +65,11 @@ pub struct NetWriter { pub(crate) writer: ::Write, } +impl AsConnection for NetWriter { + type Connection = <::Write as AsConnection>::Connection; + fn as_connection(&self) -> &Self::Connection { self.writer.as_connection() } +} + impl io::Write for NetWriter { fn write(&mut self, buf: &[u8]) -> io::Result { self.writer.write(buf) } fn flush(&mut self) -> io::Result<()> { self.writer.flush() } @@ -73,6 +84,11 @@ impl io::Read for TcpReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { self.connection.read(buf) } } +impl AsConnection for TcpReader { + type Connection = C; + fn as_connection(&self) -> &Self::Connection { &self.connection } +} + pub struct TcpWriter { unique_id: u64, connection: C, @@ -84,6 +100,11 @@ impl io::Write for TcpWriter { fn flush(&mut self) -> io::Result<()> { self.connection.flush() } } +impl AsConnection for TcpWriter { + type Connection = C; + fn as_connection(&self) -> &Self::Connection { &self.connection } +} + impl SplitIo for TcpStream { type Read = TcpReader; type Write = TcpWriter;