1mod binary;
2#[cfg(any(
3 feature = "deflate",
4 feature = "deflate_ng",
5 feature = "deflate_static"
6))]
7mod deflate;
8mod frame;
9mod text;
10
11pub use binary::*;
12#[cfg(any(
13 feature = "deflate",
14 feature = "deflate_ng",
15 feature = "deflate_static"
16))]
17pub use deflate::*;
18pub use frame::*;
19pub use text::*;
20
21pub trait Split {
23 type R;
25 type W;
27 fn split(self) -> (Self::R, Self::W);
29}
30
31#[cfg(feature = "sync")]
32mod blocking {
33 use super::Split;
34 use std::{
35 io::{Read, Write},
36 net::TcpStream,
37 };
38
39 pub struct TcpReadHalf(pub TcpStream);
56
57 impl Read for TcpReadHalf {
58 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
59 self.0.read(buf)
60 }
61 }
62
63 pub struct TcpWriteHalf(pub TcpStream);
64
65 impl Write for TcpWriteHalf {
66 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
67 self.0.write(buf)
68 }
69
70 fn flush(&mut self) -> std::io::Result<()> {
71 self.0.flush()
72 }
73 }
74
75 impl Split for TcpStream {
76 type R = TcpStream;
77 type W = TcpStream;
78 fn split(self) -> (Self::R, Self::W) {
79 let cloned = self.try_clone().expect("failed to split tcp stream");
80 (self, cloned)
81 }
82 }
83}
84
85#[cfg(feature = "async")]
86mod non_blocking {
87 use tokio::{
88 io::{AsyncRead, AsyncWrite, BufStream},
89 net::TcpStream,
90 };
91
92 impl crate::codec::Split for TcpStream {
93 type R = tokio::io::ReadHalf<TcpStream>;
94 type W = tokio::io::WriteHalf<TcpStream>;
95 fn split(self) -> (Self::R, Self::W) {
96 tokio::io::split(self)
97 }
98 }
99
100 impl<S: AsyncRead + AsyncWrite> crate::codec::Split for BufStream<S> {
101 type R = tokio::io::ReadHalf<BufStream<S>>;
102 type W = tokio::io::WriteHalf<BufStream<S>>;
103 fn split(self) -> (Self::R, Self::W) {
104 tokio::io::split(self)
105 }
106 }
107
108 }