irc_async/client/
stream.rs

1use std::io;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use tokio::io::{AsyncRead, AsyncWrite};
6use tokio::net::TcpStream;
7use tokio_tls::TlsStream;
8
9pub enum ClientStream {
10    Plain(TcpStream),
11    Tls(TlsStream<TcpStream>),
12}
13
14impl AsyncRead for ClientStream {
15    fn poll_read(
16        self: Pin<&mut Self>,
17        context: &mut Context,
18        buf: &mut [u8],
19    ) -> Poll<Result<usize, io::Error>> {
20        match self.get_mut() {
21            ClientStream::Plain(stream) => TcpStream::poll_read(Pin::new(stream), context, buf),
22            ClientStream::Tls(stream) => {
23                TlsStream::<TcpStream>::poll_read(Pin::new(stream), context, buf)
24            }
25        }
26    }
27}
28
29impl AsyncWrite for ClientStream {
30    fn poll_write(
31        self: Pin<&mut Self>,
32        context: &mut Context,
33        buf: &[u8],
34    ) -> Poll<Result<usize, io::Error>> {
35        match self.get_mut() {
36            ClientStream::Plain(stream) => TcpStream::poll_write(Pin::new(stream), context, buf),
37            ClientStream::Tls(stream) => {
38                TlsStream::<TcpStream>::poll_write(Pin::new(stream), context, buf)
39            }
40        }
41    }
42
43    fn poll_flush(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
44        match self.get_mut() {
45            ClientStream::Plain(stream) => TcpStream::poll_flush(Pin::new(stream), context),
46            ClientStream::Tls(stream) => {
47                TlsStream::<TcpStream>::poll_flush(Pin::new(stream), context)
48            }
49        }
50    }
51
52    fn poll_shutdown(
53        self: Pin<&mut Self>,
54        context: &mut Context<'_>,
55    ) -> Poll<Result<(), io::Error>> {
56        match self.get_mut() {
57            ClientStream::Plain(stream) => TcpStream::poll_shutdown(Pin::new(stream), context),
58            ClientStream::Tls(stream) => {
59                TlsStream::<TcpStream>::poll_shutdown(Pin::new(stream), context)
60            }
61        }
62    }
63}