remove async_trait where possible

This commit is contained in:
Aiden McClelland
2024-04-25 12:13:26 -06:00
parent 3bc2c84c6f
commit 94cf1ff5bf
8 changed files with 71 additions and 53 deletions

View File

@@ -3,6 +3,7 @@ use std::collections::VecDeque;
use std::ffi::OsString; use std::ffi::OsString;
use clap::{CommandFactory, FromArgMatches}; use clap::{CommandFactory, FromArgMatches};
use futures::Future;
use imbl_value::{InOMap, Value}; use imbl_value::{InOMap, Value};
use reqwest::header::{ACCEPT, CONTENT_LENGTH, CONTENT_TYPE}; use reqwest::header::{ACCEPT, CONTENT_LENGTH, CONTENT_TYPE};
use reqwest::{Client, Method}; use reqwest::{Client, Method};
@@ -83,9 +84,12 @@ impl<Context: crate::Context + Clone, Config: CommandFactory + FromArgMatches>
} }
} }
#[async_trait::async_trait]
pub trait CallRemote<RemoteContext>: crate::Context { pub trait CallRemote<RemoteContext>: crate::Context {
async fn call_remote(&self, method: &str, params: Value) -> Result<Value, RpcError>; fn call_remote(
&self,
method: &str,
params: Value,
) -> impl Future<Output = Result<Value, RpcError>> + Send;
} }
pub async fn call_remote_http( pub async fn call_remote_http(
@@ -206,7 +210,7 @@ where
type Ok = RemoteHandler::Ok; type Ok = RemoteHandler::Ok;
type Err = RemoteHandler::Err; type Err = RemoteHandler::Err;
} }
#[async_trait::async_trait]
impl<Context, RemoteHandler> Handler for CallRemoteHandler<Context, RemoteHandler> impl<Context, RemoteHandler> Handler for CallRemoteHandler<Context, RemoteHandler>
where where
Context: CallRemote<RemoteHandler::Context>, Context: CallRemote<RemoteHandler::Context>,

View File

@@ -134,7 +134,7 @@ impl<H: HandlerTypes> HandlerTypes for NoDisplay<H> {
type Ok = H::Ok; type Ok = H::Ok;
type Err = H::Err; type Err = H::Err;
} }
#[async_trait::async_trait]
impl<H> Handler for NoDisplay<H> impl<H> Handler for NoDisplay<H>
where where
H: Handler, H: Handler,
@@ -221,7 +221,7 @@ where
type Ok = H::Ok; type Ok = H::Ok;
type Err = H::Err; type Err = H::Err;
} }
#[async_trait::async_trait]
impl<P, H> Handler for CustomDisplay<P, H> impl<P, H> Handler for CustomDisplay<P, H>
where where
H: Handler, H: Handler,
@@ -355,7 +355,7 @@ where
type Ok = H::Ok; type Ok = H::Ok;
type Err = H::Err; type Err = H::Err;
} }
#[async_trait::async_trait]
impl<F, H, Context> Handler for CustomDisplayFn<F, H, Context> impl<F, H, Context> Handler for CustomDisplayFn<F, H, Context>
where where
Context: Send + Sync + 'static, Context: Send + Sync + 'static,
@@ -478,7 +478,7 @@ where
type Ok = H::Ok; type Ok = H::Ok;
type Err = H::Err; type Err = H::Err;
} }
#[async_trait::async_trait]
impl<Context, H> Handler for RemoteCaller<Context, H> impl<Context, H> Handler for RemoteCaller<Context, H>
where where
Context: CallRemote<H::Context>, Context: CallRemote<H::Context>,
@@ -610,7 +610,7 @@ where
type Ok = H::Ok; type Ok = H::Ok;
type Err = H::Err; type Err = H::Err;
} }
#[async_trait::async_trait]
impl<Params, InheritedParams, H, F> Handler for InheritanceHandler<Params, InheritedParams, H, F> impl<Params, InheritedParams, H, F> Handler for InheritanceHandler<Params, InheritedParams, H, F>
where where
Params: Send + Sync + 'static, Params: Send + Sync + 'static,

