Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove async_trait and use impl Future for some traits #647

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions crates/cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use std::borrow::Borrow;
use std::collections::VecDeque;
use std::error::Error as StdError;
use std::future::Future;
use std::hash::Hash;

use bytes::Bytes;
Expand All @@ -42,14 +43,12 @@ cfg_feature! {
}

/// Issuer
#[async_trait]
pub trait CacheIssuer: Send + Sync + 'static {
/// The key is used to identify the rate limit.
type Key: Hash + Eq + Send + Sync + 'static;
/// Issue a new key for the request. If it returns `None`, the request will not be cached.
async fn issue(&self, req: &mut Request, depot: &Depot) -> Option<Self::Key>;
fn issue(&self, req: &mut Request, depot: &Depot) -> impl Future<Output = Option<Self::Key>> + Send;
}
#[async_trait]
impl<F, K> CacheIssuer for F
where
F: Fn(&mut Request, &Depot) -> Option<K> + Send + Sync + 'static,
Expand Down Expand Up @@ -113,7 +112,6 @@ impl RequestIssuer {
}
}

#[async_trait]
impl CacheIssuer for RequestIssuer {
type Key = String;
async fn issue(&self, req: &mut Request, _depot: &Depot) -> Option<Self::Key> {
Expand Down Expand Up @@ -147,19 +145,18 @@ impl CacheIssuer for RequestIssuer {
}

/// Store cache.
#[async_trait]
pub trait CacheStore: Send + Sync + 'static {
/// Error type for CacheStore.
type Error: StdError + Sync + Send + 'static;
/// Key
type Key: Hash + Eq + Send + Clone + 'static;
/// Get the cache item from the store.
async fn load_entry<Q>(&self, key: &Q) -> Option<CachedEntry>
fn load_entry<Q>(&self, key: &Q) -> impl Future<Output = Option<CachedEntry>> + Send
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + Sync;
/// Save the cache item from the store.
async fn save_entry(&self, key: Self::Key, data: CachedEntry) -> Result<(), Self::Error>;
fn save_entry(&self, key: Self::Key, data: CachedEntry) -> impl Future<Output = Result<(), Self::Error>> + Send;
}

/// `CachedBody` is used to save response body to `CachedStore`.
Expand Down
2 changes: 0 additions & 2 deletions crates/cache/src/moka_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::time::Duration;
use moka::future::Cache as MokaCache;
use moka::future::CacheBuilder as MokaCacheBuilder;
use moka::notification::RemovalCause;
use salvo_core::async_trait;

use super::{CacheStore, CachedEntry};

Expand Down Expand Up @@ -114,7 +113,6 @@ where
}
}

#[async_trait]
impl<K> CacheStore for MokaStore<K>
where
K: Hash + Eq + Send + Sync + Clone + 'static,
Expand Down
32 changes: 25 additions & 7 deletions crates/core/src/conn/acme/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Note that the files contain private keys.
*/

use std::error::Error as StdError;
use std::future::Future;
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::path::Path;

use async_trait::async_trait;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::engine::Engine;
use ring::digest::{Context, SHA256};
Expand All @@ -19,8 +19,8 @@ use tokio::io::AsyncWriteExt;
pub trait CacheError: StdError + Send + Sync + 'static {}

impl<T> CacheError for T where T: StdError + Send + Sync + 'static {}

