From 1a0c344c913adc447f61d9d28136b5ce8fb23df7 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Fri, 25 Dec 2020 22:01:21 +0100 Subject: [PATCH] Update to tokio 1.0 (#529) --- CHANGELOG.md | 2 +- Cargo.toml | 22 +- examples/README.md | 2 +- ...tp_starttls.rs => tokio1_smtp_starttls.rs} | 8 +- ...tokio03_smtp_tls.rs => tokio1_smtp_tls.rs} | 8 +- src/lib.rs | 22 +- src/transport/file/mod.rs | 18 +- src/transport/sendmail/mod.rs | 43 +++- src/transport/smtp/async_transport.rs | 43 ++-- src/transport/smtp/client/async_connection.rs | 6 +- src/transport/smtp/client/async_net.rs | 205 +++++++++--------- src/transport/smtp/client/mod.rs | 8 +- src/transport/smtp/mod.rs | 8 +- 13 files changed, 204 insertions(+), 191 deletions(-) rename examples/{tokio03_smtp_starttls.rs => tokio1_smtp_starttls.rs} (85%) rename examples/{tokio03_smtp_tls.rs => tokio1_smtp_tls.rs} (86%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84e8f16..3877fa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ Several breaking changes were made between 0.9 and 0.10, but changes should be s #### Features -* Add `tokio` 0.2 and 0.3 support +* Add `tokio` 0.2 and 1.0 support * Add `rustls` support * Add `async-std` support * Allow enabling multiple SMTP authentication mechanisms diff --git a/Cargo.toml b/Cargo.toml index 85d9d87..d3d00c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,9 +59,9 @@ async-trait = { version = "0.1", optional = true } tokio02_crate = { package = "tokio", version = "0.2.7", features = ["fs", "process", "tcp", "dns", "io-util"], optional = true } tokio02_native_tls_crate = { package = "tokio-native-tls", version = "0.1", optional = true } tokio02_rustls = { package = "tokio-rustls", version = "0.15", optional = true } -tokio03_crate = { package = "tokio", version = "0.3", features = ["fs", "process", "net", "io-util"], optional = true } -tokio03_native_tls_crate = { package = "tokio-native-tls", version = "0.2", optional = true } -tokio03_rustls = { package = "tokio-rustls", version = "0.21", optional = true } +tokio1_crate = { package = "tokio", version = "1", features = ["fs", "process", "net", "io-util"], optional = true } +tokio1_native_tls_crate = { package = "tokio-native-tls", version = "0.3", optional = true } +tokio1_rustls = { package = "tokio-rustls", version = "0.22", optional = true } [dev-dependencies] criterion = "0.3" @@ -69,7 +69,7 @@ tracing-subscriber = "0.2.10" glob = "0.3" walkdir = "2" tokio02_crate = { package = "tokio", version = "0.2.7", features = ["macros", "rt-threaded"] } -tokio03_crate = { package = "tokio", version = "0.3", features = ["macros", "rt-multi-thread"] } +tokio1_crate = { package = "tokio", version = "1", features = ["macros", "rt-multi-thread"] } serde_json = "1" maud = "0.22.1" @@ -94,9 +94,9 @@ async-std1 = ["async-std", "async-trait", "async-attributes"] tokio02 = ["tokio02_crate", "async-trait", "futures-io", "futures-util"] tokio02-native-tls = ["tokio02", "native-tls", "tokio02_native_tls_crate"] tokio02-rustls-tls = ["tokio02", "rustls-tls", "tokio02_rustls"] -tokio03 = ["tokio03_crate", "async-trait", "futures-io", "futures-util"] -tokio03-native-tls = ["tokio03", "native-tls", "tokio03_native_tls_crate"] -tokio03-rustls-tls = ["tokio03", "rustls-tls", "tokio03_rustls"] +tokio1 = ["tokio1_crate", "async-trait", "futures-io", "futures-util"] +tokio1-native-tls = ["tokio1", "native-tls", "tokio1_native_tls_crate"] +tokio1-rustls-tls = ["tokio1", "rustls-tls", "tokio1_rustls"] [package.metadata.docs.rs] all-features = true @@ -135,10 +135,10 @@ name = "tokio02_smtp_starttls" required-features = ["smtp-transport", "tokio02", "tokio02-native-tls", "builder"] [[example]] -name = "tokio03_smtp_tls" -required-features = ["smtp-transport", "tokio03", "tokio03-native-tls", "builder"] +name = "tokio1_smtp_tls" +required-features = ["smtp-transport", "tokio1", "tokio1-native-tls", "builder"] [[example]] -name = "tokio03_smtp_starttls" -required-features = ["smtp-transport", "tokio03", "tokio03-native-tls", "builder"] +name = "tokio1_smtp_starttls" +required-features = ["smtp-transport", "tokio1", "tokio1-native-tls", "builder"] diff --git a/examples/README.md b/examples/README.md index 0fb0eb6..c4c5e97 100644 --- a/examples/README.md +++ b/examples/README.md @@ -14,7 +14,7 @@ This folder contains examples showing how to use lettre in your own projects. - [smtp_starttls.rs] - Send an email over SMTP with STARTTLS and authenticating with username and password. - [smtp_selfsigned.rs] - Send an email over SMTP encrypted with TLS using a self-signed certificate and authenticating with username and password. - The [smtp_tls.rs] and [smtp_starttls.rs] examples also feature `async`hronous implementations powered by [Tokio](https://tokio.rs/). - These files are prefixed with `tokio02_` or `tokio03_`. + These files are prefixed with `tokio02_` or `tokio1_`. [basic_html.rs]: ./basic_html.rs [maud_html.rs]: ./maud_html.rs diff --git a/examples/tokio03_smtp_starttls.rs b/examples/tokio1_smtp_starttls.rs similarity index 85% rename from examples/tokio03_smtp_starttls.rs rename to examples/tokio1_smtp_starttls.rs index 4dc4e4b..1d43655 100644 --- a/examples/tokio03_smtp_starttls.rs +++ b/examples/tokio1_smtp_starttls.rs @@ -1,11 +1,11 @@ // This line is only to make it compile from lettre's examples folder, // since it uses Rust 2018 crate renaming to import tokio. // Won't be needed in user's code. -use tokio03_crate as tokio; +use tokio1_crate as tokio; use lettre::{ - transport::smtp::authentication::Credentials, AsyncSmtpTransport, Message, Tokio03Connector, - Tokio03Transport, + transport::smtp::authentication::Credentials, AsyncSmtpTransport, Message, Tokio1Connector, + Tokio1Transport, }; #[tokio::main] @@ -23,7 +23,7 @@ async fn main() { let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); // Open a remote connection to gmail using STARTTLS - let mailer = AsyncSmtpTransport::::starttls_relay("smtp.gmail.com") + let mailer = AsyncSmtpTransport::::starttls_relay("smtp.gmail.com") .unwrap() .credentials(creds) .build(); diff --git a/examples/tokio03_smtp_tls.rs b/examples/tokio1_smtp_tls.rs similarity index 86% rename from examples/tokio03_smtp_tls.rs rename to examples/tokio1_smtp_tls.rs index 8d13071..60addbd 100644 --- a/examples/tokio03_smtp_tls.rs +++ b/examples/tokio1_smtp_tls.rs @@ -1,11 +1,11 @@ // This line is only to make it compile from lettre's examples folder, // since it uses Rust 2018 crate renaming to import tokio. // Won't be needed in user's code. -use tokio03_crate as tokio; +use tokio1_crate as tokio; use lettre::{ - transport::smtp::authentication::Credentials, AsyncSmtpTransport, Message, Tokio03Connector, - Tokio03Transport, + transport::smtp::authentication::Credentials, AsyncSmtpTransport, Message, Tokio1Connector, + Tokio1Transport, }; #[tokio::main] @@ -23,7 +23,7 @@ async fn main() { let creds = Credentials::new("smtp_username".to_string(), "smtp_password".to_string()); // Open a remote connection to gmail - let mailer = AsyncSmtpTransport::::relay("smtp.gmail.com") + let mailer = AsyncSmtpTransport::::relay("smtp.gmail.com") .unwrap() .credentials(creds) .build(); diff --git a/src/lib.rs b/src/lib.rs index f7364e0..ec9a75f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,9 +19,9 @@ //! * **tokio02**: Allow to asyncronously send emails using tokio 0.2.x //! * **tokio02-rustls-tls**: Async TLS support with the `rustls` crate using tokio 0.2 //! * **tokio02-native-tls**: Async TLS support with the `native-tls` crate using tokio 0.2 -//! * **tokio03**: Allow to asyncronously send emails using tokio 0.3.x -//! * **tokio03-rustls-tls**: Async TLS support with the `rustls` crate using tokio 0.3 -//! * **tokio03-native-tls**: Async TLS support with the `native-tls` crate using tokio 0.3 +//! * **tokio1**: Allow to asyncronously send emails using tokio 1.x +//! * **tokio1-rustls-tls**: Async TLS support with the `rustls` crate using tokio 1.x +//! * **tokio1-native-tls**: Async TLS support with the `native-tls` crate using tokio 1.x //! * **async-std1**: Allow to asynchronously send emails using async-std 1.x (SMTP isn't supported yet) //! * **r2d2**: Connection pool for SMTP transport //! * **tracing**: Logging using the `tracing` crate @@ -64,16 +64,16 @@ pub use crate::transport::file::FileTransport; pub use crate::transport::sendmail::SendmailTransport; #[cfg(all( feature = "smtp-transport", - any(feature = "tokio02", feature = "tokio03") + any(feature = "tokio02", feature = "tokio1") ))] pub use crate::transport::smtp::AsyncSmtpTransport; #[cfg(feature = "smtp-transport")] pub use crate::transport::smtp::SmtpTransport; #[cfg(all(feature = "smtp-transport", feature = "tokio02"))] pub use crate::transport::smtp::Tokio02Connector; -#[cfg(all(feature = "smtp-transport", feature = "tokio03"))] -pub use crate::transport::smtp::Tokio03Connector; -#[cfg(any(feature = "async-std1", feature = "tokio02", feature = "tokio03"))] +#[cfg(all(feature = "smtp-transport", feature = "tokio1"))] +pub use crate::transport::smtp::Tokio1Connector; +#[cfg(any(feature = "async-std1", feature = "tokio02", feature = "tokio1"))] use async_trait::async_trait; /// Blocking Transport method for emails @@ -140,11 +140,11 @@ pub trait Tokio02Transport { async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result; } -/// tokio 0.3.x based Transport method for emails -#[cfg(feature = "tokio03")] -#[cfg_attr(docsrs, doc(cfg(feature = "tokio03")))] +/// tokio 1.x based Transport method for emails +#[cfg(feature = "tokio1")] +#[cfg_attr(docsrs, doc(cfg(feature = "tokio1")))] #[async_trait] -pub trait Tokio03Transport { +pub trait Tokio1Transport { /// Response produced by the Transport type Ok; /// Error produced by the Transport diff --git a/src/transport/file/mod.rs b/src/transport/file/mod.rs index f9ed1bb..7ac3347 100644 --- a/src/transport/file/mod.rs +++ b/src/transport/file/mod.rs @@ -62,15 +62,15 @@ //! # fn main() {} //! ``` //! -//! ## Async tokio 0.2 +//! ## Async tokio 1.x //! //! ```rust,no_run //! # use std::error::Error; //! -//! # #[cfg(all(feature = "tokio02", feature = "file-transport", feature = "builder"))] +//! # #[cfg(all(feature = "tokio1", feature = "file-transport", feature = "builder"))] //! # async fn run() -> Result<(), Box> { //! use std::env::temp_dir; -//! use lettre::{Tokio02Transport, Message, FileTransport}; +//! use lettre::{Tokio1Transport, Message, FileTransport}; //! //! // Write to the local temp directory //! let sender = FileTransport::new(temp_dir()); @@ -138,10 +138,10 @@ use crate::address::Envelope; use crate::AsyncStd1Transport; #[cfg(feature = "tokio02")] use crate::Tokio02Transport; -#[cfg(feature = "tokio03")] -use crate::Tokio03Transport; +#[cfg(feature = "tokio1")] +use crate::Tokio1Transport; use crate::Transport; -#[cfg(any(feature = "async-std1", feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "async-std1", feature = "tokio02", feature = "tokio1"))] use async_trait::async_trait; use std::{ path::{Path, PathBuf}, @@ -273,14 +273,14 @@ impl Tokio02Transport for FileTransport { } } -#[cfg(feature = "tokio03")] +#[cfg(feature = "tokio1")] #[async_trait] -impl Tokio03Transport for FileTransport { +impl Tokio1Transport for FileTransport { type Ok = Id; type Error = Error; async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result { - use tokio03_crate::fs; + use tokio1_crate::fs; let email_id = Uuid::new_v4(); diff --git a/src/transport/sendmail/mod.rs b/src/transport/sendmail/mod.rs index b21952f..513c1f7 100644 --- a/src/transport/sendmail/mod.rs +++ b/src/transport/sendmail/mod.rs @@ -49,6 +49,29 @@ //! # } //! ``` //! +//! ## Async tokio 1.x example +//! +//! ```rust,no_run +//! # use std::error::Error; +//! +//! # #[cfg(all(feature = "tokio1", feature = "sendmail-transport", feature = "builder"))] +//! # async fn run() -> Result<(), Box> { +//! use lettre::{Message, Tokio1Transport, SendmailTransport}; +//! +//! let email = Message::builder() +//! .from("NoBody ".parse()?) +//! .reply_to("Yuin ".parse()?) +//! .to("Hei ".parse()?) +//! .subject("Happy new year") +//! .body(String::from("Be happy!"))?; +//! +//! let sender = SendmailTransport::new(); +//! let result = sender.send(email).await; +//! assert!(result.is_ok()); +//! # Ok(()) +//! # } +//! ``` +//! //! ## Async async-std 1.x example //! //!```rust,no_run @@ -78,10 +101,10 @@ use crate::address::Envelope; use crate::AsyncStd1Transport; #[cfg(feature = "tokio02")] use crate::Tokio02Transport; -#[cfg(feature = "tokio03")] -use crate::Tokio03Transport; +#[cfg(feature = "tokio1")] +use crate::Tokio1Transport; use crate::Transport; -#[cfg(any(feature = "async-std1", feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "async-std1", feature = "tokio02", feature = "tokio1"))] use async_trait::async_trait; use std::{ ffi::OsString, @@ -147,9 +170,9 @@ impl SendmailTransport { c } - #[cfg(feature = "tokio03")] - fn tokio03_command(&self, envelope: &Envelope) -> tokio03_crate::process::Command { - use tokio03_crate::process::Command; + #[cfg(feature = "tokio1")] + fn tokio1_command(&self, envelope: &Envelope) -> tokio1_crate::process::Command { + use tokio1_crate::process::Command; let mut c = Command::new(&self.command); c.kill_on_drop(true); @@ -260,16 +283,16 @@ impl Tokio02Transport for SendmailTransport { } } -#[cfg(feature = "tokio03")] +#[cfg(feature = "tokio1")] #[async_trait] -impl Tokio03Transport for SendmailTransport { +impl Tokio1Transport for SendmailTransport { type Ok = (); type Error = Error; async fn send_raw(&self, envelope: &Envelope, email: &[u8]) -> Result { - use tokio03_crate::io::AsyncWriteExt; + use tokio1_crate::io::AsyncWriteExt; - let mut command = self.tokio03_command(envelope); + let mut command = self.tokio1_command(envelope); // Spawn the sendmail command let mut process = command.spawn()?; diff --git a/src/transport/smtp/async_transport.rs b/src/transport/smtp/async_transport.rs index 9ac96da..9116dbc 100644 --- a/src/transport/smtp/async_transport.rs +++ b/src/transport/smtp/async_transport.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -#[cfg(any(feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "tokio02", feature = "tokio1"))] use super::Tls; use super::{ client::AsyncSmtpConnection, ClientId, Credentials, Error, Mechanism, Response, SmtpInfo, @@ -8,8 +8,8 @@ use super::{ use crate::Envelope; #[cfg(feature = "tokio02")] use crate::Tokio02Transport; -#[cfg(feature = "tokio03")] -use crate::Tokio03Transport; +#[cfg(feature = "tokio1")] +use crate::Tokio1Transport; #[allow(missing_debug_implementations)] #[derive(Clone)] @@ -36,9 +36,9 @@ impl Tokio02Transport for AsyncSmtpTransport { } } -#[cfg(feature = "tokio03")] +#[cfg(feature = "tokio1")] #[async_trait] -impl Tokio03Transport for AsyncSmtpTransport { +impl Tokio1Transport for AsyncSmtpTransport { type Ok = Response; type Error = Error; @@ -67,8 +67,8 @@ where #[cfg(any( feature = "tokio02-native-tls", feature = "tokio02-rustls-tls", - feature = "tokio03-native-tls", - feature = "tokio03-rustls-tls" + feature = "tokio1-native-tls", + feature = "tokio1-rustls-tls" ))] pub fn relay(relay: &str) -> Result { use super::{TlsParameters, SUBMISSIONS_PORT}; @@ -94,8 +94,8 @@ where #[cfg(any( feature = "tokio02-native-tls", feature = "tokio02-rustls-tls", - feature = "tokio03-native-tls", - feature = "tokio03-rustls-tls" + feature = "tokio1-native-tls", + feature = "tokio1-rustls-tls" ))] pub fn starttls_relay(relay: &str) -> Result { use super::{TlsParameters, SUBMISSION_PORT}; @@ -170,8 +170,8 @@ impl AsyncSmtpTransportBuilder { #[cfg(any( feature = "tokio02-native-tls", feature = "tokio02-rustls-tls", - feature = "tokio03-native-tls", - feature = "tokio03-rustls-tls" + feature = "tokio1-native-tls", + feature = "tokio1-rustls-tls" ))] pub fn tls(mut self, tls: Tls) -> Self { self.info.tls = tls; @@ -275,13 +275,13 @@ impl AsyncSmtpConnector for Tokio02Connector { } #[derive(Debug, Copy, Clone, Default)] -#[cfg(feature = "tokio03")] -#[cfg_attr(docsrs, doc(cfg(feature = "tokio03")))] -pub struct Tokio03Connector; +#[cfg(feature = "tokio1")] +#[cfg_attr(docsrs, doc(cfg(feature = "tokio1")))] +pub struct Tokio1Connector; #[async_trait] -#[cfg(feature = "tokio03")] -impl AsyncSmtpConnector for Tokio03Connector { +#[cfg(feature = "tokio1")] +impl AsyncSmtpConnector for Tokio1Connector { async fn connect( hostname: &str, port: u16, @@ -290,16 +290,15 @@ impl AsyncSmtpConnector for Tokio03Connector { ) -> Result { #[allow(clippy::match_single_binding)] let tls_parameters = match tls { - #[cfg(any(feature = "tokio03-native-tls", feature = "tokio03-rustls-tls"))] + #[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))] Tls::Wrapper(ref tls_parameters) => Some(tls_parameters.clone()), _ => None, }; #[allow(unused_mut)] let mut conn = - AsyncSmtpConnection::connect_tokio03(hostname, port, hello_name, tls_parameters) - .await?; + AsyncSmtpConnection::connect_tokio1(hostname, port, hello_name, tls_parameters).await?; - #[cfg(any(feature = "tokio03-native-tls", feature = "tokio03-rustls-tls"))] + #[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))] match tls { Tls::Opportunistic(ref tls_parameters) => { if conn.can_starttls() { @@ -324,6 +323,6 @@ mod private { #[cfg(feature = "tokio02")] impl Sealed for Tokio02Connector {} - #[cfg(feature = "tokio03")] - impl Sealed for Tokio03Connector {} + #[cfg(feature = "tokio1")] + impl Sealed for Tokio1Connector {} } diff --git a/src/transport/smtp/client/async_connection.rs b/src/transport/smtp/client/async_connection.rs index 6b42996..c217cdc 100644 --- a/src/transport/smtp/client/async_connection.rs +++ b/src/transport/smtp/client/async_connection.rs @@ -62,14 +62,14 @@ impl AsyncSmtpConnection { /// Connects to the configured server /// /// Sends EHLO and parses server information - #[cfg(feature = "tokio03")] - pub async fn connect_tokio03( + #[cfg(feature = "tokio1")] + pub async fn connect_tokio1( hostname: &str, port: u16, hello_name: &ClientId, tls_parameters: Option, ) -> Result { - let stream = AsyncNetworkStream::connect_tokio03(hostname, port, tls_parameters).await?; + let stream = AsyncNetworkStream::connect_tokio1(hostname, port, tls_parameters).await?; Self::connect_impl(stream, hello_name).await } diff --git a/src/transport/smtp/client/async_net.rs b/src/transport/smtp/client/async_net.rs index c565172..d56249a 100644 --- a/src/transport/smtp/client/async_net.rs +++ b/src/transport/smtp/client/async_net.rs @@ -1,7 +1,7 @@ -#[cfg(any(feature = "tokio02-rustls-tls", feature = "tokio03-rustls-tls"))] +#[cfg(any(feature = "tokio02-rustls-tls", feature = "tokio1-rustls-tls"))] use std::sync::Arc; use std::{ - net::{Shutdown, SocketAddr}, + net::SocketAddr, pin::Pin, task::{Context, Poll}, }; @@ -11,26 +11,26 @@ use futures_io::{Error as IoError, ErrorKind, Result as IoResult}; use tokio02_crate::io::{AsyncRead as _, AsyncWrite as _}; #[cfg(feature = "tokio02")] use tokio02_crate::net::TcpStream as Tokio02TcpStream; -#[cfg(feature = "tokio03")] -use tokio03_crate::io::{AsyncRead as _, AsyncWrite as _, ReadBuf as Tokio03ReadBuf}; -#[cfg(feature = "tokio03")] -use tokio03_crate::net::TcpStream as Tokio03TcpStream; +#[cfg(feature = "tokio1")] +use tokio1_crate::io::{AsyncRead as _, AsyncWrite as _, ReadBuf as Tokio1ReadBuf}; +#[cfg(feature = "tokio1")] +use tokio1_crate::net::TcpStream as Tokio1TcpStream; #[cfg(feature = "tokio02-native-tls")] use tokio02_native_tls_crate::TlsStream as Tokio02TlsStream; -#[cfg(feature = "tokio03-native-tls")] -use tokio03_native_tls_crate::TlsStream as Tokio03TlsStream; +#[cfg(feature = "tokio1-native-tls")] +use tokio1_native_tls_crate::TlsStream as Tokio1TlsStream; #[cfg(feature = "tokio02-rustls-tls")] use tokio02_rustls::client::TlsStream as Tokio02RustlsTlsStream; -#[cfg(feature = "tokio03-rustls-tls")] -use tokio03_rustls::client::TlsStream as Tokio03RustlsTlsStream; +#[cfg(feature = "tokio1-rustls-tls")] +use tokio1_rustls::client::TlsStream as Tokio1RustlsTlsStream; #[cfg(any( feature = "tokio02-native-tls", feature = "tokio02-rustls-tls", - feature = "tokio03-native-tls", - feature = "tokio03-rustls-tls" + feature = "tokio1-native-tls", + feature = "tokio1-rustls-tls" ))] use super::InnerTlsParameters; use super::TlsParameters; @@ -56,15 +56,15 @@ enum InnerAsyncNetworkStream { /// Encrypted Tokio 0.2 TCP stream #[cfg(feature = "tokio02-rustls-tls")] Tokio02RustlsTls(Tokio02RustlsTlsStream), - /// Plain Tokio 0.3 TCP stream - #[cfg(feature = "tokio03")] - Tokio03Tcp(Tokio03TcpStream), - /// Encrypted Tokio 0.3 TCP stream - #[cfg(feature = "tokio03-native-tls")] - Tokio03NativeTls(Tokio03TlsStream), - /// Encrypted Tokio 0.3 TCP stream - #[cfg(feature = "tokio03-rustls-tls")] - Tokio03RustlsTls(Tokio03RustlsTlsStream), + /// Plain Tokio 1.x TCP stream + #[cfg(feature = "tokio1")] + Tokio1Tcp(Tokio1TcpStream), + /// Encrypted Tokio 1.x TCP stream + #[cfg(feature = "tokio1-native-tls")] + Tokio1NativeTls(Tokio1TlsStream), + /// Encrypted Tokio 1.x TCP stream + #[cfg(feature = "tokio1-rustls-tls")] + Tokio1RustlsTls(Tokio1RustlsTlsStream), /// Can't be built None, } @@ -89,14 +89,14 @@ impl AsyncNetworkStream { } #[cfg(feature = "tokio02-rustls-tls")] InnerAsyncNetworkStream::Tokio02RustlsTls(ref s) => s.get_ref().0.peer_addr(), - #[cfg(feature = "tokio03")] - InnerAsyncNetworkStream::Tokio03Tcp(ref s) => s.peer_addr(), - #[cfg(feature = "tokio03-native-tls")] - InnerAsyncNetworkStream::Tokio03NativeTls(ref s) => { + #[cfg(feature = "tokio1")] + InnerAsyncNetworkStream::Tokio1Tcp(ref s) => s.peer_addr(), + #[cfg(feature = "tokio1-native-tls")] + InnerAsyncNetworkStream::Tokio1NativeTls(ref s) => { s.get_ref().get_ref().get_ref().peer_addr() } - #[cfg(feature = "tokio03-rustls-tls")] - InnerAsyncNetworkStream::Tokio03RustlsTls(ref s) => s.get_ref().0.peer_addr(), + #[cfg(feature = "tokio1-rustls-tls")] + InnerAsyncNetworkStream::Tokio1RustlsTls(ref s) => s.get_ref().0.peer_addr(), InnerAsyncNetworkStream::None => { debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); Err(IoError::new( @@ -107,32 +107,6 @@ impl AsyncNetworkStream { } } - /// Shutdowns the connection - pub fn shutdown(&self, how: Shutdown) -> IoResult<()> { - match self.inner { - #[cfg(feature = "tokio02")] - InnerAsyncNetworkStream::Tokio02Tcp(ref s) => s.shutdown(how), - #[cfg(feature = "tokio02-native-tls")] - InnerAsyncNetworkStream::Tokio02NativeTls(ref s) => { - s.get_ref().get_ref().get_ref().shutdown(how) - } - #[cfg(feature = "tokio02-rustls-tls")] - InnerAsyncNetworkStream::Tokio02RustlsTls(ref s) => s.get_ref().0.shutdown(how), - #[cfg(feature = "tokio03")] - InnerAsyncNetworkStream::Tokio03Tcp(ref s) => s.shutdown(how), - #[cfg(feature = "tokio03-native-tls")] - InnerAsyncNetworkStream::Tokio03NativeTls(ref s) => { - s.get_ref().get_ref().get_ref().shutdown(how) - } - #[cfg(feature = "tokio03-rustls-tls")] - InnerAsyncNetworkStream::Tokio03RustlsTls(ref s) => s.get_ref().0.shutdown(how), - InnerAsyncNetworkStream::None => { - debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); - Ok(()) - } - } - } - #[cfg(feature = "tokio02")] pub async fn connect_tokio02( hostname: &str, @@ -148,15 +122,15 @@ impl AsyncNetworkStream { Ok(stream) } - #[cfg(feature = "tokio03")] - pub async fn connect_tokio03( + #[cfg(feature = "tokio1")] + pub async fn connect_tokio1( hostname: &str, port: u16, tls_parameters: Option, ) -> Result { - let tcp_stream = Tokio03TcpStream::connect((hostname, port)).await?; + let tcp_stream = Tokio1TcpStream::connect((hostname, port)).await?; - let mut stream = AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio03Tcp(tcp_stream)); + let mut stream = AsyncNetworkStream::new(InnerAsyncNetworkStream::Tokio1Tcp(tcp_stream)); if let Some(tls_parameters) = tls_parameters { stream.upgrade_tls(tls_parameters).await?; } @@ -187,24 +161,24 @@ impl AsyncNetworkStream { Ok(()) } #[cfg(all( - feature = "tokio03", - not(any(feature = "tokio03-native-tls", feature = "tokio03-rustls-tls")) + feature = "tokio1", + not(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls")) ))] - InnerAsyncNetworkStream::Tokio03Tcp(_) => { + InnerAsyncNetworkStream::Tokio1Tcp(_) => { let _ = tls_parameters; - panic!("Trying to upgrade an AsyncNetworkStream without having enabled either the tokio03-native-tls or the tokio03-rustls-tls feature"); + panic!("Trying to upgrade an AsyncNetworkStream without having enabled either the tokio1-native-tls or the tokio1-rustls-tls feature"); } - #[cfg(any(feature = "tokio03-native-tls", feature = "tokio03-rustls-tls"))] - InnerAsyncNetworkStream::Tokio03Tcp(_) => { + #[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))] + InnerAsyncNetworkStream::Tokio1Tcp(_) => { // get owned TcpStream let tcp_stream = std::mem::replace(&mut self.inner, InnerAsyncNetworkStream::None); let tcp_stream = match tcp_stream { - InnerAsyncNetworkStream::Tokio03Tcp(tcp_stream) => tcp_stream, + InnerAsyncNetworkStream::Tokio1Tcp(tcp_stream) => tcp_stream, _ => unreachable!(), }; - self.inner = Self::upgrade_tokio03_tls(tcp_stream, tls_parameters).await?; + self.inner = Self::upgrade_tokio1_tls(tcp_stream, tls_parameters).await?; Ok(()) } _ => Ok(()), @@ -254,9 +228,9 @@ impl AsyncNetworkStream { } #[allow(unused_variables)] - #[cfg(any(feature = "tokio03-native-tls", feature = "tokio03-rustls-tls"))] - async fn upgrade_tokio03_tls( - tcp_stream: Tokio03TcpStream, + #[cfg(any(feature = "tokio1-native-tls", feature = "tokio1-rustls-tls"))] + async fn upgrade_tokio1_tls( + tcp_stream: Tokio1TcpStream, mut tls_parameters: TlsParameters, ) -> Result { let domain = std::mem::take(&mut tls_parameters.domain); @@ -264,32 +238,32 @@ impl AsyncNetworkStream { match tls_parameters.connector { #[cfg(feature = "native-tls")] InnerTlsParameters::NativeTls(connector) => { - #[cfg(not(feature = "tokio03-native-tls"))] - panic!("built without the tokio03-native-tls feature"); + #[cfg(not(feature = "tokio1-native-tls"))] + panic!("built without the tokio1-native-tls feature"); - #[cfg(feature = "tokio03-native-tls")] + #[cfg(feature = "tokio1-native-tls")] return { - use tokio03_native_tls_crate::TlsConnector; + use tokio1_native_tls_crate::TlsConnector; let connector = TlsConnector::from(connector); let stream = connector.connect(&domain, tcp_stream).await?; - Ok(InnerAsyncNetworkStream::Tokio03NativeTls(stream)) + Ok(InnerAsyncNetworkStream::Tokio1NativeTls(stream)) }; } #[cfg(feature = "rustls-tls")] InnerTlsParameters::RustlsTls(config) => { - #[cfg(not(feature = "tokio03-rustls-tls"))] - panic!("built without the tokio03-rustls-tls feature"); + #[cfg(not(feature = "tokio1-rustls-tls"))] + panic!("built without the tokio1-rustls-tls feature"); - #[cfg(feature = "tokio03-rustls-tls")] + #[cfg(feature = "tokio1-rustls-tls")] return { - use tokio03_rustls::{webpki::DNSNameRef, TlsConnector}; + use tokio1_rustls::{webpki::DNSNameRef, TlsConnector}; let domain = DNSNameRef::try_from_ascii_str(&domain)?; let connector = TlsConnector::from(Arc::new(config)); let stream = connector.connect(domain, tcp_stream).await?; - Ok(InnerAsyncNetworkStream::Tokio03RustlsTls(stream)) + Ok(InnerAsyncNetworkStream::Tokio1RustlsTls(stream)) }; } } @@ -303,12 +277,12 @@ impl AsyncNetworkStream { InnerAsyncNetworkStream::Tokio02NativeTls(_) => true, #[cfg(feature = "tokio02-rustls-tls")] InnerAsyncNetworkStream::Tokio02RustlsTls(_) => true, - #[cfg(feature = "tokio03")] - InnerAsyncNetworkStream::Tokio03Tcp(_) => false, - #[cfg(feature = "tokio03-native-tls")] - InnerAsyncNetworkStream::Tokio03NativeTls(_) => true, - #[cfg(feature = "tokio03-rustls-tls")] - InnerAsyncNetworkStream::Tokio03RustlsTls(_) => true, + #[cfg(feature = "tokio1")] + InnerAsyncNetworkStream::Tokio1Tcp(_) => false, + #[cfg(feature = "tokio1-native-tls")] + InnerAsyncNetworkStream::Tokio1NativeTls(_) => true, + #[cfg(feature = "tokio1-rustls-tls")] + InnerAsyncNetworkStream::Tokio1RustlsTls(_) => true, InnerAsyncNetworkStream::None => false, } } @@ -327,27 +301,27 @@ impl futures_io::AsyncRead for AsyncNetworkStream { InnerAsyncNetworkStream::Tokio02NativeTls(ref mut s) => Pin::new(s).poll_read(cx, buf), #[cfg(feature = "tokio02-rustls-tls")] InnerAsyncNetworkStream::Tokio02RustlsTls(ref mut s) => Pin::new(s).poll_read(cx, buf), - #[cfg(feature = "tokio03")] - InnerAsyncNetworkStream::Tokio03Tcp(ref mut s) => { - let mut b = Tokio03ReadBuf::new(buf); + #[cfg(feature = "tokio1")] + InnerAsyncNetworkStream::Tokio1Tcp(ref mut s) => { + let mut b = Tokio1ReadBuf::new(buf); match Pin::new(s).poll_read(cx, &mut b) { Poll::Ready(Ok(())) => Poll::Ready(Ok(b.filled().len())), Poll::Ready(Err(err)) => Poll::Ready(Err(err)), Poll::Pending => Poll::Pending, } } - #[cfg(feature = "tokio03-native-tls")] - InnerAsyncNetworkStream::Tokio03NativeTls(ref mut s) => { - let mut b = Tokio03ReadBuf::new(buf); + #[cfg(feature = "tokio1-native-tls")] + InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => { + let mut b = Tokio1ReadBuf::new(buf); match Pin::new(s).poll_read(cx, &mut b) { Poll::Ready(Ok(())) => Poll::Ready(Ok(b.filled().len())), Poll::Ready(Err(err)) => Poll::Ready(Err(err)), Poll::Pending => Poll::Pending, } } - #[cfg(feature = "tokio03-rustls-tls")] - InnerAsyncNetworkStream::Tokio03RustlsTls(ref mut s) => { - let mut b = Tokio03ReadBuf::new(buf); + #[cfg(feature = "tokio1-rustls-tls")] + InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => { + let mut b = Tokio1ReadBuf::new(buf); match Pin::new(s).poll_read(cx, &mut b) { Poll::Ready(Ok(())) => Poll::Ready(Ok(b.filled().len())), Poll::Ready(Err(err)) => Poll::Ready(Err(err)), @@ -375,12 +349,12 @@ impl futures_io::AsyncWrite for AsyncNetworkStream { InnerAsyncNetworkStream::Tokio02NativeTls(ref mut s) => Pin::new(s).poll_write(cx, buf), #[cfg(feature = "tokio02-rustls-tls")] InnerAsyncNetworkStream::Tokio02RustlsTls(ref mut s) => Pin::new(s).poll_write(cx, buf), - #[cfg(feature = "tokio03")] - InnerAsyncNetworkStream::Tokio03Tcp(ref mut s) => Pin::new(s).poll_write(cx, buf), - #[cfg(feature = "tokio03-native-tls")] - InnerAsyncNetworkStream::Tokio03NativeTls(ref mut s) => Pin::new(s).poll_write(cx, buf), - #[cfg(feature = "tokio03-rustls-tls")] - InnerAsyncNetworkStream::Tokio03RustlsTls(ref mut s) => Pin::new(s).poll_write(cx, buf), + #[cfg(feature = "tokio1")] + InnerAsyncNetworkStream::Tokio1Tcp(ref mut s) => Pin::new(s).poll_write(cx, buf), + #[cfg(feature = "tokio1-native-tls")] + InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_write(cx, buf), + #[cfg(feature = "tokio1-rustls-tls")] + InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_write(cx, buf), InnerAsyncNetworkStream::None => { debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); Poll::Ready(Ok(0)) @@ -396,12 +370,12 @@ impl futures_io::AsyncWrite for AsyncNetworkStream { InnerAsyncNetworkStream::Tokio02NativeTls(ref mut s) => Pin::new(s).poll_flush(cx), #[cfg(feature = "tokio02-rustls-tls")] InnerAsyncNetworkStream::Tokio02RustlsTls(ref mut s) => Pin::new(s).poll_flush(cx), - #[cfg(feature = "tokio03")] - InnerAsyncNetworkStream::Tokio03Tcp(ref mut s) => Pin::new(s).poll_flush(cx), - #[cfg(feature = "tokio03-native-tls")] - InnerAsyncNetworkStream::Tokio03NativeTls(ref mut s) => Pin::new(s).poll_flush(cx), - #[cfg(feature = "tokio03-rustls-tls")] - InnerAsyncNetworkStream::Tokio03RustlsTls(ref mut s) => Pin::new(s).poll_flush(cx), + #[cfg(feature = "tokio1")] + InnerAsyncNetworkStream::Tokio1Tcp(ref mut s) => Pin::new(s).poll_flush(cx), + #[cfg(feature = "tokio1-native-tls")] + InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_flush(cx), + #[cfg(feature = "tokio1-rustls-tls")] + InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_flush(cx), InnerAsyncNetworkStream::None => { debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); Poll::Ready(Ok(())) @@ -409,7 +383,24 @@ impl futures_io::AsyncWrite for AsyncNetworkStream { } } - fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(self.shutdown(Shutdown::Write)) + fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + match self.inner { + #[cfg(feature = "tokio02")] + InnerAsyncNetworkStream::Tokio02Tcp(ref mut s) => Pin::new(s).poll_shutdown(cx), + #[cfg(feature = "tokio02-native-tls")] + InnerAsyncNetworkStream::Tokio02NativeTls(ref mut s) => Pin::new(s).poll_shutdown(cx), + #[cfg(feature = "tokio02-rustls-tls")] + InnerAsyncNetworkStream::Tokio02RustlsTls(ref mut s) => Pin::new(s).poll_shutdown(cx), + #[cfg(feature = "tokio1")] + InnerAsyncNetworkStream::Tokio1Tcp(ref mut s) => Pin::new(s).poll_shutdown(cx), + #[cfg(feature = "tokio1-native-tls")] + InnerAsyncNetworkStream::Tokio1NativeTls(ref mut s) => Pin::new(s).poll_shutdown(cx), + #[cfg(feature = "tokio1-rustls-tls")] + InnerAsyncNetworkStream::Tokio1RustlsTls(ref mut s) => Pin::new(s).poll_shutdown(cx), + InnerAsyncNetworkStream::None => { + debug_assert!(false, "InnerAsyncNetworkStream::None must never be built"); + Poll::Ready(Ok(())) + } + } } } diff --git a/src/transport/smtp/client/mod.rs b/src/transport/smtp/client/mod.rs index 3d7f23e..8829e0e 100644 --- a/src/transport/smtp/client/mod.rs +++ b/src/transport/smtp/client/mod.rs @@ -27,9 +27,9 @@ #[cfg(feature = "serde")] use std::fmt::Debug; -#[cfg(any(feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "tokio02", feature = "tokio1"))] pub(crate) use self::async_connection::AsyncSmtpConnection; -#[cfg(any(feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "tokio02", feature = "tokio1"))] pub(crate) use self::async_net::AsyncNetworkStream; use self::net::NetworkStream; #[cfg(any(feature = "native-tls", feature = "rustls-tls"))] @@ -40,9 +40,9 @@ pub use self::{ tls::{Certificate, Tls, TlsParameters, TlsParametersBuilder}, }; -#[cfg(any(feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "tokio02", feature = "tokio1"))] mod async_connection; -#[cfg(any(feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "tokio02", feature = "tokio1"))] mod async_net; mod connection; mod mock; diff --git a/src/transport/smtp/mod.rs b/src/transport/smtp/mod.rs index ce577c2..b94259b 100644 --- a/src/transport/smtp/mod.rs +++ b/src/transport/smtp/mod.rs @@ -156,9 +156,9 @@ #[cfg(feature = "tokio02")] pub use self::async_transport::Tokio02Connector; -#[cfg(feature = "tokio03")] -pub use self::async_transport::Tokio03Connector; -#[cfg(any(feature = "tokio02", feature = "tokio03"))] +#[cfg(feature = "tokio1")] +pub use self::async_transport::Tokio1Connector; +#[cfg(any(feature = "tokio02", feature = "tokio1"))] pub use self::async_transport::{ AsyncSmtpConnector, AsyncSmtpTransport, AsyncSmtpTransportBuilder, }; @@ -181,7 +181,7 @@ use crate::transport::smtp::{ use client::Tls; use std::time::Duration; -#[cfg(any(feature = "tokio02", feature = "tokio03"))] +#[cfg(any(feature = "tokio02", feature = "tokio1"))] mod async_transport; pub mod authentication; pub mod client;