mirror of https://github.com/fafhrd91/actix-net
replace `derive_more` with declarative macros
This commit is contained in:
parent
b7b7bd2cbf
commit
bb38752c13
|
@ -47,7 +47,6 @@ actix-rt = { version = "2.2.0", default-features = false }
|
||||||
actix-service = "2.0.0"
|
actix-service = "2.0.0"
|
||||||
actix-utils = "3.0.0"
|
actix-utils = "3.0.0"
|
||||||
|
|
||||||
derive_more = "0.99.5"
|
|
||||||
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
pin-project-lite = "0.2.7"
|
pin-project-lite = "0.2.7"
|
||||||
|
|
|
@ -5,8 +5,7 @@ use std::{
|
||||||
sync::atomic::{AtomicUsize, Ordering},
|
sync::atomic::{AtomicUsize, Ordering},
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_utils::counter::Counter;
|
use actix_utils::{counter::Counter, derive};
|
||||||
use derive_more::{Display, Error};
|
|
||||||
|
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
#[cfg_attr(docsrs, doc(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`],
|
/// 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,
|
/// which can be cast to your own service type, inferred or otherwise,
|
||||||
/// using [`into_service_error`](Self::into_service_error).
|
/// using [`into_service_error`](Self::into_service_error).
|
||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug)]
|
||||||
pub enum TlsError<TlsErr, SvcErr> {
|
pub enum TlsError<TlsErr, SvcErr> {
|
||||||
/// TLS handshake has timed-out.
|
/// TLS handshake has timed-out.
|
||||||
#[display(fmt = "TLS handshake has timed-out")]
|
|
||||||
Timeout,
|
Timeout,
|
||||||
|
|
||||||
/// Wraps TLS service errors.
|
/// Wraps TLS service errors.
|
||||||
#[display(fmt = "TLS handshake error")]
|
|
||||||
Tls(TlsErr),
|
Tls(TlsErr),
|
||||||
|
|
||||||
/// Wraps service errors.
|
/// Wraps service errors.
|
||||||
#[display(fmt = "Service error")]
|
|
||||||
Service(SvcErr),
|
Service(SvcErr),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
derive::enum_error! {
|
||||||
|
match TlsError<TlsErr, SvcErr> |f| {
|
||||||
|
Timeout => "TLS handshake has timed-out",
|
||||||
|
Tls(e) => "TLS handshake error",
|
||||||
|
Service(e) => "Service error",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<TlsErr> TlsError<TlsErr, Infallible> {
|
impl<TlsErr> TlsError<TlsErr, Infallible> {
|
||||||
/// Casts the infallible service error type returned from acceptors into caller's type.
|
/// Casts the infallible service error type returned from acceptors into caller's type.
|
||||||
///
|
///
|
||||||
|
|
|
@ -18,9 +18,9 @@ use actix_rt::{
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use actix_utils::{
|
use actix_utils::{
|
||||||
counter::Counter,
|
counter::Counter,
|
||||||
|
derive,
|
||||||
future::{ready, Ready as FutReady},
|
future::{ready, Ready as FutReady},
|
||||||
};
|
};
|
||||||
use derive_more::{Deref, DerefMut, From};
|
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use tokio_native_tls::{native_tls::Error, TlsAcceptor};
|
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`].
|
/// Wraps a `native-tls` based async TLS stream in order to implement [`ActixStream`].
|
||||||
#[derive(Deref, DerefMut, From)]
|
|
||||||
pub struct TlsStream<IO>(tokio_native_tls::TlsStream<IO>);
|
pub struct TlsStream<IO>(tokio_native_tls::TlsStream<IO>);
|
||||||
|
|
||||||
|
derive::from! { tokio_native_tls::TlsStream<IO> => TlsStream<IO> }
|
||||||
|
derive::deref! { TlsStream<IO> => 0: tokio_native_tls::TlsStream<IO> }
|
||||||
|
derive::deref_mut! { TlsStream<IO> => 0 }
|
||||||
|
|
||||||
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
|
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
|
|
|
@ -19,9 +19,9 @@ use actix_rt::{
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use actix_utils::{
|
use actix_utils::{
|
||||||
counter::{Counter, CounterGuard},
|
counter::{Counter, CounterGuard},
|
||||||
|
derive,
|
||||||
future::{ready, Ready as FutReady},
|
future::{ready, Ready as FutReady},
|
||||||
};
|
};
|
||||||
use derive_more::{Deref, DerefMut, From};
|
|
||||||
use openssl::ssl::{Error, Ssl, SslAcceptor};
|
use openssl::ssl::{Error, Ssl, SslAcceptor};
|
||||||
use pin_project_lite::pin_project;
|
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`].
|
/// Wraps an `openssl` based async TLS stream in order to implement [`ActixStream`].
|
||||||
#[derive(Deref, DerefMut, From)]
|
|
||||||
pub struct TlsStream<IO>(tokio_openssl::SslStream<IO>);
|
pub struct TlsStream<IO>(tokio_openssl::SslStream<IO>);
|
||||||
|
|
||||||
|
derive::from! { tokio_openssl::SslStream<IO> => TlsStream<IO> }
|
||||||
|
derive::deref! { TlsStream<IO> => 0: tokio_openssl::SslStream<IO> }
|
||||||
|
derive::deref_mut! { TlsStream<IO> => 0 }
|
||||||
|
|
||||||
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
|
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
|
|
|
@ -20,9 +20,9 @@ use actix_rt::{
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use actix_utils::{
|
use actix_utils::{
|
||||||
counter::{Counter, CounterGuard},
|
counter::{Counter, CounterGuard},
|
||||||
|
derive,
|
||||||
future::{ready, Ready as FutReady},
|
future::{ready, Ready as FutReady},
|
||||||
};
|
};
|
||||||
use derive_more::{Deref, DerefMut, From};
|
|
||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
use tokio_rustls::rustls::ServerConfig;
|
use tokio_rustls::rustls::ServerConfig;
|
||||||
use tokio_rustls::{Accept, TlsAcceptor};
|
use tokio_rustls::{Accept, TlsAcceptor};
|
||||||
|
@ -36,9 +36,12 @@ pub mod reexports {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps a `rustls` based async TLS stream in order to implement [`ActixStream`].
|
/// Wraps a `rustls` based async TLS stream in order to implement [`ActixStream`].
|
||||||
#[derive(Deref, DerefMut, From)]
|
|
||||||
pub struct TlsStream<IO>(tokio_rustls::server::TlsStream<IO>);
|
pub struct TlsStream<IO>(tokio_rustls::server::TlsStream<IO>);
|
||||||
|
|
||||||
|
derive::from! { tokio_rustls::server::TlsStream<IO> => TlsStream<IO> }
|
||||||
|
derive::deref! { TlsStream<IO> => 0: tokio_rustls::server::TlsStream<IO> }
|
||||||
|
derive::deref_mut! { TlsStream<IO> => 0 }
|
||||||
|
|
||||||
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
|
impl<IO: ActixStream> AsyncRead for TlsStream<IO> {
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
use derive_more::{Deref, DerefMut};
|
|
||||||
|
|
||||||
use super::Host;
|
use super::Host;
|
||||||
|
use actix_utils::derive;
|
||||||
|
|
||||||
/// Wraps underlying I/O and the connection request that initiated it.
|
/// Wraps underlying I/O and the connection request that initiated it.
|
||||||
#[derive(Debug, Deref, DerefMut)]
|
#[derive(Debug)]
|
||||||
pub struct Connection<R, IO> {
|
pub struct Connection<R, IO> {
|
||||||
pub(crate) req: R,
|
pub(crate) req: R,
|
||||||
|
|
||||||
#[deref]
|
|
||||||
#[deref_mut]
|
|
||||||
pub(crate) io: IO,
|
pub(crate) io: IO,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
derive::deref! { Connection<R, IO> => io: IO }
|
||||||
|
derive::deref_mut! { Connection<R, IO> => io }
|
||||||
|
|
||||||
impl<R, IO> Connection<R, IO> {
|
impl<R, IO> Connection<R, IO> {
|
||||||
/// Construct new `Connection` from request and IO parts.
|
/// Construct new `Connection` from request and IO parts.
|
||||||
pub(crate) fn new(req: R, io: IO) -> Self {
|
pub(crate) fn new(req: R, io: IO) -> Self {
|
||||||
|
|
|
@ -1,36 +1,27 @@
|
||||||
use std::{error::Error, io};
|
use actix_utils::derive;
|
||||||
|
use std::io;
|
||||||
use derive_more::Display;
|
|
||||||
|
|
||||||
/// Errors that can result from using a connector service.
|
/// Errors that can result from using a connector service.
|
||||||
#[derive(Debug, Display)]
|
#[derive(Debug)]
|
||||||
pub enum ConnectError {
|
pub enum ConnectError {
|
||||||
/// Failed to resolve the hostname
|
/// Failed to resolve the hostname
|
||||||
#[display(fmt = "Failed resolving hostname")]
|
|
||||||
Resolver(Box<dyn std::error::Error>),
|
Resolver(Box<dyn std::error::Error>),
|
||||||
|
|
||||||
/// No DNS records
|
/// No DNS records
|
||||||
#[display(fmt = "No DNS records found for the input")]
|
|
||||||
NoRecords,
|
NoRecords,
|
||||||
|
|
||||||
/// Invalid input
|
/// Invalid input
|
||||||
InvalidInput,
|
InvalidInput,
|
||||||
|
|
||||||
/// Unresolved host name
|
/// Unresolved host name
|
||||||
#[display(fmt = "Connector received `Connect` method with unresolved host")]
|
|
||||||
Unresolved,
|
Unresolved,
|
||||||
|
|
||||||
/// Connection IO error
|
/// Connection IO error
|
||||||
#[display(fmt = "{}", _0)]
|
|
||||||
Io(io::Error),
|
Io(io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for ConnectError {
|
derive::enum_error! {
|
||||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
match ConnectError |f| {
|
||||||
match self {
|
NoRecords => "No DNS records found for the input",
|
||||||
Self::Resolver(err) => Some(&**err),
|
InvalidInput => "Invalid input",
|
||||||
Self::Io(err) => Some(err),
|
Unresolved => "Connector received `Connect` method with unresolved host",
|
||||||
Self::NoRecords | Self::InvalidInput | Self::Unresolved => None,
|
#[source(&**e)] Resolver(e) => "Failed to resolve hostname",
|
||||||
}
|
#[source(e)] Io(e) => return write!(f, "{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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};
|
|
@ -6,4 +6,5 @@
|
||||||
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
||||||
|
|
||||||
pub mod counter;
|
pub mod counter;
|
||||||
|
pub mod derive;
|
||||||
pub mod future;
|
pub mod future;
|
||||||
|
|
Loading…
Reference in New Issue