/// Trait to define a custom location/mechanism to cache account data and certificates.
#[async_trait]
pub trait AcmeCache {
/// The error type returned from the functions on this trait.
type Error: CacheError;
Expand All @@ -36,7 +36,11 @@ pub trait AcmeCache {
///
/// Returns an error when the private key was unable to be written
/// successfully.
async fn read_key(&self, directory_name: &str, domains: &[String]) -> Result<Option<Vec<u8>>, Self::Error>;
fn read_key(
&self,
directory_name: &str,
domains: &[String],
) -> impl Future<Output = Result<Option<Vec<u8>>, Self::Error>> + Send;

/// Writes a certificate retrieved from `Acme`. The parameters are:
///
Expand All @@ -50,7 +54,12 @@ pub trait AcmeCache {
///
/// Returns an error when the certificate was unable to be written
/// successfully.
async fn write_key(&self, directory_name: &str, domains: &[String], data: &[u8]) -> Result<(), Self::Error>;
fn write_key(
&self,
directory_name: &str,
domains: &[String],
data: &[u8],
) -> impl Future<Output = Result<(), Self::Error>> + Send;

/// Returns the previously written certificate retrieved from `Acme`. The parameters are:
///
Expand All @@ -63,7 +72,11 @@ pub trait AcmeCache {
///
/// Returns an error when the certificate was unable to be written
/// successfully.
async fn read_cert(&self, directory_name: &str, domains: &[String]) -> Result<Option<Vec<u8>>, Self::Error>;
fn read_cert(
&self,
directory_name: &str,
domains: &[String],
) -> impl Future<Output = Result<Option<Vec<u8>>, Self::Error>> + Send;

/// Writes a certificate retrieved from `Acme`. The parameters are:
///
Expand All @@ -77,12 +90,17 @@ pub trait AcmeCache {
///
/// Returns an error when the certificate was unable to be written
/// successfully.
async fn write_cert(&self, directory_name: &str, domains: &[String], data: &[u8]) -> Result<(), Self::Error>;
fn write_cert(
&self,
directory_name: &str,
domains: &[String],
data: &[u8],
) -> impl Future<Output = Result<(), Self::Error>> + Send;
}

static KEY_PEM_PREFIX: &str = "key-";
static CERT_PEM_PREFIX: &str = "cert-";
#[async_trait]

impl<P> AcmeCache for P
where
P: AsRef<Path> + Send + Sync,
Expand Down
8 changes: 3 additions & 5 deletions crates/core/src/conn/acme/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::conn::{Accepted, Acceptor, HandshakeStream, Holding, Listener};

use crate::http::uri::Scheme;
use crate::http::Version;
use crate::{async_trait, Router};
use crate::Router;

use super::config::{AcmeConfig, AcmeConfigBuilder};
use super::resolver::{ResolveServerCert, ACME_TLS_ALPN_NAME};
Expand Down Expand Up @@ -265,15 +265,14 @@ impl<T> AcmeListener<T> {
}
}

#[async_trait]
impl<T> Listener for AcmeListener<T>
where
T: Listener + Send,
T::Acceptor: Send + 'static,
{
type Acceptor = AcmeAcceptor<T::Acceptor>;

async fn try_bind(mut self) -> crate::Result<Self::Acceptor> {
async fn try_bind(self) -> crate::Result<Self::Acceptor> {
let Self {
inner,
config_builder,
Expand Down Expand Up @@ -320,7 +319,6 @@ cfg_feature! {
}
}

#[async_trait]
impl<T, A> Listener for AcmeQuinnListener<T, A>
where
T: Listener + Send,
Expand Down Expand Up @@ -420,7 +418,7 @@ where
self.server_config.clone()
}
}
#[async_trait]

impl<T: Acceptor> Acceptor for AcmeAcceptor<T>
where
T: Acceptor + Send + 'static,
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/conn/joined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::time::Duration;
use pin_project::pin_project;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

use crate::async_trait;
use crate::conn::{Holding, HttpBuilder};
use crate::http::HttpConnection;
use crate::service::HyperHandler;
Expand Down Expand Up @@ -83,7 +82,6 @@ impl<A, B> JoinedListener<A, B> {
JoinedListener { a, b }
}
}
#[async_trait]
impl<A, B> Listener for JoinedListener<A, B>
where
A: Listener + Send + Unpin + 'static,
Expand Down Expand Up @@ -131,7 +129,6 @@ where
}
}

#[async_trait]
impl<A, B> Acceptor for JoinedAcceptor<A, B>
where
A: Acceptor + Send + Unpin + 'static,
Expand Down
19 changes: 9 additions & 10 deletions crates/core/src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
//! The module also provides support for HTTP versions 1 and 2, as well as the QUIC protocol.
//! Additionally, it includes implementations for Unix domain sockets.
use std::fmt::{self, Display, Formatter};
use std::future::Future;
use std::io::Result as IoResult;

use http::uri::Scheme;
use tokio::io::{AsyncRead, AsyncWrite};

use crate::async_trait;
use crate::http::{HttpConnection, Version};

mod proto;
pub use proto::HttpBuilder;

cfg_feature! {
#![any(feature = "native-tls", feature = "rustls", feature = "openssl-tls", feature = "acme")]
#![any(feature = "native-tls", feature = "rustls", feature = "openssl", feature = "acme")]
mod handshake_stream;
pub use handshake_stream::HandshakeStream;
}
Expand Down Expand Up @@ -155,7 +155,6 @@ where
}

