Skip to content

Commit

Permalink
AsConnection trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Maxim Orlovsky committed Jan 27, 2023
1 parent 485a81c commit 196a887
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ impl<T> 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<Self>
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
25 changes: 23 additions & 2 deletions src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use std::io;
use std::net::TcpStream;

use crate::connection::AsConnection;
use crate::{NetConnection, NetSession, NetStateMachine};

#[derive(Debug, Display)]
Expand All @@ -34,8 +35,8 @@ pub struct SplitIoError<T: SplitIo> {
impl<T: SplitIo + std::fmt::Debug> std::error::Error for SplitIoError<T> {}

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
///
Expand All @@ -49,6 +50,11 @@ pub struct NetReader<S: NetSession> {
pub(crate) reader: <S as SplitIo>::Read,
}

impl<S: NetSession> AsConnection for NetReader<S> {
type Connection = <<S as SplitIo>::Read as AsConnection>::Connection;
fn as_connection(&self) -> &Self::Connection { self.reader.as_connection() }
}

impl<S: NetSession> io::Read for NetReader<S> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.reader.read(buf) }
}
Expand All @@ -59,6 +65,11 @@ pub struct NetWriter<M: NetStateMachine, S: NetSession> {
pub(crate) writer: <S as SplitIo>::Write,
}

impl<M: NetStateMachine, S: NetSession> AsConnection for NetWriter<M, S> {
type Connection = <<S as SplitIo>::Write as AsConnection>::Connection;
fn as_connection(&self) -> &Self::Connection { self.writer.as_connection() }
}

impl<M: NetStateMachine, S: NetSession> io::Write for NetWriter<M, S> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.writer.write(buf) }
fn flush(&mut self) -> io::Result<()> { self.writer.flush() }
Expand All @@ -73,6 +84,11 @@ impl<C: NetConnection> io::Read for TcpReader<C> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.connection.read(buf) }
}

impl<C: NetConnection> AsConnection for TcpReader<C> {
type Connection = C;
fn as_connection(&self) -> &Self::Connection { &self.connection }
}

pub struct TcpWriter<C: NetConnection> {
unique_id: u64,
connection: C,
Expand All @@ -84,6 +100,11 @@ impl<C: NetConnection> io::Write for TcpWriter<C> {
fn flush(&mut self) -> io::Result<()> { self.connection.flush() }
}

impl<C: NetConnection> AsConnection for TcpWriter<C> {
type Connection = C;
fn as_connection(&self) -> &Self::Connection { &self.connection }
}

impl SplitIo for TcpStream {
type Read = TcpReader<Self>;
type Write = TcpWriter<Self>;
Expand Down

0 comments on commit 196a887

Please sign in to comment.