View File

@@ -151,7 +151,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<F, T, E, Context, Params, InheritedParams> Handler impl<F, T, E, Context, Params, InheritedParams> Handler
for FromFn<F, T, E, HandlerArgs<Context, Params, InheritedParams>> for FromFn<F, T, E, HandlerArgs<Context, Params, InheritedParams>>
where where
@@ -203,7 +203,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<F, Fut, T, E, Context, Params, InheritedParams> Handler impl<F, Fut, T, E, Context, Params, InheritedParams> Handler
for FromFnAsync<F, Fut, T, E, HandlerArgs<Context, Params, InheritedParams>> for FromFnAsync<F, Fut, T, E, HandlerArgs<Context, Params, InheritedParams>>
where where
@@ -238,7 +238,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<F, T, E> Handler for FromFn<F, T, E, ()> impl<F, T, E> Handler for FromFn<F, T, E, ()>
where where
F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static, F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static,
@@ -275,7 +275,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, ()> impl<F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, ()>
where where
F: Fn() -> Fut + Send + Sync + Clone + 'static, F: Fn() -> Fut + Send + Sync + Clone + 'static,
@@ -307,7 +307,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<Context, F, T, E> Handler for FromFn<F, T, E, (Context,)> impl<Context, F, T, E> Handler for FromFn<F, T, E, (Context,)>
where where
Context: IntoContext, Context: IntoContext,
@@ -349,7 +349,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<Context, F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, (Context,)> impl<Context, F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, (Context,)>
where where
Context: IntoContext, Context: IntoContext,
@@ -383,7 +383,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<Context, F, T, E, Params> Handler for FromFn<F, T, E, (Context, Params)> impl<Context, F, T, E, Params> Handler for FromFn<F, T, E, (Context, Params)>
where where
Context: IntoContext, Context: IntoContext,
@@ -430,7 +430,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<Context, F, Fut, T, E, Params> Handler for FromFnAsync<F, Fut, T, E, (Context, Params)> impl<Context, F, Fut, T, E, Params> Handler for FromFnAsync<F, Fut, T, E, (Context, Params)>
where where
Context: IntoContext, Context: IntoContext,
@@ -470,7 +470,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<Context, F, T, E, Params, InheritedParams> Handler impl<Context, F, T, E, Params, InheritedParams> Handler
for FromFn<F, T, E, (Context, Params, InheritedParams)> for FromFn<F, T, E, (Context, Params, InheritedParams)>
where where
@@ -524,7 +524,7 @@ where
type Ok = T; type Ok = T;
type Err = E; type Err = E;
} }
#[async_trait::async_trait]
impl<Context, F, Fut, T, E, Params, InheritedParams> Handler impl<Context, F, Fut, T, E, Params, InheritedParams> Handler
for FromFnAsync<F, Fut, T, E, (Context, Params, InheritedParams)> for FromFnAsync<F, Fut, T, E, (Context, Params, InheritedParams)>
where where

View File

