mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-02-23 15:49:54 +01:00
cargo fmt
This commit is contained in:
@@ -12,16 +12,19 @@ extern crate proc_macro2;
|
|||||||
extern crate quote;
|
extern crate quote;
|
||||||
extern crate syn;
|
extern crate syn;
|
||||||
|
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
|
||||||
use proc_macro::TokenStream;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{parse_macro_input, parenthesized, braced, Attribute, Ident, FnArg, ArgCaptured,
|
use syn::{
|
||||||
ReturnType, Pat, Token, Visibility,
|
braced, parenthesized,
|
||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
punctuated::Punctuated,
|
parse_macro_input,
|
||||||
spanned::Spanned,
|
punctuated::Punctuated,
|
||||||
token::Comma};
|
spanned::Spanned,
|
||||||
|
token::Comma,
|
||||||
|
ArgCaptured, Attribute, FnArg, Ident, Pat, ReturnType, Token, Visibility,
|
||||||
|
};
|
||||||
|
|
||||||
struct Service {
|
struct Service {
|
||||||
attrs: Vec<Attribute>,
|
attrs: Vec<Attribute>,
|
||||||
@@ -49,7 +52,12 @@ impl Parse for Service {
|
|||||||
while !content.is_empty() {
|
while !content.is_empty() {
|
||||||
rpcs.push(content.parse()?);
|
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;
|
let content;
|
||||||
parenthesized!(content in input);
|
parenthesized!(content in input);
|
||||||
let args: Punctuated<FnArg, Comma> = content.parse_terminated(FnArg::parse)?;
|
let args: Punctuated<FnArg, Comma> = content.parse_terminated(FnArg::parse)?;
|
||||||
let args = args.into_iter().map(|arg| match arg {
|
let args = args
|
||||||
FnArg::Captured(captured) => match captured.pat {
|
.into_iter()
|
||||||
Pat::Ident(_) => Ok(captured),
|
.map(|arg| match arg {
|
||||||
_ => return Err(syn::Error::new(
|
FnArg::Captured(captured) => match captured.pat {
|
||||||
captured.pat.span(), "patterns aren't allowed in RPC args"))
|
Pat::Ident(_) => Ok(captured),
|
||||||
},
|
_ => {
|
||||||
FnArg::SelfRef(self_ref) => return Err(syn::Error::new(
|
return Err(syn::Error::new(
|
||||||
self_ref.span(), "RPC args cannot start with self")),
|
captured.pat.span(),
|
||||||
FnArg::SelfValue(self_val) => return Err(syn::Error::new(
|
"patterns aren't allowed in RPC args",
|
||||||
self_val.span(), "RPC args cannot start with self")),
|
))
|
||||||
arg => return Err(syn::Error::new(
|
}
|
||||||
arg.span(), "RPC args must be explicitly typed patterns")),
|
},
|
||||||
})
|
FnArg::SelfRef(self_ref) => {
|
||||||
.collect::<Result<_, _>>()?;
|
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::<Result<_, _>>()?;
|
||||||
let output = input.parse()?;
|
let output = input.parse()?;
|
||||||
input.parse::<Token![;]>()?;
|
input.parse::<Token![;]>()?;
|
||||||
|
|
||||||
Ok(RpcMethod { attrs, ident, args, output, })
|
Ok(RpcMethod {
|
||||||
}
|
attrs,
|
||||||
|
ident,
|
||||||
|
args,
|
||||||
|
output,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn service(attr: TokenStream, input: TokenStream) -> TokenStream {
|
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);
|
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<String> = rpcs.iter()
|
let camel_case_fn_names: Vec<String> = rpcs
|
||||||
|
.iter()
|
||||||
.map(|rpc| convert_str(&rpc.ident.to_string()))
|
.map(|rpc| convert_str(&rpc.ident.to_string()))
|
||||||
.collect();
|
.collect();
|
||||||
let ref outputs: Vec<TokenStream2> = rpcs.iter().map(|rpc| match rpc.output {
|
let ref outputs: Vec<TokenStream2> = rpcs
|
||||||
|
.iter()
|
||||||
|
.map(|rpc| match rpc.output {
|
||||||
ReturnType::Type(_, ref ty) => quote!(#ty),
|
ReturnType::Type(_, ref ty) => quote!(#ty),
|
||||||
ReturnType::Default => quote!(()),
|
ReturnType::Default => quote!(()),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let future_types: Vec<Ident> = camel_case_fn_names.iter()
|
let future_types: Vec<Ident> = camel_case_fn_names
|
||||||
.map(|name| Ident::new(&format!("{}Fut", name), ident.span()))
|
.iter()
|
||||||
.collect();
|
.map(|name| Ident::new(&format!("{}Fut", name), ident.span()))
|
||||||
let ref camel_case_idents: Vec<Ident> = rpcs.iter().zip(camel_case_fn_names.iter())
|
.collect();
|
||||||
|
let ref camel_case_idents: Vec<Ident> = rpcs
|
||||||
|
.iter()
|
||||||
|
.zip(camel_case_fn_names.iter())
|
||||||
.map(|(rpc, name)| Ident::new(name, rpc.ident.span()))
|
.map(|(rpc, name)| Ident::new(name, rpc.ident.span()))
|
||||||
.collect();
|
.collect();
|
||||||
let camel_case_idents2 = camel_case_idents;
|
let camel_case_idents2 = camel_case_idents;
|
||||||
|
|
||||||
let ref args: Vec<&Punctuated<ArgCaptured, Comma>> = rpcs.iter().map(|rpc| &rpc.args).collect();
|
let ref args: Vec<&Punctuated<ArgCaptured, Comma>> = rpcs.iter().map(|rpc| &rpc.args).collect();
|
||||||
let ref arg_vars: Vec<Punctuated<&Pat, Comma>> =
|
let ref arg_vars: Vec<Punctuated<&Pat, Comma>> = args
|
||||||
args.iter()
|
.iter()
|
||||||
.map(|args| args.iter().map(|arg| &arg.pat).collect())
|
.map(|args| args.iter().map(|arg| &arg.pat).collect())
|
||||||
.collect();
|
.collect();
|
||||||
let arg_vars2 = arg_vars;
|
let arg_vars2 = arg_vars;
|
||||||
let ref method_names: Vec<&Ident> = rpcs.iter().map(|rpc| &rpc.ident).collect();
|
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 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(future_types.iter())
|
||||||
.zip(outputs.iter())
|
.zip(outputs.iter())
|
||||||
.map(|((RpcMethod { attrs, ident, args, .. }, future_type), output)| {
|
.map(
|
||||||
let ty_doc = format!("The response future returned by {}.", ident);
|
|(
|
||||||
quote! {
|
(
|
||||||
#[doc = #ty_doc]
|
RpcMethod {
|
||||||
type #future_type: std::future::Future<Output = #output>;
|
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<Output = #output>;
|
||||||
|
|
||||||
#( #attrs )*
|
#( #attrs )*
|
||||||
fn #ident(self, context: tarpc::context::Context, #args) -> Self::#future_type;
|
fn #ident(self, context: tarpc::context::Context, #args) -> Self::#future_type;
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
let service_name_repeated = std::iter::repeat(ident.clone());
|
let service_name_repeated = std::iter::repeat(ident.clone());
|
||||||
let service_name_repeated2 = service_name_repeated.clone();
|
let service_name_repeated2 = service_name_repeated.clone();
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ fn syntax() {
|
|||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
async fn TestCamelCaseDoesntConflict();
|
async fn TestCamelCaseDoesntConflict();
|
||||||
async fn hello() -> String;
|
async fn hello() -> String;
|
||||||
#[doc="attr"]
|
#[doc = "attr"]
|
||||||
async fn attr(s: String) -> String;
|
async fn attr(s: String) -> String;
|
||||||
async fn no_args_no_return();
|
async fn no_args_no_return();
|
||||||
async fn no_args() -> ();
|
async fn no_args() -> ();
|
||||||
@@ -49,7 +49,7 @@ fn syntax() {
|
|||||||
async fn no_args_ret_error() -> i32;
|
async fn no_args_ret_error() -> i32;
|
||||||
async fn one_arg_ret_error(foo: String) -> String;
|
async fn one_arg_ret_error(foo: String) -> String;
|
||||||
async fn no_arg_implicit_return_error();
|
async fn no_arg_implicit_return_error();
|
||||||
#[doc="attr"]
|
#[doc = "attr"]
|
||||||
async fn one_arg_implicit_return_error(foo: String);
|
async fn one_arg_implicit_return_error(foo: String);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ pub mod subscriber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod publisher {
|
pub mod publisher {
|
||||||
pub use ServiceClient as Client;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
pub use ServiceClient as Client;
|
||||||
|
|
||||||
#[tarpc::service]
|
#[tarpc::service]
|
||||||
pub trait Service {
|
pub trait Service {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#![feature(async_await, external_doc)]
|
#![feature(async_await, external_doc)]
|
||||||
#![cfg_attr(test, feature(proc_macro_hygiene))]
|
#![cfg_attr(test, feature(proc_macro_hygiene))]
|
||||||
|
|
||||||
|
pub use rpc::*;
|
||||||
/// The main macro that creates RPC services.
|
/// The main macro that creates RPC services.
|
||||||
///
|
///
|
||||||
/// Rpc methods are specified, mirroring trait syntax:
|
/// Rpc methods are specified, mirroring trait syntax:
|
||||||
@@ -34,4 +35,3 @@
|
|||||||
/// * `Client` -- a client stub with a fn for each RPC.
|
/// * `Client` -- a client stub with a fn for each RPC.
|
||||||
/// * `fn new_stub` -- creates a new Client stub.
|
/// * `fn new_stub` -- creates a new Client stub.
|
||||||
pub use tarpc_plugins::service;
|
pub use tarpc_plugins::service;
|
||||||
pub use rpc::*;
|
|
||||||
|
|||||||
@@ -5,10 +5,9 @@ use futures::{
|
|||||||
future::{ready, Ready},
|
future::{ready, Ready},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use tarpc::{client, context, server::Handler, transport::channel};
|
|
||||||
#[cfg(feature = "serde1")]
|
#[cfg(feature = "serde1")]
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use tarpc::{client, context, server::Handler, transport::channel};
|
||||||
|
|
||||||
#[tarpc_plugins::service]
|
#[tarpc_plugins::service]
|
||||||
trait Service {
|
trait Service {
|
||||||
@@ -65,7 +64,7 @@ async fn serde() -> io::Result<()> {
|
|||||||
let _ = runtime::spawn(
|
let _ = runtime::spawn(
|
||||||
tarpc::Server::default()
|
tarpc::Server::default()
|
||||||
.incoming(transport.take(1).filter_map(|r| async { r.ok() }))
|
.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?;
|
let transport = bincode_transport::connect(&addr).await?;
|
||||||
|
|||||||
Reference in New Issue
Block a user