1use std::io;
2use std::net::SocketAddr;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
6
7use crate::{ClientIo, Connected, ConnectedIo, Io, ServerIo};
8
9impl<T> Io for T where T: AsyncRead + AsyncWrite + Send + 'static {}
10
11impl<T> ConnectedIo for T where T: Io + Connected {}
12
13impl ClientIo {
14 pub fn new<I: Io>(io: I) -> Self {
15 ClientIo(Box::pin(io))
16 }
17}
18
19impl Connected for ClientIo {}
20
21impl AsyncRead for ClientIo {
22 fn poll_read(
23 mut self: Pin<&mut Self>,
24 cx: &mut Context<'_>,
25 buf: &mut ReadBuf<'_>,
26 ) -> Poll<io::Result<()>> {
27 Pin::new(&mut self.0).poll_read(cx, buf)
28 }
29}
30
31impl AsyncWrite for ClientIo {
32 fn poll_write(
33 mut self: Pin<&mut Self>,
34 cx: &mut Context<'_>,
35 buf: &[u8],
36 ) -> Poll<io::Result<usize>> {
37 Pin::new(&mut self.0).poll_write(cx, buf)
38 }
39
40 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
41 Pin::new(&mut self.0).poll_flush(cx)
42 }
43
44 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
45 Pin::new(&mut self.0).poll_shutdown(cx)
46 }
47}
48
49impl ServerIo {
50 pub fn new<I: ConnectedIo>(io: I) -> Self {
51 ServerIo(Box::pin(io))
52 }
53}
54
55impl Connected for ServerIo {
56 fn remote_addr(&self) -> Option<SocketAddr> {
57 (&*self.0).remote_addr()
58 }
59}
60
61impl AsyncRead for ServerIo {
62 fn poll_read(
63 mut self: Pin<&mut Self>,
64 cx: &mut Context<'_>,
65 buf: &mut ReadBuf<'_>,
66 ) -> Poll<io::Result<()>> {
67 Pin::new(&mut self.0).poll_read(cx, buf)
68 }
69}
70
71impl AsyncWrite for ServerIo {
72 fn poll_write(
73 mut self: Pin<&mut Self>,
74 cx: &mut Context<'_>,
75 buf: &[u8],
76 ) -> Poll<io::Result<usize>> {
77 Pin::new(&mut self.0).poll_write(cx, buf)
78 }
79
80 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
81 Pin::new(&mut self.0).poll_flush(cx)
82 }
83
84 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
85 Pin::new(&mut self.0).poll_shutdown(cx)
86 }
87}