From 94cf1ff5bf5bf0b29e1891fc70b9ca7ec0834173 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Thu, 25 Apr 2024 12:13:26 -0600 Subject: [PATCH] remove async_trait where possible --- rpc-toolkit/src/cli.rs | 10 +++++--- rpc-toolkit/src/handler/adapters.rs | 10 ++++---- rpc-toolkit/src/handler/from_fn.rs | 20 +++++++-------- rpc-toolkit/src/handler/mod.rs | 36 ++++++++++++++------------- rpc-toolkit/src/handler/parent.rs | 2 +- rpc-toolkit/src/server/http.rs | 38 +++++++++++++++++++---------- rpc-toolkit/tests/handler.rs | 2 +- rpc-toolkit/tests/test.rs | 6 ++--- 8 files changed, 71 insertions(+), 53 deletions(-) diff --git a/rpc-toolkit/src/cli.rs b/rpc-toolkit/src/cli.rs index 7ba0cac..11f9a9c 100644 --- a/rpc-toolkit/src/cli.rs +++ b/rpc-toolkit/src/cli.rs @@ -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 } } -#[async_trait::async_trait] pub trait CallRemote: crate::Context { - async fn call_remote(&self, method: &str, params: Value) -> Result; + fn call_remote( + &self, + method: &str, + params: Value, + ) -> impl Future> + 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 Handler for CallRemoteHandler where Context: CallRemote, diff --git a/rpc-toolkit/src/handler/adapters.rs b/rpc-toolkit/src/handler/adapters.rs index fc40baa..7d9ad75 100644 --- a/rpc-toolkit/src/handler/adapters.rs +++ b/rpc-toolkit/src/handler/adapters.rs @@ -134,7 +134,7 @@ impl HandlerTypes for NoDisplay { type Ok = H::Ok; type Err = H::Err; } -#[async_trait::async_trait] + impl Handler for NoDisplay where H: Handler, @@ -221,7 +221,7 @@ where type Ok = H::Ok; type Err = H::Err; } -#[async_trait::async_trait] + impl Handler for CustomDisplay where H: Handler, @@ -355,7 +355,7 @@ where type Ok = H::Ok; type Err = H::Err; } -#[async_trait::async_trait] + impl Handler for CustomDisplayFn where Context: Send + Sync + 'static, @@ -478,7 +478,7 @@ where type Ok = H::Ok; type Err = H::Err; } -#[async_trait::async_trait] + impl Handler for RemoteCaller where Context: CallRemote, @@ -610,7 +610,7 @@ where type Ok = H::Ok; type Err = H::Err; } -#[async_trait::async_trait] + impl Handler for InheritanceHandler where Params: Send + Sync + 'static, diff --git a/rpc-toolkit/src/handler/from_fn.rs b/rpc-toolkit/src/handler/from_fn.rs index 02144b8..c77a0f2 100644 --- a/rpc-toolkit/src/handler/from_fn.rs +++ b/rpc-toolkit/src/handler/from_fn.rs @@ -151,7 +151,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFn> where @@ -203,7 +203,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFnAsync> where @@ -238,7 +238,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFn where F: Fn() -> Result + Send + Sync + Clone + 'static, @@ -275,7 +275,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFnAsync where F: Fn() -> Fut + Send + Sync + Clone + 'static, @@ -307,7 +307,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFn where Context: IntoContext, @@ -349,7 +349,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFnAsync where Context: IntoContext, @@ -383,7 +383,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFn where Context: IntoContext, @@ -430,7 +430,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFnAsync where Context: IntoContext, @@ -470,7 +470,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFn where @@ -524,7 +524,7 @@ where type Ok = T; type Err = E; } -#[async_trait::async_trait] + impl Handler for FromFnAsync where diff --git a/rpc-toolkit/src/handler/mod.rs b/rpc-toolkit/src/handler/mod.rs index 1a5a643..31d9587 100644 --- a/rpc-toolkit/src/handler/mod.rs +++ b/rpc-toolkit/src/handler/mod.rs @@ -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, - ) -> Result; - async fn handle_async_with_sync( - &self, + ) -> impl Future> + Send; + fn handle_async_with_sync<'a>( + &'a self, handle_args: HandlerArgsFor, - ) -> Result { - self.handle_sync(handle_args) + ) -> impl Future> + 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, - ) -> Result { - let s = self.clone(); - handle_args - .context - .runtime() - .spawn_blocking(move || s.handle_sync(handle_args)) - .await - .unwrap() + ) -> impl Future> + 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( diff --git a/rpc-toolkit/src/handler/parent.rs b/rpc-toolkit/src/handler/parent.rs index 0b61eab..9220d15 100644 --- a/rpc-toolkit/src/handler/parent.rs +++ b/rpc-toolkit/src/handler/parent.rs @@ -181,7 +181,7 @@ where type Ok = Value; type Err = RpcError; } -#[async_trait::async_trait] + impl Handler for ParentHandler where Params: Send + Sync + 'static, diff --git a/rpc-toolkit/src/server/http.rs b/rpc-toolkit/src/server/http.rs index f433825..22cebb1 100644 --- a/rpc-toolkit/src/server/http.rs +++ b/rpc-toolkit/src/server/http.rs @@ -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: &T) -> Response { .unwrap_or_else(|_| fallback_rpc_error_response()) } -#[async_trait::async_trait] pub trait Middleware: 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> + 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> + 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 + 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 + Send { + async { () } + } } #[allow(private_bounds)] @@ -101,7 +112,7 @@ impl + Send + Sync> _Middleware< context: &'a Context, request: &'a mut Request, ) -> BoxFuture<'a, Result<(), Response>> { - >::process_http_request(self, context, request) + >::process_http_request(self, context, request).boxed() } fn process_rpc_request<'a>( &'a mut self, @@ -118,20 +129,21 @@ impl + Send + Sync> _Middleware< }, request, ) + .boxed() } fn process_rpc_response<'a>( &'a mut self, context: &'a Context, response: &'a mut RpcResponse, ) -> BoxFuture<'a, ()> { - >::process_rpc_response(self, context, response) + >::process_rpc_response(self, context, response).boxed() } fn process_http_response<'a>( &'a mut self, context: &'a Context, response: &'a mut Response, ) -> BoxFuture<'a, ()> { - >::process_http_response(self, context, response) + >::process_http_response(self, context, response).boxed() } } diff --git a/rpc-toolkit/tests/handler.rs b/rpc-toolkit/tests/handler.rs index 380294e..ea599c5 100644 --- a/rpc-toolkit/tests/handler.rs +++ b/rpc-toolkit/tests/handler.rs @@ -60,7 +60,7 @@ impl Context for CliContext { self.0.rt.get().unwrap().handle().clone() } } -#[async_trait::async_trait] + impl CallRemote for CliContext { async fn call_remote(&self, method: &str, params: Value) -> Result { call_remote_socket( diff --git a/rpc-toolkit/tests/test.rs b/rpc-toolkit/tests/test.rs index bc555b0..5ef6b81 100644 --- a/rpc-toolkit/tests/test.rs +++ b/rpc-toolkit/tests/test.rs @@ -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 { // 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 for Thing1 { // async fn implementation( // self,