mod binary;
#[cfg(feature = "deflate")]
mod deflate;
mod frame;
mod text;
pub use binary::*;
#[cfg(feature = "deflate")]
pub use deflate::*;
pub use frame::*;
pub use text::*;
pub trait Split {
type R;
type W;
fn split(self) -> (Self::R, Self::W);
}
#[cfg(feature = "blocking")]
mod blocking {
use super::Split;
use std::{
io::{Read, Write},
net::TcpStream,
};
pub struct TcpReadHalf(TcpStream);
impl TcpReadHalf {
pub fn new(stream: TcpStream) -> Self {
Self(stream)
}
}
impl Read for TcpReadHalf {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.read(buf)
}
}
pub struct TcpWriteHalf(TcpStream);
impl TcpWriteHalf {
pub fn new(stream: TcpStream) -> Self {
Self(stream)
}
}
impl Write for TcpWriteHalf {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.0.flush()
}
}
impl Split for TcpStream {
type R = TcpReadHalf;
type W = TcpWriteHalf;
fn split(self) -> (Self::R, Self::W) {
let cloned = self.try_clone().expect("failed to split tcp stream");
(TcpReadHalf::new(self), TcpWriteHalf(cloned))
}
}
}
#[cfg(feature = "async")]
mod non_blocking {
use tokio::net::{
tcp::{OwnedReadHalf, OwnedWriteHalf},
TcpStream,
};
use super::Split;
impl Split for TcpStream {
type R = OwnedReadHalf;
type W = OwnedWriteHalf;
fn split(self) -> (Self::R, Self::W) {
self.into_split()
}
}
}