1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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 struct ReadHalf<R> {
//     inner: R,
// }

// impl<R> ReadHalf<R> {
//     pub fn inner_mut(&mut self) -> &mut R {
//         &mut self.inner
//     }
// }

// pub struct WriteHalf<W> {
//     inner: W,
// }

// impl<W> WriteHalf<W> {
//     pub fn inner_mut(&mut self) -> &mut W {
//         &mut self.inner
//     }
// }

/// split something into two parts
pub trait Split {
    /// read half type
    type R;
    /// write half type
    type W;
    /// consume and return parts
    fn split(self) -> (Self::R, Self::W);
}

#[cfg(feature = "blocking")]
mod blocking {
    use super::Split;
    use std::{
        io::{Read, Write},
        net::TcpStream,
    };

    // impl<R: Read> Read for ReadHalf<R> {
    //     fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
    //         self.inner.read(buf)
    //     }
    // }

    // impl<W: Write> Write for WriteHalf<W> {
    //     fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
    //         self.inner.write(buf)
    //     }

    //     fn flush(&mut self) -> std::io::Result<()> {
    //         self.inner.flush()
    //     }
    // }

    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<R: AsyncRead + Unpin> AsyncRead for ReadHalf<R> {
    //     fn poll_read(
    //         mut self: std::pin::Pin<&mut Self>,
    //         cx: &mut std::task::Context<'_>,
    //         buf: &mut tokio::io::ReadBuf<'_>,
    //     ) -> std::task::Poll<std::io::Result<()>> {
    //         std::pin::Pin::new(self.inner_mut()).poll_read(cx, buf)
    //     }
    // }

    // impl<W: AsyncWrite + Unpin> AsyncWrite for WriteHalf<W> {
    //     fn poll_write(
    //         mut self: std::pin::Pin<&mut Self>,
    //         cx: &mut std::task::Context<'_>,
    //         buf: &[u8],
    //     ) -> std::task::Poll<Result<usize, std::io::Error>> {
    //         std::pin::Pin::new(self.inner_mut()).poll_write(cx, buf)
    //     }

    //     fn poll_flush(
    //         mut self: std::pin::Pin<&mut Self>,
    //         cx: &mut std::task::Context<'_>,
    //     ) -> std::task::Poll<Result<(), std::io::Error>> {
    //         std::pin::Pin::new(self.inner_mut()).poll_flush(cx)
    //     }

    //     fn poll_shutdown(
    //         mut self: std::pin::Pin<&mut Self>,
    //         cx: &mut std::task::Context<'_>,
    //     ) -> std::task::Poll<Result<(), std::io::Error>> {
    //         std::pin::Pin::new(self.inner_mut()).poll_shutdown(cx)
    //     }
    // }

    impl Split for TcpStream {
        type R = OwnedReadHalf;

        type W = OwnedWriteHalf;

        fn split(self) -> (Self::R, Self::W) {
            self.into_split()
        }
    }
}