From 35f6ec6b3c852a516540fb473dd4dbd5f535504a Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Tue, 19 May 2020 10:57:01 +0200 Subject: [PATCH 1/2] get it actually to compile --- src/upgrade/connection.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/upgrade/connection.rs b/src/upgrade/connection.rs index d8331a57..a46edc48 100644 --- a/src/upgrade/connection.rs +++ b/src/upgrade/connection.rs @@ -1,42 +1,49 @@ use async_std::io::{self, prelude::*}; +use std::fmt; use std::pin::Pin; use std::task::{Context, Poll}; /// An upgraded HTTP connection. -#[derive(Debug, Clone)] pub struct Connection { - inner: Pin>, + inner: Pin>, } +pub(crate) trait InnerConnection: Read + Write + Send + Sync + Unpin {} +impl InnerConnection for T {} + impl Read for Connection { fn poll_read( - self: Pin<&mut Self>, + mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll> { - 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 { fn poll_write( - self: Pin<&mut Self>, + mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { - let this = self.project(); - Pin::new(this.inner).poll_write(cx, buf) + Pin::new(&mut self.inner).poll_write(cx, buf) + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.inner).poll_flush(cx) } - fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let this = self.project(); - Pin::new(this.inner).poll_flush(cx) + fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.inner).poll_close(cx) } +} - fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let this = self.project(); - Pin::new(this.inner).poll_close(cx) +impl fmt::Debug for Connection { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Connection") + .field("inner", &"Pin>") + .finish() } } From d1416d1a17df7d3fbbad5b7db9273aec9a23f503 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Tue, 19 May 2020 11:03:15 +0200 Subject: [PATCH 2/2] improve structure using generic --- src/upgrade/connection.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/upgrade/connection.rs b/src/upgrade/connection.rs index a46edc48..abd47ec6 100644 --- a/src/upgrade/connection.rs +++ b/src/upgrade/connection.rs @@ -1,18 +1,22 @@ use async_std::io::{self, prelude::*}; -use std::fmt; use std::pin::Pin; use std::task::{Context, Poll}; /// An upgraded HTTP connection. -pub struct Connection { - inner: Pin>, +#[derive(Debug, Clone)] +pub struct RawConnection { + inner: Inner, } -pub(crate) trait InnerConnection: Read + Write + Send + Sync + Unpin {} +/// A boxed upgraded HTTP connection. +pub type Connection = RawConnection>; + +/// Trait to signal the requirements for an underlying connection type. +pub trait InnerConnection: Read + Write + Send + Sync + Unpin {} impl InnerConnection for T {} -impl Read for Connection { +impl Read for RawConnection { fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -22,7 +26,7 @@ impl Read for Connection { } } -impl Write for Connection { +impl Write for RawConnection { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -39,11 +43,3 @@ impl Write for Connection { Pin::new(&mut self.inner).poll_close(cx) } } - -impl fmt::Debug for Connection { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Connection") - .field("inner", &"Pin>") - .finish() - } -}