diff --git a/actix-tls/Cargo.toml b/actix-tls/Cargo.toml index 75ea26b0..61bd8bc9 100755 --- a/actix-tls/Cargo.toml +++ b/actix-tls/Cargo.toml @@ -47,7 +47,6 @@ actix-rt = { version = "2.2.0", default-features = false } actix-service = "2.0.0" actix-utils = "3.0.0" -derive_more = "0.99.5" futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] } log = "0.4" pin-project-lite = "0.2.7" diff --git a/actix-tls/src/accept/mod.rs b/actix-tls/src/accept/mod.rs index de220ac5..0f387e0c 100644 --- a/actix-tls/src/accept/mod.rs +++ b/actix-tls/src/accept/mod.rs @@ -5,8 +5,7 @@ use std::{ sync::atomic::{AtomicUsize, Ordering}, }; -use actix_utils::counter::Counter; -use derive_more::{Display, Error}; +use actix_utils::{counter::Counter, derive}; #[cfg(feature = "openssl")] #[cfg_attr(docsrs, doc(cfg(feature = "openssl")))] @@ -45,21 +44,24 @@ pub fn max_concurrent_tls_connect(num: usize) { /// All TLS acceptors from this crate will return the `SvcErr` type parameter as [`Infallible`], /// which can be cast to your own service type, inferred or otherwise, /// using [`into_service_error`](Self::into_service_error). -#[derive(Debug, Display, Error)] +#[derive(Debug)] pub enum TlsError { /// TLS handshake has timed-out. - #[display(fmt = "TLS handshake has timed-out")] Timeout, - /// Wraps TLS service errors. - #[display(fmt = "TLS handshake error")] Tls(TlsErr), - /// Wraps service errors. - #[display(fmt = "Service error")] Service(SvcErr), } +derive::enum_error! { + match TlsError |f| { + Timeout => "TLS handshake has timed-out", + Tls(e) => "TLS handshake error", + Service(e) => "Service error", + } +} + impl TlsError { /// Casts the infallible service error type returned from acceptors into caller's type. /// diff --git a/actix-tls/src/accept/native_tls.rs b/actix-tls/src/accept/native_tls.rs index 9a864a72..c853c77f 100644 --- a/actix-tls/src/accept/native_tls.rs +++ b/actix-tls/src/accept/native_tls.rs @@ -18,9 +18,9 @@ use actix_rt::{ use actix_service::{Service, ServiceFactory}; use actix_utils::{ counter::Counter, + derive, future::{ready, Ready as FutReady}, }; -use derive_more::{Deref, DerefMut, From}; use futures_core::future::LocalBoxFuture; use tokio_native_tls::{native_tls::Error, TlsAcceptor}; @@ -33,9 +33,12 @@ pub mod reexports { } /// Wraps a `native-tls` based async TLS stream in order to implement [`ActixStream`]. -#[derive(Deref, DerefMut, From)] pub struct TlsStream(tokio_native_tls::TlsStream); +derive::from! { tokio_native_tls::TlsStream => TlsStream } +derive::deref! { TlsStream => 0: tokio_native_tls::TlsStream } +derive::deref_mut! { TlsStream => 0 } + impl AsyncRead for TlsStream { fn poll_read( self: Pin<&mut Self>, diff --git a/actix-tls/src/accept/openssl.rs b/actix-tls/src/accept/openssl.rs index 51a45942..47e7bdd8 100644 --- a/actix-tls/src/accept/openssl.rs +++ b/actix-tls/src/accept/openssl.rs @@ -19,9 +19,9 @@ use actix_rt::{ use actix_service::{Service, ServiceFactory}; use actix_utils::{ counter::{Counter, CounterGuard}, + derive, future::{ready, Ready as FutReady}, }; -use derive_more::{Deref, DerefMut, From}; use openssl::ssl::{Error, Ssl, SslAcceptor}; use pin_project_lite::pin_project; @@ -36,9 +36,12 @@ pub mod reexports { } /// Wraps an `openssl` based async TLS stream in order to implement [`ActixStream`]. -#[derive(Deref, DerefMut, From)] pub struct TlsStream(tokio_openssl::SslStream); +derive::from! { tokio_openssl::SslStream => TlsStream } +derive::deref! { TlsStream => 0: tokio_openssl::SslStream } +derive::deref_mut! { TlsStream => 0 } + impl AsyncRead for TlsStream { fn poll_read( self: Pin<&mut Self>, diff --git a/actix-tls/src/accept/rustls.rs b/actix-tls/src/accept/rustls.rs index 4eca996e..0742b97d 100644 --- a/actix-tls/src/accept/rustls.rs +++ b/actix-tls/src/accept/rustls.rs @@ -20,9 +20,9 @@ use actix_rt::{ use actix_service::{Service, ServiceFactory}; use actix_utils::{ counter::{Counter, CounterGuard}, + derive, future::{ready, Ready as FutReady}, }; -use derive_more::{Deref, DerefMut, From}; use pin_project_lite::pin_project; use tokio_rustls::rustls::ServerConfig; use tokio_rustls::{Accept, TlsAcceptor}; @@ -36,9 +36,12 @@ pub mod reexports { } /// Wraps a `rustls` based async TLS stream in order to implement [`ActixStream`]. -#[derive(Deref, DerefMut, From)] pub struct TlsStream(tokio_rustls::server::TlsStream); +derive::from! { tokio_rustls::server::TlsStream => TlsStream } +derive::deref! { TlsStream => 0: tokio_rustls::server::TlsStream } +derive::deref_mut! { TlsStream => 0 } + impl AsyncRead for TlsStream { fn poll_read( self: Pin<&mut Self>, diff --git a/actix-tls/src/connect/connection.rs b/actix-tls/src/connect/connection.rs index 68972a2a..41215597 100644 --- a/actix-tls/src/connect/connection.rs +++ b/actix-tls/src/connect/connection.rs @@ -1,17 +1,16 @@ -use derive_more::{Deref, DerefMut}; - use super::Host; +use actix_utils::derive; /// Wraps underlying I/O and the connection request that initiated it. -#[derive(Debug, Deref, DerefMut)] +#[derive(Debug)] pub struct Connection { pub(crate) req: R, - - #[deref] - #[deref_mut] pub(crate) io: IO, } +derive::deref! { Connection => io: IO } +derive::deref_mut! { Connection => io } + impl Connection { /// Construct new `Connection` from request and IO parts. pub(crate) fn new(req: R, io: IO) -> Self { diff --git a/actix-tls/src/connect/error.rs b/actix-tls/src/connect/error.rs index 46944988..bba9a965 100644 --- a/actix-tls/src/connect/error.rs +++ b/actix-tls/src/connect/error.rs @@ -1,36 +1,27 @@ -use std::{error::Error, io}; - -use derive_more::Display; +use actix_utils::derive; +use std::io; /// Errors that can result from using a connector service. -#[derive(Debug, Display)] +#[derive(Debug)] pub enum ConnectError { /// Failed to resolve the hostname - #[display(fmt = "Failed resolving hostname")] Resolver(Box), - /// No DNS records - #[display(fmt = "No DNS records found for the input")] NoRecords, - /// Invalid input InvalidInput, - /// Unresolved host name - #[display(fmt = "Connector received `Connect` method with unresolved host")] Unresolved, - /// Connection IO error - #[display(fmt = "{}", _0)] Io(io::Error), } -impl Error for ConnectError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Resolver(err) => Some(&**err), - Self::Io(err) => Some(err), - Self::NoRecords | Self::InvalidInput | Self::Unresolved => None, - } +derive::enum_error! { + match ConnectError |f| { + NoRecords => "No DNS records found for the input", + InvalidInput => "Invalid input", + Unresolved => "Connector received `Connect` method with unresolved host", + #[source(&**e)] Resolver(e) => "Failed to resolve hostname", + #[source(e)] Io(e) => return write!(f, "{}", e), } } diff --git a/actix-utils/src/derive.rs b/actix-utils/src/derive.rs new file mode 100644 index 00000000..499b3db1 --- /dev/null +++ b/actix-utils/src/derive.rs @@ -0,0 +1,77 @@ +/// A helper to implement `Deref` for a type. +#[doc(hidden)] +#[macro_export] +macro_rules! deref { + ($ty:ident $(<$($generic:ident),*>)? => $field:tt: $target:ty) => { + impl $(<$($generic),*>)? ::std::ops::Deref for $ty $(<$($generic),*>)? { + type Target = $target; + + fn deref(&self) -> &Self::Target { + &self.$field + } + } + }; +} + +/// A helper to implement `DerefMut` for a type. +#[doc(hidden)] +#[macro_export] +macro_rules! deref_mut { + ($ty:ident $(<$($generic:ident),*>)? => $field:tt) => { + impl $(<$($generic),*>)? ::std::ops::DerefMut for $ty $(<$($generic),*>)? { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.$field + } + } + }; +} + +/// A helper to implement `From` for a unit struct. +#[doc(hidden)] +#[macro_export] +macro_rules! from { + ($from:ty => $ty:ident $(<$($generic:ident),*>)?) => { + impl $(<$($generic),*>)? ::std::convert::From<$from> for $ty $(<$($generic),*>)? { + fn from(from: $from) -> Self { + Self(from) + } + } + }; +} + +/// A helper to implement `Display` and `Error` for an enum. +#[doc(hidden)] +#[macro_export] +macro_rules! enum_error { + (match $ty:ident $(<$($generic:ident),*>)? |$f:ident| {$( + $( #[source($source:expr)] )? $variant:pat => $fmt:expr $(,)? + ),*}) => { + #[allow(unused)] + impl $(<$($generic),*>)? ::std::fmt::Display for $ty $(<$($generic),*>)? { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + use $ty::*; + + let $f = f; + let res = match self {$( + $variant => $fmt, + )*}; + + write!($f, "{}", res) + } + } + + impl $(<$($generic: ::std::fmt::Debug),*>)? ::std::error::Error for $ty $(<$($generic),*>)? { + fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> { + use $ty::*; + + match self { + $($( $variant => { Some($source) } )?)* + _ => None + } + } + } + }; +} + +#[doc(inline)] +pub use crate::{deref, deref_mut, enum_error, from}; diff --git a/actix-utils/src/lib.rs b/actix-utils/src/lib.rs index b02687cb..82d19ba5 100644 --- a/actix-utils/src/lib.rs +++ b/actix-utils/src/lib.rs @@ -6,4 +6,5 @@ #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] pub mod counter; +pub mod derive; pub mod future;