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 clap::{CommandFactory, FromArgMatches};
use futures::Future;
use imbl_value::{InOMap, Value};
use reqwest::header::{ACCEPT, CONTENT_LENGTH, CONTENT_TYPE};
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 {
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(
@@ -206,7 +210,7 @@ where
type Ok = RemoteHandler::Ok;
type Err = RemoteHandler::Err;
}
#[async_trait::async_trait]
impl<Context, RemoteHandler> Handler for CallRemoteHandler<Context, RemoteHandler>
where
Context: CallRemote<RemoteHandler::Context>,

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,7 +5,7 @@ use axum::extract::Request;
use axum::handler::Handler;
use axum::response::Response;
use futures::future::{join_all, BoxFuture};
use futures::FutureExt;
use futures::{Future, FutureExt};
use http::header::{CONTENT_LENGTH, CONTENT_TYPE};
use http_body_util::BodyExt;
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())
}
#[async_trait::async_trait]
pub trait Middleware<Context: Send + 'static>: Clone + Send + Sync + 'static {
type Metadata: DeserializeOwned + Send + 'static;
#[allow(unused_variables)]
async fn process_http_request(
fn process_http_request(
&mut self,
context: &Context,
request: &mut Request,
) -> Result<(), Response> {
Ok(())
) -> impl Future<Output = Result<(), Response>> + Send {
async { Ok(()) }
}
#[allow(unused_variables)]
async fn process_rpc_request(
fn process_rpc_request(
&mut self,
context: &Context,
metadata: Self::Metadata,
request: &mut RpcRequest,
) -> Result<(), RpcResponse> {
Ok(())
) -> impl Future<Output = Result<(), RpcResponse>> + Send {
async { Ok(()) }
}
#[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)]
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)]
@@ -101,7 +112,7 @@ impl<Context: Send + 'static, T: Middleware<Context> + Send + Sync> _Middleware<
context: &'a Context,
request: &'a mut Request,
) -> 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>(
&'a mut self,
@@ -118,20 +129,21 @@ impl<Context: Send + 'static, T: Middleware<Context> + Send + Sync> _Middleware<
},
request,
)
.boxed()
}
fn process_rpc_response<'a>(
&'a mut self,
context: &'a Context,
response: &'a mut RpcResponse,
) -> 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>(
&'a mut self,
context: &'a Context,
response: &'a mut Response,
) -> 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()
}
}
#[async_trait::async_trait]
impl CallRemote<ServerContext> for CliContext {
async fn call_remote(&self, method: &str, params: Value) -> Result<Value, RpcError> {
call_remote_socket(

View File

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