Skip to content

Commit 26446fd

Browse files
Replace derive_more with declarative macros (#438)
Co-authored-by: Rob Ede <robjtede@icloud.com>
1 parent b7b7bd2 commit 26446fd

File tree

10 files changed

+114
-33
lines changed

10 files changed

+114
-33
lines changed

actix-tls/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
[#429]: https://github.com/actix/actix-net/pull/429
1818

19+
1920
## 3.0.0-rc.1 - 2021-11-29
2021
### Added
2122
- Derive `Debug` for `connect::Connection`. [#422]

actix-tls/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ actix-rt = { version = "2.2.0", default-features = false }
4747
actix-service = "2.0.0"
4848
actix-utils = "3.0.0"
4949

50-
derive_more = "0.99.5"
5150
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
5251
log = "0.4"
5352
pin-project-lite = "0.2.7"

actix-tls/src/accept/mod.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
33
use std::{
44
convert::Infallible,
5+
error::Error,
6+
fmt,
57
sync::atomic::{AtomicUsize, Ordering},
68
};
79

810
use actix_utils::counter::Counter;
9-
use derive_more::{Display, Error};
1011

1112
#[cfg(feature = "openssl")]
1213
#[cfg_attr(docsrs, doc(cfg(feature = "openssl")))]
@@ -43,23 +44,45 @@ pub fn max_concurrent_tls_connect(num: usize) {
4344
/// TLS handshake error, TLS timeout, or inner service error.
4445
///
4546
/// All TLS acceptors from this crate will return the `SvcErr` type parameter as [`Infallible`],
46-
/// which can be cast to your own service type, inferred or otherwise,
47-
/// using [`into_service_error`](Self::into_service_error).
48-
#[derive(Debug, Display, Error)]
47+
/// which can be cast to your own service type, inferred or otherwise, using [`into_service_error`].
48+
///
49+
/// [`into_service_error`]: Self::into_service_error
50+
#[derive(Debug)]
4951
pub enum TlsError<TlsErr, SvcErr> {
5052
/// TLS handshake has timed-out.
51-
#[display(fmt = "TLS handshake has timed-out")]
5253
Timeout,
5354

5455
/// Wraps TLS service errors.
55-
#[display(fmt = "TLS handshake error")]
5656
Tls(TlsErr),
5757

5858
/// Wraps service errors.
59-
#[display(fmt = "Service error")]
6059
Service(SvcErr),
6160
}
6261

62+
impl<TlsErr, SvcErr> fmt::Display for TlsError<TlsErr, SvcErr> {
63+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64+
match self {
65+
Self::Timeout => f.write_str("TLS handshake has timed-out"),
66+
Self::Tls(_) => f.write_str("TLS handshake error"),
67+
Self::Service(_) => f.write_str("Service error"),
68+
}
69+
}
70+
}
71+
72+
impl<TlsErr, SvcErr> Error for TlsError<TlsErr, SvcErr>
73+
where
74+
TlsErr: Error + 'static,
75+
SvcErr: Error + 'static,
76+
{
77+
fn source(&self) -> Option<&(dyn Error + 'static)> {
78+
match self {
79+
TlsError::Tls(err) => Some(err),
80+
TlsError::Service(err) => Some(err),
81+
TlsError::Timeout => None,
82+
}
83+
}
84+
}
85+
6386
impl<TlsErr> TlsError<TlsErr, Infallible> {
6487
/// Casts the infallible service error type returned from acceptors into caller's type.
6588
///

actix-tls/src/accept/native_tls.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ use actix_utils::{
2020
counter::Counter,
2121
future::{ready, Ready as FutReady},
2222
};
23-
use derive_more::{Deref, DerefMut, From};
2423
use futures_core::future::LocalBoxFuture;
2524
use tokio_native_tls::{native_tls::Error, TlsAcceptor};
2625

2726
use super::{TlsError, DEFAULT_TLS_HANDSHAKE_TIMEOUT, MAX_CONN_COUNTER};
27+
use crate::impl_more;
2828

2929
pub mod reexports {
3030
//! Re-exports from `native-tls` that are useful for acceptors.
@@ -33,9 +33,12 @@ pub mod reexports {
3333
}
3434

3535
/// Wraps a `native-tls` based async TLS stream in order to implement [`ActixStream`].
36-
#[derive(Deref, DerefMut, From)]
3736
pub struct TlsStream<IO>(tokio_native_tls::TlsStream<IO>);
3837

38+
impl_more::from! { tokio_native_tls::TlsStream<IO> => TlsStream<IO> }
39+
impl_more::deref! { TlsStream<IO> => 0: tokio_native_tls::TlsStream<IO> }
40+
impl_more::deref_mut! { TlsStream<IO> => 0 }
41+
3942
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
4043
fn poll_read(
4144
self: Pin<&mut Self>,

actix-tls/src/accept/openssl.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ use actix_utils::{
2121
counter::{Counter, CounterGuard},
2222
future::{ready, Ready as FutReady},
2323
};
24-
use derive_more::{Deref, DerefMut, From};
2524
use openssl::ssl::{Error, Ssl, SslAcceptor};
2625
use pin_project_lite::pin_project;
2726

2827
use super::{TlsError, DEFAULT_TLS_HANDSHAKE_TIMEOUT, MAX_CONN_COUNTER};
28+
use crate::impl_more;
2929

3030
pub mod reexports {
3131
//! Re-exports from `openssl` that are useful for acceptors.
@@ -36,9 +36,12 @@ pub mod reexports {
3636
}
3737

3838
/// Wraps an `openssl` based async TLS stream in order to implement [`ActixStream`].
39-
#[derive(Deref, DerefMut, From)]
4039
pub struct TlsStream<IO>(tokio_openssl::SslStream<IO>);
4140

41+
impl_more::from! { tokio_openssl::SslStream<IO> => TlsStream<IO> }
42+
impl_more::deref! { TlsStream<IO> => 0: tokio_openssl::SslStream<IO> }
43+
impl_more::deref_mut! { TlsStream<IO> => 0 }
44+
4245
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
4346
fn poll_read(
4447
self: Pin<&mut Self>,

actix-tls/src/accept/rustls.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use actix_utils::{
2222
counter::{Counter, CounterGuard},
2323
future::{ready, Ready as FutReady},
2424
};
25-
use derive_more::{Deref, DerefMut, From};
2625
use pin_project_lite::pin_project;
2726
use tokio_rustls::rustls::ServerConfig;
2827
use tokio_rustls::{Accept, TlsAcceptor};
2928

3029
use super::{TlsError, DEFAULT_TLS_HANDSHAKE_TIMEOUT, MAX_CONN_COUNTER};
30+
use crate::impl_more;
3131

3232
pub mod reexports {
3333
//! Re-exports from `rustls` that are useful for acceptors.
@@ -36,9 +36,12 @@ pub mod reexports {
3636
}
3737

3838
/// Wraps a `rustls` based async TLS stream in order to implement [`ActixStream`].
39-
#[derive(Deref, DerefMut, From)]
4039
pub struct TlsStream<IO>(tokio_rustls::server::TlsStream<IO>);
4140

41+
impl_more::from! { tokio_rustls::server::TlsStream<IO> => TlsStream<IO> }
42+
impl_more::deref! { TlsStream<IO> => 0: tokio_rustls::server::TlsStream<IO> }
43+
impl_more::deref_mut! { TlsStream<IO> => 0 }
44+
4245
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
4346
fn poll_read(
4447
self: Pin<&mut Self>,

actix-tls/src/connect/connection.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
use derive_more::{Deref, DerefMut};
2-
31
use super::Host;
2+
use crate::impl_more;
43

54
/// Wraps underlying I/O and the connection request that initiated it.
6-
#[derive(Debug, Deref, DerefMut)]
5+
#[derive(Debug)]
76
pub struct Connection<R, IO> {
87
pub(crate) req: R,
9-
10-
#[deref]
11-
#[deref_mut]
128
pub(crate) io: IO,
139
}
1410

11+
impl_more::deref! { Connection<R, IO> => io: IO }
12+
impl_more::deref_mut! { Connection<R, IO> => io }
13+
1514
impl<R, IO> Connection<R, IO> {
1615
/// Construct new `Connection` from request and IO parts.
1716
pub(crate) fn new(req: R, io: IO) -> Self {

actix-tls/src/connect/error.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1-
use std::{error::Error, io};
2-
3-
use derive_more::Display;
1+
use std::{error::Error, fmt, io};
42

53
/// Errors that can result from using a connector service.
6-
#[derive(Debug, Display)]
4+
#[derive(Debug)]
75
pub enum ConnectError {
8-
/// Failed to resolve the hostname
9-
#[display(fmt = "Failed resolving hostname")]
6+
/// Failed to resolve the hostname.
107
Resolver(Box<dyn std::error::Error>),
118

12-
/// No DNS records
13-
#[display(fmt = "No DNS records found for the input")]
9+
/// No DNS records.
1410
NoRecords,
1511

16-
/// Invalid input
12+
/// Invalid input.
1713
InvalidInput,
1814

19-
/// Unresolved host name
20-
#[display(fmt = "Connector received `Connect` method with unresolved host")]
15+
/// Unresolved host name.
2116
Unresolved,
2217

23-
/// Connection IO error
24-
#[display(fmt = "{}", _0)]
18+
/// Connection IO error.
2519
Io(io::Error),
2620
}
2721

22+
impl fmt::Display for ConnectError {
23+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24+
match self {
25+
Self::NoRecords => f.write_str("No DNS records found for the input"),
26+
Self::InvalidInput => f.write_str("Invalid input"),
27+
Self::Unresolved => {
28+
f.write_str("Connector received `Connect` method with unresolved host")
29+
}
30+
Self::Resolver(_) => f.write_str("Failed to resolve hostname"),
31+
Self::Io(_) => f.write_str("I/O error"),
32+
}
33+
}
34+
}
35+
2836
impl Error for ConnectError {
2937
fn source(&self) -> Option<&(dyn Error + 'static)> {
3038
match self {

actix-tls/src/impl_more.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/// A helper to implement `Deref` for a type.
2+
#[macro_export]
3+
macro_rules! deref {
4+
($ty:ident $(<$($generic:ident),*>)? => $field:tt: $target:ty) => {
5+
impl $(<$($generic),*>)? ::core::ops::Deref for $ty $(<$($generic),*>)? {
6+
type Target = $target;
7+
8+
fn deref(&self) -> &Self::Target {
9+
&self.$field
10+
}
11+
}
12+
};
13+
}
14+
15+
/// A helper to implement `DerefMut` for a type.
16+
#[macro_export]
17+
macro_rules! deref_mut {
18+
($ty:ident $(<$($generic:ident),*>)? => $field:tt) => {
19+
impl $(<$($generic),*>)? ::core::ops::DerefMut for $ty $(<$($generic),*>)? {
20+
fn deref_mut(&mut self) -> &mut Self::Target {
21+
&mut self.$field
22+
}
23+
}
24+
};
25+
}
26+
27+
/// A helper to implement `From` for a unit struct.
28+
#[macro_export]
29+
macro_rules! from {
30+
($from:ty => $ty:ident $(<$($generic:ident),*>)?) => {
31+
impl $(<$($generic),*>)? ::core::convert::From<$from> for $ty $(<$($generic),*>)? {
32+
fn from(from: $from) -> Self {
33+
Self(from)
34+
}
35+
}
36+
};
37+
}
38+
39+
#[allow(unused_imports)]
40+
pub(crate) use crate::{deref, deref_mut, from};

actix-tls/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ pub mod accept;
1818
#[cfg(feature = "connect")]
1919
#[cfg_attr(docsrs, doc(cfg(feature = "connect")))]
2020
pub mod connect;
21+
22+
mod impl_more;

0 commit comments

Comments
 (0)