Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't project if you require Unpin. #90

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -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]
Expand Down
13 changes: 5 additions & 8 deletions 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;
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,66 +3,56 @@
//! 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<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_shutdown(
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_shutdown(cx),
Stream::Tls(ref mut s) => Pin::new(s).poll_shutdown(cx),
}
Expand Down