/// `Acceptor` represents an acceptor that can accept incoming connections.
#[async_trait]
pub trait Acceptor {
/// Conn type
type Conn: HttpConnection + AsyncRead + AsyncWrite + Send + Unpin + 'static;
Expand All @@ -164,7 +163,7 @@ pub trait Acceptor {
fn holdings(&self) -> &[Holding];

/// Accepts a new incoming connection from this listener.
async fn accept(&mut self) -> IoResult<Accepted<Self::Conn>>;
fn accept(&mut self) -> impl Future<Output = IoResult<Accepted<Self::Conn>>> + Send;
}

/// Holding information.
Expand All @@ -191,27 +190,27 @@ impl Display for Holding {
}

/// `Listener` represents a listener that can bind to a specific address and port and return an acceptor.
#[async_trait]

pub trait Listener {
/// Acceptor type.
type Acceptor: Acceptor;

/// Bind and returns acceptor.
async fn bind(self) -> Self::Acceptor
fn bind(self) -> impl Future<Output = Self::Acceptor> + Send
where
Self: Sized,
Self: Sized + Send,
{
self.try_bind().await.unwrap()
async move { self.try_bind().await.unwrap() }
}

/// Bind and returns acceptor.
async fn try_bind(self) -> crate::Result<Self::Acceptor>;
fn try_bind(self) -> impl Future<Output = crate::Result<Self::Acceptor>> + Send;

/// Join current Listener with the other.
#[inline]
fn join<T>(self, other: T) -> JoinedListener<Self, T>
where
Self: Sized,
Self: Sized + Send,
{
JoinedListener::new(self, other)
}
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/conn/native_tls/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use http::uri::Scheme;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_native_tls::TlsStream;

use crate::async_trait;
use crate::conn::{Accepted, Acceptor, HandshakeStream, Holding, HttpBuilder, IntoConfigStream, Listener};
use crate::http::{HttpConnection, Version};
use crate::service::HyperHandler;
Expand Down Expand Up @@ -43,7 +42,6 @@ where
}
}

#[async_trait]
impl<S, C, T, E> Listener for NativeTlsListener<S, C, T, E>
where
S: IntoConfigStream<C> + Send + 'static,
Expand Down Expand Up @@ -124,7 +122,6 @@ where
}
}

#[async_trait]
impl<S, C, T, E> Acceptor for NativeTlsAcceptor<S, C, T, E>
where
S: Stream<Item = C> + Send + Unpin + 'static,
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/conn/openssl/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use tokio_openssl::SslStream;

use super::SslAcceptorBuilder;

use crate::async_trait;
use crate::conn::{Accepted, Acceptor, HandshakeStream, Holding, HttpBuilder, IntoConfigStream, Listener};
use crate::http::{HttpConnection, Version};
use crate::service::HyperHandler;
Expand Down Expand Up @@ -46,7 +45,6 @@ where
}
}

#[async_trait]
impl<S, C, T, E> Listener for OpensslListener<S, C, T, E>
where
S: IntoConfigStream<C> + Send + 'static,
Expand Down Expand Up @@ -129,7 +127,6 @@ where
}
}

#[async_trait]
impl<S, C, T, E> Acceptor for OpensslAcceptor<S, C, T, E>
where
S: Stream<Item = C> + Send + Unpin + 'static,
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/conn/quinn/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use futures_util::task::noop_waker_ref;
use http::uri::Scheme;
use salvo_http3::http3_quinn::{self, Endpoint};

use crate::async_trait;
use crate::conn::quinn::ServerConfig;
use crate::conn::{Holding, IntoConfigStream};
use crate::http::Version;
Expand Down Expand Up @@ -44,7 +43,6 @@ where
}
}
}
#[async_trait]
impl<S, C, T, E> Listener for QuinnListener<S, C, T, E>
where
S: IntoConfigStream<C> + Send + 'static,
Expand Down Expand Up @@ -100,7 +98,6 @@ where
}
}

#[async_trait]
impl<S, C, E> Acceptor for QuinnAcceptor<S, C, E>
where
S: Stream<Item = C> + Send + Unpin + 'static,
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/conn/rustls/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use futures_util::task::noop_waker_ref;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_rustls::server::TlsStream;

use crate::async_trait;
use crate::conn::Holding;
use crate::conn::{Accepted, HandshakeStream, Acceptor, IntoConfigStream, Listener};
use crate::http::uri::Scheme;
Expand Down Expand Up @@ -44,7 +43,6 @@ where
}
}

#[async_trait]
impl<S, C, T, E> Listener for RustlsListener<S, C, T, E>
where
S: IntoConfigStream<C> + Send + 'static,
Expand Down Expand Up @@ -110,7 +108,6 @@ where
}
}

#[async_trait]
impl<S, C, T, E> Acceptor for RustlsAcceptor<S, C, T, E>
where
S: Stream<Item = C> + Send + Unpin + 'static,
Expand Down
Loading