From fb5022b1c0cc7143376540d933a2c9d624edbaa6 Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Mon, 29 Jul 2019 22:08:53 -0700 Subject: [PATCH] cargo fmt --- plugins/src/lib.rs | 143 ++++++++++++++++++++---------- plugins/tests/service.rs | 4 +- tarpc/examples/pubsub.rs | 2 +- tarpc/src/lib.rs | 2 +- tarpc/tests/service_functional.rs | 5 +- 5 files changed, 104 insertions(+), 52 deletions(-) diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index 4d5ef41..6c85758 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -12,16 +12,19 @@ extern crate proc_macro2; extern crate quote; extern crate syn; -use proc_macro2::TokenStream as TokenStream2; -use proc_macro::TokenStream; use itertools::Itertools; +use proc_macro::TokenStream; +use proc_macro2::TokenStream as TokenStream2; use quote::quote; -use syn::{parse_macro_input, parenthesized, braced, Attribute, Ident, FnArg, ArgCaptured, - ReturnType, Pat, Token, Visibility, - parse::{Parse, ParseStream}, - punctuated::Punctuated, - spanned::Spanned, - token::Comma}; +use syn::{ + braced, parenthesized, + parse::{Parse, ParseStream}, + parse_macro_input, + punctuated::Punctuated, + spanned::Spanned, + token::Comma, + ArgCaptured, Attribute, FnArg, Ident, Pat, ReturnType, Token, Visibility, +}; struct Service { attrs: Vec, @@ -49,7 +52,12 @@ impl Parse for Service { while !content.is_empty() { rpcs.push(content.parse()?); } - Ok(Service { attrs, vis, ident, rpcs }) + Ok(Service { + attrs, + vis, + ident, + rpcs, + }) } } @@ -62,26 +70,49 @@ impl Parse for RpcMethod { let content; parenthesized!(content in input); let args: Punctuated = content.parse_terminated(FnArg::parse)?; - let args = args.into_iter().map(|arg| match arg { - FnArg::Captured(captured) => match captured.pat { - Pat::Ident(_) => Ok(captured), - _ => return Err(syn::Error::new( - captured.pat.span(), "patterns aren't allowed in RPC args")) - }, - FnArg::SelfRef(self_ref) => return Err(syn::Error::new( - self_ref.span(), "RPC args cannot start with self")), - FnArg::SelfValue(self_val) => return Err(syn::Error::new( - self_val.span(), "RPC args cannot start with self")), - arg => return Err(syn::Error::new( - arg.span(), "RPC args must be explicitly typed patterns")), - }) - .collect::>()?; + let args = args + .into_iter() + .map(|arg| match arg { + FnArg::Captured(captured) => match captured.pat { + Pat::Ident(_) => Ok(captured), + _ => { + return Err(syn::Error::new( + captured.pat.span(), + "patterns aren't allowed in RPC args", + )) + } + }, + FnArg::SelfRef(self_ref) => { + return Err(syn::Error::new( + self_ref.span(), + "RPC args cannot start with self", + )) + } + FnArg::SelfValue(self_val) => { + return Err(syn::Error::new( + self_val.span(), + "RPC args cannot start with self", + )) + } + arg => { + return Err(syn::Error::new( + arg.span(), + "RPC args must be explicitly typed patterns", + )) + } + }) + .collect::>()?; let output = input.parse()?; input.parse::()?; - Ok(RpcMethod { attrs, ident, args, output, }) - } + Ok(RpcMethod { + attrs, + ident, + args, + output, + }) } +} #[proc_macro_attribute] pub fn service(attr: TokenStream, input: TokenStream) -> TokenStream { @@ -93,46 +124,68 @@ pub fn service(attr: TokenStream, input: TokenStream) -> TokenStream { } parse_macro_input!(attr as EmptyArgs); - let Service { attrs, vis, ident, rpcs } = parse_macro_input!(input as Service); + let Service { + attrs, + vis, + ident, + rpcs, + } = parse_macro_input!(input as Service); - let camel_case_fn_names: Vec = rpcs.iter() + let camel_case_fn_names: Vec = rpcs + .iter() .map(|rpc| convert_str(&rpc.ident.to_string())) .collect(); - let ref outputs: Vec = rpcs.iter().map(|rpc| match rpc.output { + let ref outputs: Vec = rpcs + .iter() + .map(|rpc| match rpc.output { ReturnType::Type(_, ref ty) => quote!(#ty), ReturnType::Default => quote!(()), }) - .collect(); - let future_types: Vec = camel_case_fn_names.iter() - .map(|name| Ident::new(&format!("{}Fut", name), ident.span())) - .collect(); - let ref camel_case_idents: Vec = rpcs.iter().zip(camel_case_fn_names.iter()) + .collect(); + let future_types: Vec = camel_case_fn_names + .iter() + .map(|name| Ident::new(&format!("{}Fut", name), ident.span())) + .collect(); + let ref camel_case_idents: Vec = rpcs + .iter() + .zip(camel_case_fn_names.iter()) .map(|(rpc, name)| Ident::new(name, rpc.ident.span())) .collect(); let camel_case_idents2 = camel_case_idents; let ref args: Vec<&Punctuated> = rpcs.iter().map(|rpc| &rpc.args).collect(); - let ref arg_vars: Vec> = - args.iter() + let ref arg_vars: Vec> = args + .iter() .map(|args| args.iter().map(|arg| &arg.pat).collect()) .collect(); let arg_vars2 = arg_vars; let ref method_names: Vec<&Ident> = rpcs.iter().map(|rpc| &rpc.ident).collect(); let method_attrs: Vec<_> = rpcs.iter().map(|rpc| &rpc.attrs).collect(); - let types_and_fns = rpcs.iter() + let types_and_fns = rpcs + .iter() .zip(future_types.iter()) .zip(outputs.iter()) - .map(|((RpcMethod { attrs, ident, args, .. }, future_type), output)| { - let ty_doc = format!("The response future returned by {}.", ident); - quote! { - #[doc = #ty_doc] - type #future_type: std::future::Future; + .map( + |( + ( + RpcMethod { + attrs, ident, args, .. + }, + future_type, + ), + output, + )| { + let ty_doc = format!("The response future returned by {}.", ident); + quote! { + #[doc = #ty_doc] + type #future_type: std::future::Future; - #( #attrs )* - fn #ident(self, context: tarpc::context::Context, #args) -> Self::#future_type; - } - }); + #( #attrs )* + fn #ident(self, context: tarpc::context::Context, #args) -> Self::#future_type; + } + }, + ); let service_name_repeated = std::iter::repeat(ident.clone()); let service_name_repeated2 = service_name_repeated.clone(); diff --git a/plugins/tests/service.rs b/plugins/tests/service.rs index 2c51251..fde4e10 100644 --- a/plugins/tests/service.rs +++ b/plugins/tests/service.rs @@ -39,7 +39,7 @@ fn syntax() { #[allow(non_snake_case)] async fn TestCamelCaseDoesntConflict(); async fn hello() -> String; - #[doc="attr"] + #[doc = "attr"] async fn attr(s: String) -> String; async fn no_args_no_return(); async fn no_args() -> (); @@ -49,7 +49,7 @@ fn syntax() { async fn no_args_ret_error() -> i32; async fn one_arg_ret_error(foo: String) -> String; async fn no_arg_implicit_return_error(); - #[doc="attr"] + #[doc = "attr"] async fn one_arg_implicit_return_error(foo: String); } } diff --git a/tarpc/examples/pubsub.rs b/tarpc/examples/pubsub.rs index 35117c2..8c57b9b 100644 --- a/tarpc/examples/pubsub.rs +++ b/tarpc/examples/pubsub.rs @@ -35,8 +35,8 @@ pub mod subscriber { } pub mod publisher { - pub use ServiceClient as Client; use std::net::SocketAddr; + pub use ServiceClient as Client; #[tarpc::service] pub trait Service { diff --git a/tarpc/src/lib.rs b/tarpc/src/lib.rs index e5948a9..ba11314 100644 --- a/tarpc/src/lib.rs +++ b/tarpc/src/lib.rs @@ -9,6 +9,7 @@ #![feature(async_await, external_doc)] #![cfg_attr(test, feature(proc_macro_hygiene))] +pub use rpc::*; /// The main macro that creates RPC services. /// /// Rpc methods are specified, mirroring trait syntax: @@ -34,4 +35,3 @@ /// * `Client` -- a client stub with a fn for each RPC. /// * `fn new_stub` -- creates a new Client stub. pub use tarpc_plugins::service; -pub use rpc::*; diff --git a/tarpc/tests/service_functional.rs b/tarpc/tests/service_functional.rs index 9f5d346..4ac5ef7 100644 --- a/tarpc/tests/service_functional.rs +++ b/tarpc/tests/service_functional.rs @@ -5,10 +5,9 @@ use futures::{ future::{ready, Ready}, prelude::*, }; -use tarpc::{client, context, server::Handler, transport::channel}; #[cfg(feature = "serde1")] use std::io; - +use tarpc::{client, context, server::Handler, transport::channel}; #[tarpc_plugins::service] trait Service { @@ -65,7 +64,7 @@ async fn serde() -> io::Result<()> { let _ = runtime::spawn( tarpc::Server::default() .incoming(transport.take(1).filter_map(|r| async { r.ok() })) - .respond_with(serve(Server)) + .respond_with(serve(Server)), ); let transport = bincode_transport::connect(&addr).await?;