@@ -4,6 +4,7 @@ use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches, Parser}; use clap::{ArgMatches, Command, CommandFactory, FromArgMatches, Parser};
use futures::Future;
use imbl_value::imbl::{OrdMap, OrdSet}; use imbl_value::imbl::{OrdMap, OrdSet};
use imbl_value::Value; use imbl_value::Value;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
@@ -267,7 +268,6 @@ pub trait HandlerTypes {
type Err: Send + Sync; type Err: Send + Sync;
} }
#[async_trait::async_trait]
pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static { pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
type Context: IntoContext; type Context: IntoContext;
fn handle_sync( fn handle_sync(
@@ -279,27 +279,29 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
.runtime() .runtime()
.block_on(self.handle_async(handle_args)) .block_on(self.handle_async(handle_args))
} }
async fn handle_async( fn handle_async(
&self, &self,
handle_args: HandlerArgsFor<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err>; ) -> impl Future<Output = Result<Self::Ok, Self::Err>> + Send;
async fn handle_async_with_sync( fn handle_async_with_sync<'a>(
&self, &'a self,
handle_args: HandlerArgsFor<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> impl Future<Output = Result<Self::Ok, Self::Err>> + Send + 'a {
self.handle_sync(handle_args) async move { self.handle_sync(handle_args) }
} }
async fn handle_async_with_sync_blocking( fn handle_async_with_sync_blocking<'a>(
&self, &'a self,
handle_args: HandlerArgsFor<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> impl Future<Output = Result<Self::Ok, Self::Err>> + Send + 'a {
let s = self.clone(); async move {
handle_args let s = self.clone();
.context handle_args
.runtime() .context
.spawn_blocking(move || s.handle_sync(handle_args)) .runtime()
.await .spawn_blocking(move || s.handle_sync(handle_args))
.unwrap() .await
.unwrap()
}
} }
#[allow(unused_variables)] #[allow(unused_variables)]
fn metadata( fn metadata(

View File

@@ -181,7 +181,7 @@ where
type Ok = Value; type Ok = Value;
type Err = RpcError; type Err = RpcError;
} }
#[async_trait::async_trait]
impl<Params, InheritedParams> Handler for ParentHandler<Params, InheritedParams> impl<Params, InheritedParams> Handler for ParentHandler<Params, InheritedParams>
where where
Params: Send + Sync + 'static, Params: Send + Sync + 'static,

View File

@@ -5,7 +5,7 @@ use axum::extract::Request;
use axum::handler::Handler; use axum::handler::Handler;
use axum::response::Response; use axum::response::Response;
use futures::future::{join_all, BoxFuture}; use futures::future::{join_all, BoxFuture};
use futures::FutureExt; use futures::{Future, FutureExt};
use http::header::{CONTENT_LENGTH, CONTENT_TYPE}; use http::header::{CONTENT_LENGTH, CONTENT_TYPE};
use http_body_util::BodyExt; use http_body_util::BodyExt;
use imbl_value::imbl::Vector; use imbl_value::imbl::Vector;
@@ -40,30 +40,41 @@ pub fn json_http_response<T: Serialize>(t: &T) -> Response {
.unwrap_or_else(|_| fallback_rpc_error_response()) .unwrap_or_else(|_| fallback_rpc_error_response())
} }
#[async_trait::async_trait]
pub trait Middleware<Context: Send + 'static>: Clone + Send + Sync + 'static { pub trait Middleware<Context: Send + 'static>: Clone + Send + Sync + 'static {
type Metadata: DeserializeOwned + Send + 'static; type Metadata: DeserializeOwned + Send + 'static;
#[allow(unused_variables)] #[allow(unused_variables)]
async fn process_http_request( fn process_http_request(
&mut self, &mut self,
context: &Context, context: &Context,
request: &mut Request, request: &mut Request,
) -> Result<(), Response> { ) -> impl Future<Output = Result<(), Response>> + Send {
Ok(()) async { Ok(()) }
} }
#[allow(unused_variables)] #[allow(unused_variables)]
async fn process_rpc_request( fn process_rpc_request(
&mut self, &mut self,
context: &Context, context: &Context,
metadata: Self::Metadata, metadata: Self::Metadata,
request: &mut RpcRequest, request: &mut RpcRequest,
) -> Result<(), RpcResponse> { ) -> impl Future<Output = Result<(), RpcResponse>> + Send {
Ok(()) async { Ok(()) }
} }
#[allow(unused_variables)] #[allow(unused_variables)]
async fn process_rpc_response(&mut self, context: &Context, response: &mut RpcResponse) {} fn process_rpc_response(
&mut self,
context: &Context,
response: &mut RpcResponse,
) -> impl Future<Output = ()> + Send {
async { () }
}
#[allow(unused_variables)] #[allow(unused_variables)]
async fn process_http_response(&mut self, context: &Context, response: &mut Response) {} fn process_http_response(
&mut self,
context: &Context,
response: &mut Response,
) -> impl Future<Output = ()> + Send {
async { () }
}
} }
#[allow(private_bounds)] #[allow(private_bounds)]
@@ -101,7 +112,7 @@ impl<Context: Send + 'static, T: Middleware<Context> + Send + Sync> _Middleware<
context: &'a Context, context: &'a Context,
request: &'a mut Request, request: &'a mut Request,
) -> BoxFuture<'a, Result<(), Response>> { ) -> BoxFuture<'a, Result<(), Response>> {
<Self as Middleware<Context>>::process_http_request(self, context, request) <Self as Middleware<Context>>::process_http_request(self, context, request).boxed()
} }
fn process_rpc_request<'a>( fn process_rpc_request<'a>(
&'a mut self, &'a mut self,
@@ -118,20 +129,21 @@ impl<Context: Send + 'static, T: Middleware<Context> + Send + Sync> _Middleware<
}, },
request, request,
) )
.boxed()
} }
fn process_rpc_response<'a>( fn process_rpc_response<'a>(
&'a mut self, &'a mut self,
context: &'a Context, context: &'a Context,
response: &'a mut RpcResponse, response: &'a mut RpcResponse,
) -> BoxFuture<'a, ()> { ) -> BoxFuture<'a, ()> {
<Self as Middleware<Context>>::process_rpc_response(self, context, response) <Self as Middleware<Context>>::process_rpc_response(self, context, response).boxed()
} }
fn process_http_response<'a>( fn process_http_response<'a>(
&'a mut self, &'a mut self,
context: &'a Context, context: &'a Context,
response: &'a mut Response, response: &'a mut Response,
) -> BoxFuture<'a, ()> { ) -> BoxFuture<'a, ()> {
<Self as Middleware<Context>>::process_http_response(self, context, response) <Self as Middleware<Context>>::process_http_response(self, context, response).boxed()
} }
} }

