http_types/upgrade/
connection.rs

1use futures_lite::{io, prelude::*};
2
3use std::fmt::{self, Debug};
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7/// An upgraded HTTP connection.
8pub struct Connection {
9    inner: Box<dyn InnerConnection>,
10}
11
12impl Debug for Connection {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        let inner = "Box<dyn Asyncread + AsyncWrite + Send + Sync + Unpin>";
15        f.debug_struct("Connection").field("inner", &inner).finish()
16    }
17}
18
19impl Connection {
20    /// Create a new instance of `Connection`.
21    pub fn new<T>(t: T) -> Self
22    where
23        T: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static,
24    {
25        Self { inner: Box::new(t) }
26    }
27}
28
29/// Trait to signal the requirements for an underlying connection type.
30pub trait InnerConnection: AsyncRead + AsyncWrite + Send + Sync + Unpin {}
31impl<T: AsyncRead + AsyncWrite + Send + Sync + Unpin> InnerConnection for T {}
32
33impl AsyncRead for Connection {
34    fn poll_read(
35        mut self: Pin<&mut Self>,
36        cx: &mut Context<'_>,
37        buf: &mut [u8],
38    ) -> Poll<io::Result<usize>> {
39        Pin::new(&mut self.inner).poll_read(cx, buf)
40    }
41}
42
43impl AsyncWrite for Connection {
44    fn poll_write(
45        mut self: Pin<&mut Self>,
46        cx: &mut Context<'_>,
47        buf: &[u8],
48    ) -> Poll<io::Result<usize>> {
49        Pin::new(&mut self.inner).poll_write(cx, buf)
50    }
51
52    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
53        Pin::new(&mut self.inner).poll_flush(cx)
54    }
55
56    fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
57        Pin::new(&mut self.inner).poll_close(cx)
58    }
59}