Skip to content

Commit

Permalink
Don't project if you require Unpin.
Browse files Browse the repository at this point in the history
This doesn't make sense. For Unpin types, Pin does nothing, so Pin::new() is sufficient.

However Pin::new is only implemented for `Pin<P> where <P as Deref>::Target: Unpin`, so if it weren't Unpin,
you would have to use unsafe { Pin::new_unchecked(s) }, `Pin::new()` wouldn't compile.

Thus project and Pin::new together is always a code smell.
  • Loading branch information
najamelan authored and sdroege committed Mar 10, 2020
1 parent a65c53b commit bfbb940
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
13 changes: 5 additions & 8 deletions src/handshake.rs
Expand Up @@ -2,7 +2,6 @@ use crate::compat::{AllowStd, SetWaker};
use crate::WebSocketStream;
use futures::io::{AsyncRead, AsyncWrite};
use log::*;
use pin_project::pin_project;
use std::future::Future;
use std::io::{Read, Write};
use std::pin::Pin;
Expand Down Expand Up @@ -51,7 +50,6 @@ where
}
}

#[pin_project]
struct MidHandshake<Role: HandshakeRole>(Option<WsHandshake<Role>>);

enum StartedHandshake<Role: HandshakeRole> {
Expand All @@ -68,7 +66,7 @@ struct StartedHandshakeFutureInner<F, S> {
async fn handshake<Role, F, S>(stream: S, f: F) -> Result<Role::FinalResult, Error<Role>>
where
Role: HandshakeRole + Unpin,
Role::InternalStream: SetWaker,
Role::InternalStream: SetWaker + Unpin,
F: FnOnce(AllowStd<S>) -> Result<Role::FinalResult, Error<Role>> + Unpin,
S: AsyncRead + AsyncWrite + Unpin,
{
Expand Down Expand Up @@ -145,13 +143,12 @@ where
impl<Role> Future for MidHandshake<Role>
where
Role: HandshakeRole + Unpin,
Role::InternalStream: SetWaker,
Role::InternalStream: SetWaker + Unpin,
{
type Output = Result<Role::FinalResult, Error<Role>>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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<Self::Output> {
let mut s = self.as_mut().0.take().expect("future polled after completion");

let machine = s.get_mut();
trace!("Setting context in handshake");
Expand All @@ -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
}
}
Expand Down
22 changes: 6 additions & 16 deletions src/stream.rs
Expand Up @@ -3,63 +3,53 @@
//! 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
//! `AsyncRead + AsyncWrite` traits.
use pin_project::{pin_project, project};
use std::pin::Pin;
use std::task::{Context, Poll};

use futures::io::{AsyncRead, AsyncWrite};

/// Stream, either plain TCP or TLS.
#[pin_project]
pub enum Stream<S, T> {
/// Unencrypted socket stream.
Plain(#[pin] S),
Plain(S),
/// Encrypted socket stream.
Tls(#[pin] T),
Tls(T),
}

impl<S: AsyncRead + Unpin, T: AsyncRead + Unpin> AsyncRead for Stream<S, T> {
#[project]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
#[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),
}
}
}

impl<S: AsyncWrite + Unpin, T: AsyncWrite + Unpin> AsyncWrite for Stream<S, T> {
#[project]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, std::io::Error>> {
#[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<Result<(), std::io::Error>> {
#[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_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
#[project]
match self.project() {
match self.get_mut() {
Stream::Plain(ref mut s) => Pin::new(s).poll_close(cx),
Stream::Tls(ref mut s) => Pin::new(s).poll_close(cx),
}
Expand Down

0 comments on commit bfbb940

Please sign in to comment.