From 78f28e336bcf3d79ab5dccfbddf7b415ce7084de Mon Sep 17 00:00:00 2001 From: Naja Melan Date: Wed, 4 Mar 2020 14:56:34 +0100 Subject: [PATCH 1/2] Don't project if you require Unpin. This doesn't make sense. For Unpin types, Pin does nothing, so Pin::new() is sufficient and there is no need to project. If it's projected, it's already pinned, so no need to call Pin::new(). Thus project and Pin::new together is always a code smell. --- src/stream.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/stream.rs b/src/stream.rs index ad48769..a0d7366 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -3,30 +3,26 @@ //! There is no dependency on actual TLS implementations. Everything like //! `native_tls` or `openssl` will work as long as there is a TLS stream supporting standard //! `Read + Write` traits. -use pin_project::{pin_project, project}; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::io::{AsyncRead, AsyncWrite}; /// Stream, either plain TCP or TLS. -#[pin_project] pub enum Stream { /// Unencrypted socket stream. - Plain(#[pin] S), + Plain(S), /// Encrypted socket stream. - Tls(#[pin] T), + Tls(T), } impl AsyncRead for Stream { - #[project] fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll> { - #[project] - match self.project() { + match self.get_mut() { Stream::Plain(ref mut s) => Pin::new(s).poll_read(cx, buf), Stream::Tls(ref mut s) => Pin::new(s).poll_read(cx, buf), } @@ -34,35 +30,29 @@ impl AsyncRead for Stream { } impl AsyncWrite for Stream { - #[project] fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { - #[project] - match self.project() { + match self.get_mut() { Stream::Plain(ref mut s) => Pin::new(s).poll_write(cx, buf), Stream::Tls(ref mut s) => Pin::new(s).poll_write(cx, buf), } } - #[project] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - #[project] - match self.project() { + match self.get_mut() { Stream::Plain(ref mut s) => Pin::new(s).poll_flush(cx), Stream::Tls(ref mut s) => Pin::new(s).poll_flush(cx), } } - #[project] fn poll_shutdown( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll> { - #[project] - match self.project() { + match self.get_mut() { Stream::Plain(ref mut s) => Pin::new(s).poll_shutdown(cx), Stream::Tls(ref mut s) => Pin::new(s).poll_shutdown(cx), } From 880022cfc372bde69b68831ad61bc7200de36c7f Mon Sep 17 00:00:00 2001 From: Naja Melan Date: Thu, 5 Mar 2020 13:19:22 +0100 Subject: [PATCH 2/2] Require Role::InternalStream to be Unpin and remove pin-project dependency. --- Cargo.toml | 1 - src/handshake.rs | 13 +++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ebb26c..45ffbca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ stream = [] [dependencies] log = "0.4" futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] } -pin-project = "0.4" tokio = { version = "0.2", default-features = false, features = ["io-util"] } [dependencies.tungstenite] diff --git a/src/handshake.rs b/src/handshake.rs index 8020a61..988cfa9 100644 --- a/src/handshake.rs +++ b/src/handshake.rs @@ -1,7 +1,6 @@ use crate::compat::{AllowStd, SetWaker}; use crate::WebSocketStream; use log::*; -use pin_project::pin_project; use std::future::Future; use std::io::{Read, Write}; use std::pin::Pin; @@ -51,7 +50,6 @@ where } } -#[pin_project] struct MidHandshake(Option>); enum StartedHandshake { @@ -68,7 +66,7 @@ struct StartedHandshakeFutureInner { async fn handshake(stream: S, f: F) -> Result> where Role: HandshakeRole + Unpin, - Role::InternalStream: SetWaker, + Role::InternalStream: SetWaker + Unpin, F: FnOnce(AllowStd) -> Result> + Unpin, S: AsyncRead + AsyncWrite + Unpin, { @@ -145,13 +143,12 @@ where impl Future for MidHandshake where Role: HandshakeRole + Unpin, - Role::InternalStream: SetWaker, + Role::InternalStream: SetWaker + Unpin, { type Output = Result>; - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.project(); - let mut s = this.0.take().expect("future polled after completion"); + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let mut s = self.as_mut().0.take().expect("future polled after completion"); let machine = s.get_mut(); trace!("Setting context in handshake"); @@ -161,7 +158,7 @@ where Ok(stream) => Poll::Ready(Ok(stream)), Err(Error::Failure(e)) => Poll::Ready(Err(Error::Failure(e))), Err(Error::Interrupted(mid)) => { - *this.0 = Some(mid); + self.0 = Some(mid); Poll::Pending } }