ws_tool/codec/
mod.rs

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
21/// split something into two parts
22pub trait Split {
23    /// read half type
24    type R;
25    /// write half type
26    type W;
27    /// consume and return parts
28    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    // impl<R: Read> Read for ReadHalf<R> {
40    //     fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
41    //         self.inner.read(buf)
42    //     }
43    // }
44
45    // impl<W: Write> Write for WriteHalf<W> {
46    //     fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
47    //         self.inner.write(buf)
48    //     }
49
50    //     fn flush(&mut self) -> std::io::Result<()> {
51    //         self.inner.flush()
52    //     }
53    // }
54
55    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    // impl<S: AsyncRead + AsyncWrite> crate::codec::Split for BufStream<S> {
109    //     type R = tokio::io::ReadHalf<BufStream<S>>;
110    //     type W = tokio::io::WriteHalf<BufStream<S>>;
111    //     fn split(self) -> (Self::R, Self::W) {
112    //         tokio::io::split(self)
113    //     }
114    // }
115}