Debian Codegraph @

third_party/rust/warp/src/transport.rs

raw
1 use std::io;
2 use std::net::SocketAddr;
3 use std::pin::Pin;
4 use std::task::{Context, Poll};
5
6 use hyper::server::conn::AddrStream;
7 use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
8
9 pub trait Transport: AsyncRead + AsyncWrite {
10 fn remote_addr(&self) -> Option<SocketAddr>;
11 }
12
13 impl Transport for AddrStream {
14 fn remote_addr(&self) -> Option<SocketAddr> {
15 Some(self.remote_addr())
16 }
17 }
18
19 pub(crate) struct LiftIo<T>(pub(crate) T);
20
21 impl<T: AsyncRead + Unpin> AsyncRead for LiftIo<T> {
22 fn poll_read(
23 self: Pin<&mut Self>,
24 cx: &mut Context<'_>,
25 buf: &mut ReadBuf<'_>,
26 ) -> Poll<io::Result<()>> {
27 Pin::new(&mut self.get_mut().0).poll_read(cx, buf)
28 }
29 }
30
31 impl<T: AsyncWrite + Unpin> AsyncWrite for LiftIo<T> {
32 fn poll_write(
33 self: Pin<&mut Self>,
34 cx: &mut Context<'_>,
35 buf: &[u8],
36 ) -> Poll<io::Result<usize>> {
37 Pin::new(&mut self.get_mut().0).poll_write(cx, buf)
38 }
39
40 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
41 Pin::new(&mut self.get_mut().0).poll_flush(cx)
42 }
43
44 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
45 Pin::new(&mut self.get_mut().0).poll_shutdown(cx)
46 }
47 }
48
49 impl<T: AsyncRead + AsyncWrite + Unpin> Transport for LiftIo<T> {
50 fn remote_addr(&self) -> Option<SocketAddr> {
51 None
52 }
53 }