Skip to content

Commit

Permalink
Merge pull request #138 from http-rs/upgrade-2
Browse files Browse the repository at this point in the history
Upgrade 2
  • Loading branch information
yoshuawuyts authored May 22, 2020
2 parents e8c7917 + d1416d1 commit 3d61aab
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/upgrade/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,41 @@ use std::task::{Context, Poll};

/// An upgraded HTTP connection.
#[derive(Debug, Clone)]
pub struct Connection {
inner: Pin<Box<dyn Read + Write + Clone + Send + Sync + Unpin + 'static>>,
pub struct RawConnection<Inner> {
inner: Inner,
}

impl Read for Connection {
/// A boxed upgraded HTTP connection.
pub type Connection = RawConnection<Box<dyn InnerConnection + 'static>>;

/// Trait to signal the requirements for an underlying connection type.
pub trait InnerConnection: Read + Write + Send + Sync + Unpin {}
impl<T: Read + Write + Send + Sync + Unpin> InnerConnection for T {}

impl<Inner: Read + Unpin> Read for RawConnection<Inner> {
fn poll_read(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let this = self.project();
Pin::new(this.inner).poll_read(cx, buf)
Pin::new(&mut self.inner).poll_read(cx, buf)
}
}

impl Write for Connection {
impl<Inner: Write + Unpin> Write for RawConnection<Inner> {
fn poll_write(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let this = self.project();
Pin::new(this.inner).poll_write(cx, buf)
Pin::new(&mut self.inner).poll_write(cx, buf)
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let this = self.project();
Pin::new(this.inner).poll_flush(cx)
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.inner).poll_flush(cx)
}

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let this = self.project();
Pin::new(this.inner).poll_close(cx)
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.inner).poll_close(cx)
}
}

0 comments on commit 3d61aab

Please sign in to comment.