From b40114e54c786ee9f2251cdd9632ecc58d58fdd7 Mon Sep 17 00:00:00 2001 From: Jens Reidel Date: Sat, 16 Oct 2021 20:00:06 +0200 Subject: [PATCH] Re-port #90 (#194) * Don't project if you require Unpin Co-authored-by: Jens Reidel * remove async-await feature on futures-util Co-authored-by: Naja Melan --- Cargo.toml | 3 +-- src/handshake.rs | 15 ++++++--------- src/stream.rs | 36 +++++++++++++++++------------------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3b5d9d1..ec5961e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,7 @@ stream = [] [dependencies] log = "0.4" -futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] } -pin-project = "1.0" +futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] } tokio = { version = "1.0.0", default-features = false, features = ["io-util"] } [dependencies.tungstenite] diff --git a/src/handshake.rs b/src/handshake.rs index a0c52df..b237748 100644 --- a/src/handshake.rs +++ b/src/handshake.rs @@ -3,7 +3,6 @@ use crate::{ WebSocketStream, }; use log::*; -use pin_project::pin_project; use std::{ future::Future, io::{Read, Write}, @@ -54,7 +53,6 @@ where } } -#[pin_project] struct MidHandshake(Option>); enum StartedHandshake { @@ -71,7 +69,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, { @@ -125,7 +123,7 @@ where impl Future for StartedHandshakeFuture where Role: HandshakeRole, - Role::InternalStream: SetWaker, + Role::InternalStream: SetWaker + Unpin, F: FnOnce(AllowStd) -> Result> + Unpin, S: Unpin, AllowStd: Read + Write, @@ -148,13 +146,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"); @@ -164,7 +161,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 } } diff --git a/src/stream.rs b/src/stream.rs index 4db3729..18affc3 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -3,7 +3,6 @@ //! 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; use std::{ pin::Pin, task::{Context, Poll}, @@ -13,11 +12,10 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; /// A stream that might be protected with TLS. #[non_exhaustive] -#[pin_project(project = StreamProj)] #[derive(Debug)] pub enum MaybeTlsStream { /// Unencrypted socket stream. - Plain(#[pin] S), + Plain(S), /// Encrypted socket stream using `native-tls`. #[cfg(feature = "native-tls")] NativeTls(tokio_native_tls::TlsStream), @@ -32,12 +30,12 @@ impl AsyncRead for MaybeTlsStream { cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll> { - match self.project() { - StreamProj::Plain(ref mut s) => Pin::new(s).poll_read(cx, buf), + match self.get_mut() { + MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_read(cx, buf), #[cfg(feature = "native-tls")] - StreamProj::NativeTls(s) => Pin::new(s).poll_read(cx, buf), + MaybeTlsStream::NativeTls(s) => Pin::new(s).poll_read(cx, buf), #[cfg(feature = "__rustls-tls")] - StreamProj::Rustls(s) => Pin::new(s).poll_read(cx, buf), + MaybeTlsStream::Rustls(s) => Pin::new(s).poll_read(cx, buf), } } } @@ -48,22 +46,22 @@ impl AsyncWrite for MaybeTlsStream { cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { - match self.project() { - StreamProj::Plain(ref mut s) => Pin::new(s).poll_write(cx, buf), + match self.get_mut() { + MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_write(cx, buf), #[cfg(feature = "native-tls")] - StreamProj::NativeTls(s) => Pin::new(s).poll_write(cx, buf), + MaybeTlsStream::NativeTls(s) => Pin::new(s).poll_write(cx, buf), #[cfg(feature = "__rustls-tls")] - StreamProj::Rustls(s) => Pin::new(s).poll_write(cx, buf), + MaybeTlsStream::Rustls(s) => Pin::new(s).poll_write(cx, buf), } } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - match self.project() { - StreamProj::Plain(ref mut s) => Pin::new(s).poll_flush(cx), + match self.get_mut() { + MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_flush(cx), #[cfg(feature = "native-tls")] - StreamProj::NativeTls(s) => Pin::new(s).poll_flush(cx), + MaybeTlsStream::NativeTls(s) => Pin::new(s).poll_flush(cx), #[cfg(feature = "__rustls-tls")] - StreamProj::Rustls(s) => Pin::new(s).poll_flush(cx), + MaybeTlsStream::Rustls(s) => Pin::new(s).poll_flush(cx), } } @@ -71,12 +69,12 @@ impl AsyncWrite for MaybeTlsStream { self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll> { - match self.project() { - StreamProj::Plain(ref mut s) => Pin::new(s).poll_shutdown(cx), + match self.get_mut() { + MaybeTlsStream::Plain(ref mut s) => Pin::new(s).poll_shutdown(cx), #[cfg(feature = "native-tls")] - StreamProj::NativeTls(s) => Pin::new(s).poll_shutdown(cx), + MaybeTlsStream::NativeTls(s) => Pin::new(s).poll_shutdown(cx), #[cfg(feature = "__rustls-tls")] - StreamProj::Rustls(s) => Pin::new(s).poll_shutdown(cx), + MaybeTlsStream::Rustls(s) => Pin::new(s).poll_shutdown(cx), } } }