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
use futures::{Stream, Poll};
use tokio::net::TcpStream;
use tokio_io::{AsyncRead, AsyncWrite};
use std::io;
use std::net::SocketAddr;
#[cfg(feature = "rustls")]
use tokio_rustls::{TlsStream, rustls::ServerSession};
pub trait Connection: AsyncRead + AsyncWrite {
fn peer_addr(&self) -> Option<SocketAddr>;
}
pub trait ConnectionStream {
type Item: Connection;
fn poll_next(&mut self) -> Poll<Option<Self::Item>, io::Error>;
}
impl Connection for TcpStream {
fn peer_addr(&self) -> Option<SocketAddr> {
TcpStream::peer_addr(self).ok()
}
}
#[cfg(feature = "rustls")]
impl Connection for TlsStream<TcpStream, ServerSession> {
fn peer_addr(&self) -> Option<SocketAddr> {
TcpStream::peer_addr(self.get_ref().0).ok()
}
}
impl<T> ConnectionStream for T
where
T: Stream<Error = io::Error>,
T::Item: Connection,
{
type Item = <Self as Stream>::Item;
fn poll_next(&mut self) -> Poll<Option<Self::Item>, io::Error> {
self.poll()
}
}
#[derive(Debug)]
pub(crate) struct Lift<T>(pub(crate) T);
impl<T: ConnectionStream> Stream for Lift<T> {
type Item = <T as ConnectionStream>::Item;
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.0.poll_next()
}
}