Skip to content

Commit

Permalink
Merge pull request #21 from taiki-e/pin-project
Browse files Browse the repository at this point in the history
Remove uses of pin_project::project attribute
  • Loading branch information
dignifiedquire authored Jun 12, 2020
2 parents 2a52a5e + 5031ef6 commit ff9cf1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fast_chemail = "^0.9"
async-native-tls = { version = "0.3.3" }
async-std = { version = "1.6.0", features = ["unstable"] }
async-trait = "0.1.17"
pin-project = "0.4.5"
pin-project = "0.4.17"
pin-utils = "0.1.0-alpha.4"
thiserror = "1.0.9"

Expand Down
50 changes: 18 additions & 32 deletions src/smtp/client/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use async_std::task::{Context, Poll};
use async_trait::async_trait;
#[cfg(feature = "socks5")]
use fast_socks5::client::Socks5Stream;
use pin_project::{pin_project, project};
use pin_project::pin_project;

use crate::smtp::client::mock::MockStream;

Expand Down Expand Up @@ -40,7 +40,7 @@ impl ClientTlsParameters {
}

/// Represents the different types of underlying network streams
#[pin_project]
#[pin_project(project = NetworkStreamProj)]
#[allow(missing_debug_implementations)]
pub enum NetworkStream {
/// Plain TCP stream
Expand Down Expand Up @@ -82,28 +82,26 @@ impl NetworkStream {
}

impl Read for NetworkStream {
#[project]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
#[project]
match self.project() {
NetworkStream::Tcp(s) => {
NetworkStreamProj::Tcp(s) => {
let _: Pin<&mut TcpStream> = s;
s.poll_read(cx, buf)
}
NetworkStream::Tls(s) => {
NetworkStreamProj::Tls(s) => {
let _: Pin<&mut TlsStream<TcpStream>> = s;
s.poll_read(cx, buf)
}
#[cfg(feature = "socks5")]
NetworkStream::Socks5Stream(s) => {
NetworkStreamProj::Socks5Stream(s) => {
let _: Pin<&mut Socks5Stream<TcpStream>> = s;
s.poll_read(cx, buf)
}
NetworkStream::Mock(s) => {
NetworkStreamProj::Mock(s) => {
let _: Pin<&mut MockStream> = s;
s.poll_read(cx, buf)
}
Expand All @@ -112,69 +110,57 @@ impl Read for NetworkStream {
}

impl Write for NetworkStream {
#[project]
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
#[project]
match self.project() {
NetworkStream::Tcp(s) => {
NetworkStreamProj::Tcp(s) => {
let _: Pin<&mut TcpStream> = s;
s.poll_write(cx, buf)
}
NetworkStream::Tls(s) => {
NetworkStreamProj::Tls(s) => {
let _: Pin<&mut TlsStream<TcpStream>> = s;
s.poll_write(cx, buf)
}
#[cfg(feature = "socks5")]
NetworkStream::Socks5Stream(s) => {
s.poll_write(cx, buf)
}
NetworkStream::Mock(s) => {
NetworkStreamProj::Socks5Stream(s) => s.poll_write(cx, buf),
NetworkStreamProj::Mock(s) => {
let _: Pin<&mut MockStream> = s;
s.poll_write(cx, buf)
}
}
}

#[project]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
#[project]
match self.project() {
NetworkStream::Tcp(s) => {
NetworkStreamProj::Tcp(s) => {
let _: Pin<&mut TcpStream> = s;
s.poll_flush(cx)
}
NetworkStream::Tls(s) => {
NetworkStreamProj::Tls(s) => {
let _: Pin<&mut TlsStream<TcpStream>> = s;
s.poll_flush(cx)
}
#[cfg(feature = "socks5")]
NetworkStream::Socks5Stream(s) => {
s.poll_flush(cx)
}
NetworkStream::Mock(s) => {
NetworkStreamProj::Socks5Stream(s) => s.poll_flush(cx),
NetworkStreamProj::Mock(s) => {
let _: Pin<&mut MockStream> = s;
s.poll_flush(cx)
}
}
}

#[project]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
#[project]
match self.project() {
NetworkStream::Tcp(s) => {
NetworkStreamProj::Tcp(s) => {
let _: Pin<&mut TcpStream> = s;
s.poll_close(cx)
}
NetworkStream::Tls(s) => {
NetworkStreamProj::Tls(s) => {
let _: Pin<&mut TlsStream<TcpStream>> = s;
s.poll_close(cx)
}
#[cfg(feature = "socks5")]
NetworkStream::Socks5Stream(s) => {
s.poll_close(cx)
}
NetworkStream::Mock(s) => {
NetworkStreamProj::Socks5Stream(s) => s.poll_close(cx),
NetworkStreamProj::Mock(s) => {
let _: Pin<&mut MockStream> = s;
s.poll_close(cx)
}
Expand Down
10 changes: 4 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use async_std::pin::Pin;
use async_std::prelude::*;
use async_std::task::{Context, Poll};
use fast_chemail::is_valid_email;
use pin_project::{pin_project, project};
use pin_project::pin_project;

use crate::error::EmailResult;
use crate::error::Error;
Expand Down Expand Up @@ -96,29 +96,27 @@ impl Envelope {
}
}

#[pin_project]
#[pin_project(project = MessageProj)]
#[allow(missing_debug_implementations)]
pub enum Message {
Reader(#[pin] Box<dyn Read + Send + Sync>),
Bytes(#[pin] Cursor<Vec<u8>>),
}

impl Read for Message {
#[project]
#[allow(unsafe_code)]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
#[project]
match self.project() {
Message::Reader(mut rdr) => {
MessageProj::Reader(mut rdr) => {
// Probably safe..
let r: Pin<&mut _> = unsafe { Pin::new_unchecked(&mut **rdr) };
r.poll_read(cx, buf)
}
Message::Bytes(rdr) => {
MessageProj::Bytes(rdr) => {
let _: Pin<&mut _> = rdr;
rdr.poll_read(cx, buf)
}
Expand Down

0 comments on commit ff9cf1e

Please sign in to comment.