View File

@@ -60,7 +60,7 @@ impl Context for CliContext {
self.0.rt.get().unwrap().handle().clone() self.0.rt.get().unwrap().handle().clone()
} }
} }
#[async_trait::async_trait]
impl CallRemote<ServerContext> for CliContext { impl CallRemote<ServerContext> for CliContext {
async fn call_remote(&self, method: &str, params: Value) -> Result<Value, RpcError> { async fn call_remote(&self, method: &str, params: Value) -> Result<Value, RpcError> {
call_remote_socket( call_remote_socket(

View File

@@ -19,14 +19,14 @@
// impl Context for CliContext { // impl Context for CliContext {
// type Metadata = (); // type Metadata = ();
// } // }
// #[async_trait::async_trait] //
// impl CliContextSocket for CliContext { // impl CliContextSocket for CliContext {
// type Stream = UnixStream; // type Stream = UnixStream;
// async fn connect(&self) -> std::io::Result<Self::Stream> { // async fn connect(&self) -> std::io::Result<Self::Stream> {
// UnixStream::connect(&self.0).await // UnixStream::connect(&self.0).await
// } // }
// } // }
// #[async_trait::async_trait] //
// impl rpc_toolkit::CliContext for CliContext { // impl rpc_toolkit::CliContext for CliContext {
// async fn call_remote( // async fn call_remote(
// &self, // &self,
@@ -92,7 +92,7 @@
// println!("{}", res); // println!("{}", res);
// } // }
// } // }
// #[async_trait::async_trait] //
// impl AsyncCommand<ServerContext> for Thing1 { // impl AsyncCommand<ServerContext> for Thing1 {
// async fn implementation( // async fn implementation(
// self, // self,