diff options
author | Norbert Preining <norbert@preining.info> | 2020-05-28 03:03:21 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2020-05-28 03:03:21 +0000 |
commit | 745892fbddea56040139108277e728b53fd8fc11 (patch) | |
tree | 15e55e299b38fccdabce8364f1fbf4f02ef37994 /support/texlab/crates/jsonrpc_derive/src | |
parent | 195ca7e0c377d83455867bdd8e409d4e1cf024ea (diff) |
CTAN sync 202005280303
Diffstat (limited to 'support/texlab/crates/jsonrpc_derive/src')
-rw-r--r-- | support/texlab/crates/jsonrpc_derive/src/lib.rs | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/support/texlab/crates/jsonrpc_derive/src/lib.rs b/support/texlab/crates/jsonrpc_derive/src/lib.rs index fe4da8d4b6..283be858db 100644 --- a/support/texlab/crates/jsonrpc_derive/src/lib.rs +++ b/support/texlab/crates/jsonrpc_derive/src/lib.rs @@ -4,9 +4,7 @@ extern crate proc_macro; use proc_macro::TokenStream; use quote::quote; -use std::str::FromStr; -use syn::export::TokenStream2; -use syn::*; +use syn::{export::TokenStream2, *}; macro_rules! unwrap { ($input:expr, $arm:pat => $value:expr) => {{ @@ -67,8 +65,8 @@ pub fn jsonrpc_server(_attr: TokenStream, item: TokenStream) -> TokenStream { let tokens = quote! { #impl_ + #[async_trait::async_trait] impl #generics jsonrpc::RequestHandler for #self_ty { - #[boxed] async fn handle_request(&self, request: jsonrpc::Request) -> jsonrpc::Response { use jsonrpc::*; @@ -80,7 +78,7 @@ pub fn jsonrpc_server(_attr: TokenStream, item: TokenStream) -> TokenStream { } } - fn handle_notification(&self, notification: jsonrpc::Notification) { + async fn handle_notification(&self, notification: jsonrpc::Notification) { match notification.method.as_str() { #(#notifications),*, _ => log::warn!("{}: {}", "Method not found", notification.method), @@ -94,7 +92,6 @@ pub fn jsonrpc_server(_attr: TokenStream, item: TokenStream) -> TokenStream { #[proc_macro_attribute] pub fn jsonrpc_client(attr: TokenStream, item: TokenStream) -> TokenStream { - let item = TokenStream::from_str(&item.to_string().replace("async ", "")).unwrap(); let trait_: ItemTrait = parse_macro_input!(item); let trait_ident = &trait_.ident; let stubs = generate_client_stubs(&trait_.items); @@ -102,6 +99,7 @@ pub fn jsonrpc_client(attr: TokenStream, item: TokenStream) -> TokenStream { let struct_ident = unwrap!(attr.first().unwrap(), NestedMeta::Meta(Meta::Path(x)) => x); let tokens = quote! { + #[async_trait::async_trait] #trait_ pub struct #struct_ident { @@ -117,14 +115,15 @@ pub fn jsonrpc_client(attr: TokenStream, item: TokenStream) -> TokenStream { } } + #[async_trait::async_trait] impl #trait_ident for #struct_ident { #(#stubs)* } + #[async_trait::async_trait] impl jsonrpc::ResponseHandler for #struct_ident { - #[boxed] async fn handle(&self, response: jsonrpc::Response) -> () { self.client.handle(response).await } @@ -163,11 +162,11 @@ fn generate_server_skeletons(items: &Vec<ImplItem>) -> (Vec<TokenStream2>, Vec<T MethodKind::Notification => { notifications.push(quote!( #name => { - let handler = move |param: #param_ty| { - self.#ident(param); + let handler = |param: #param_ty| async move { + self.#ident(param).await; }; - jsonrpc::handle_notification(notification, handler); + jsonrpc::handle_notification(notification, handler).await; } )); } @@ -189,14 +188,12 @@ fn generate_client_stubs(items: &Vec<TraitItem>) -> Vec<TokenStream2> { let stub = match meta.kind { MethodKind::Request => quote!( - #[boxed] #sig { let result = self.client.send_request(#name.to_owned(), #param).await?; serde_json::from_value(result).map_err(|_| jsonrpc::Error::deserialize_error()) } ), MethodKind::Notification => quote!( - #[boxed] #sig { self.client.send_notification(#name.to_owned(), #param).await } |