From f38a17252394bb97b0ecbc88b717ba50be7ec609 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Mon, 19 Aug 2019 23:12:18 +0300 Subject: [PATCH] Format code with rustfmt --- plugins/src/lib.rs | 54 +++++++++++---------- rpc/src/client/channel.rs | 27 +++++------ rpc/src/lib.rs | 4 +- rpc/src/server/mod.rs | 17 ++----- tarpc/examples/pubsub.rs | 8 ++-- tarpc/examples/readme.rs | 2 +- tarpc/examples/server_calling_server.rs | 2 +- tarpc/src/lib.rs | 64 ++++++++++++------------- tarpc/tests/service_functional.rs | 13 +++-- 9 files changed, 92 insertions(+), 99 deletions(-) diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index b73b2c9..ad9e4d3 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -21,7 +21,8 @@ use syn::{ punctuated::Punctuated, spanned::Spanned, token::Comma, - ArgCaptured, Attribute, FnArg, Ident, Lit, LitBool, MetaNameValue, Pat, ReturnType, Token, Visibility, + ArgCaptured, Attribute, FnArg, Ident, Lit, LitBool, MetaNameValue, Pat, ReturnType, Token, + Visibility, }; struct Service { @@ -54,14 +55,17 @@ impl Parse for Service { if rpc.ident == "new" { return Err(syn::Error::new( rpc.ident.span(), - format!("method name conflicts with generated fn `{}Client::new`", ident) - )) + format!( + "method name conflicts with generated fn `{}Client::new`", + ident + ), + )); } if rpc.ident == "serve" { return Err(syn::Error::new( rpc.ident.span(), - format!("method name conflicts with generated fn `{}::serve`", ident) - )) + format!("method name conflicts with generated fn `{}::serve`", ident), + )); } } Ok(Service { @@ -133,29 +137,29 @@ struct DeriveSerde(bool); impl Parse for DeriveSerde { fn parse(input: ParseStream) -> syn::Result { if input.is_empty() { - return Ok(DeriveSerde(cfg!(feature = "serde1"))) + return Ok(DeriveSerde(cfg!(feature = "serde1"))); } match input.parse::()? { - MetaNameValue { ref ident, ref lit, .. } if ident == "derive_serde" => { - match lit { - Lit::Bool(LitBool{value: true, ..}) if cfg!(feature = "serde1") => Ok(DeriveSerde(true)), - Lit::Bool(LitBool{value: true, ..}) => Err(syn::Error::new( - lit.span(), - "To enable serde, first enable the `serde1` feature of tarpc", - )), - Lit::Bool(LitBool{value: false, ..}) => Ok(DeriveSerde(false)), - lit => Err(syn::Error::new( - lit.span(), - "`derive_serde` expects a value of type `bool`", - )), + MetaNameValue { + ref ident, ref lit, .. + } if ident == "derive_serde" => match lit { + Lit::Bool(LitBool { value: true, .. }) if cfg!(feature = "serde1") => { + Ok(DeriveSerde(true)) } - } - MetaNameValue { ident, .. } => { - Err(syn::Error::new( - ident.span(), - "tarpc::service only supports one meta item, `derive_serde = {bool}`", - )) - } + Lit::Bool(LitBool { value: true, .. }) => Err(syn::Error::new( + lit.span(), + "To enable serde, first enable the `serde1` feature of tarpc", + )), + Lit::Bool(LitBool { value: false, .. }) => Ok(DeriveSerde(false)), + lit => Err(syn::Error::new( + lit.span(), + "`derive_serde` expects a value of type `bool`", + )), + }, + MetaNameValue { ident, .. } => Err(syn::Error::new( + ident.span(), + "tarpc::service only supports one meta item, `derive_serde = {bool}`", + )), } } } diff --git a/rpc/src/client/channel.rs b/rpc/src/client/channel.rs index f974b5d..a343775 100644 --- a/rpc/src/client/channel.rs +++ b/rpc/src/client/channel.rs @@ -29,8 +29,8 @@ use std::{ Arc, }, }; -use trace::SpanId; use tokio_timer::{timeout, Timeout}; +use trace::SpanId; use super::{Config, NewClient}; @@ -187,12 +187,10 @@ impl Future for DispatchResponse { } } } - Err(timeout::Elapsed{..}) => Err( - io::Error::new( - io::ErrorKind::TimedOut, - "Client dropped expired request.".to_string(), - ) - ), + Err(timeout::Elapsed { .. }) => Err(io::Error::new( + io::ErrorKind::TimedOut, + "Client dropped expired request.".to_string(), + )), }) } } @@ -735,8 +733,8 @@ mod tests { use futures_test::task::noop_waker_ref; use std::time::Duration; use std::{pin::Pin, sync::atomic::AtomicU64, sync::Arc}; - use tokio_timer::Timeout; use tokio::runtime::current_thread; + use tokio_timer::Timeout; #[test] fn dispatch_response_cancels_on_timeout() { @@ -753,14 +751,11 @@ mod tests { { pin_utils::pin_mut!(resp); let timer = tokio_timer::Timer::default(); - tokio_timer::with_default( - &timer.handle(), - || { - let _ = resp - .as_mut() - .poll(&mut Context::from_waker(&noop_waker_ref())); - }, - ); + tokio_timer::with_default(&timer.handle(), || { + let _ = resp + .as_mut() + .poll(&mut Context::from_waker(&noop_waker_ref())); + }); // End of block should cause resp.drop() to run, which should send a cancel message. } assert!(canceled_requests.0.try_next().unwrap() == Some(3)); diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 4eac3a5..a637217 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -33,9 +33,7 @@ pub(crate) mod util; pub use crate::{client::Client, server::Server, transport::Transport}; -use futures::{ - task::Poll, -}; +use futures::task::Poll; use std::{io, time::SystemTime}; /// A message from a client to a server. diff --git a/rpc/src/server/mod.rs b/rpc/src/server/mod.rs index 7b24de8..d419b25 100644 --- a/rpc/src/server/mod.rs +++ b/rpc/src/server/mod.rs @@ -7,8 +7,8 @@ //! Provides a server that concurrently handles many connections sending multiplexed requests. use crate::{ - context, util::Compact, util::TimeUntil, ClientMessage, PollIo, Request, - Response, ServerError, Transport, + context, util::Compact, util::TimeUntil, ClientMessage, PollIo, Request, Response, ServerError, + Transport, }; use fnv::FnvHashMap; use futures::{ @@ -22,15 +22,8 @@ use futures::{ use humantime::format_rfc3339; use log::{debug, trace}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; -use std::{ - fmt, - hash::Hash, - io, - marker::PhantomData, - pin::Pin, - time::SystemTime, -}; -use tokio_timer::{Timeout, timeout}; +use std::{fmt, hash::Hash, io, marker::PhantomData, pin::Pin, time::SystemTime}; +use tokio_timer::{timeout, Timeout}; mod filter; #[cfg(test)] @@ -575,7 +568,7 @@ where request_id: self.request_id, message: match result { Ok(message) => Ok(message), - Err(timeout::Elapsed{..}) => { + Err(timeout::Elapsed { .. }) => { debug!( "[{}] Response did not complete before deadline of {}s.", self.ctx.trace_id(), diff --git a/tarpc/examples/pubsub.rs b/tarpc/examples/pubsub.rs index 22a636b..ab9d72d 100644 --- a/tarpc/examples/pubsub.rs +++ b/tarpc/examples/pubsub.rs @@ -12,10 +12,6 @@ use futures::{ Future, }; use publisher::Publisher as _; -use tarpc::{ - client, context, - server::{self, Handler}, -}; use std::{ collections::HashMap, io, @@ -25,6 +21,10 @@ use std::{ time::Duration, }; use subscriber::Subscriber as _; +use tarpc::{ + client, context, + server::{self, Handler}, +}; pub mod subscriber { #[tarpc::service] diff --git a/tarpc/examples/readme.rs b/tarpc/examples/readme.rs index a2f2229..e321822 100644 --- a/tarpc/examples/readme.rs +++ b/tarpc/examples/readme.rs @@ -10,11 +10,11 @@ use futures::{ future::{self, Ready}, prelude::*, }; +use std::io; use tarpc::{ client, context, server::{BaseChannel, Channel}, }; -use std::io; /// This is the service definition. It looks a lot like a trait definition. /// It defines one RPC, hello, which takes one arg, name, and returns a String. diff --git a/tarpc/examples/server_calling_server.rs b/tarpc/examples/server_calling_server.rs index 0dbec90..df6cd5a 100644 --- a/tarpc/examples/server_calling_server.rs +++ b/tarpc/examples/server_calling_server.rs @@ -11,11 +11,11 @@ use futures::{ future::{self, Ready}, prelude::*, }; +use std::io; use tarpc::{ client, context, server::{Handler, Server}, }; -use std::io; pub mod add { #[tarpc::service] diff --git a/tarpc/src/lib.rs b/tarpc/src/lib.rs index 5fdb50b..92542a0 100644 --- a/tarpc/src/lib.rs +++ b/tarpc/src/lib.rs @@ -6,26 +6,26 @@ //! [![Latest Version](https://img.shields.io/crates/v/tarpc.svg)](https://crates.io/crates/tarpc) //! [![Join the chat at https://gitter.im/tarpc/Lobby](https://badges.gitter.im/tarpc/Lobby.svg)](https://gitter.im/tarpc/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -//! +//! //! *Disclaimer*: This is not an official Google product. -//! +//! //! tarpc is an RPC framework for rust with a focus on ease of use. Defining a //! service can be done in just a few lines of code, and most of the boilerplate of //! writing a server is taken care of for you. -//! +//! //! [Documentation](https://docs.rs/crate/tarpc/) -//! +//! //! ## What is an RPC framework? //! "RPC" stands for "Remote Procedure Call," a function call where the work of //! producing the return value is being done somewhere else. When an rpc function is //! invoked, behind the scenes the function contacts some other process somewhere //! and asks them to evaluate the function instead. The original function then //! returns the value produced by the other process. -//! +//! //! RPC frameworks are a fundamental building block of most microservices-oriented //! architectures. Two well-known ones are [gRPC](http://www.grpc.io) and //! [Cap'n Proto](https://capnproto.org/). -//! +//! //! tarpc differentiates itself from other RPC frameworks by defining the schema in code, //! rather than in a separate language such as .proto. This means there's no separate compilation //! process, and no context switching between different languages. @@ -45,38 +45,38 @@ //! - Serde serialization: enabling the `serde1` Cargo feature will make service requests and //! responses `Serialize + Deserialize`. It's entirely optional, though: in-memory transports can //! be used, as well, so the price of eerialization doesn't have to be paid when it's not needed. -//! +//! //! ## Usage //! Add to your `Cargo.toml` dependencies: -//! +//! //! ```toml //! tarpc = "0.18.0" //! ``` -//! +//! //! The `tarpc::service` attribute expands to a collection of items that form an rpc service. //! These generated types make it easy and ergonomic to write servers with less boilerplate. //! Simply implement the generated service trait, and you're off to the races! -//! +//! //! ## Example -//! +//! //! For this example, in addition to tarpc, also add two other dependencies to //! your `Cargo.toml`: -//! +//! //! ```toml //! futures-preview = "0.3.0-alpha.17" //! tokio = "0.2.0-alpha.1" //! ``` -//! +//! //! In the following example, we use an in-process channel for communication between //! client and server. In real code, you will likely communicate over the network. //! For a more real-world example, see [example-service](example-service). -//! +//! //! First, let's set up the dependencies and service definition. -//! +//! //! ```rust //! #![feature(async_await)] //! # extern crate futures; -//! +//! //! use futures::{ //! future::{self, Ready}, //! prelude::*, @@ -86,7 +86,7 @@ //! server::{self, Handler}, //! }; //! use std::io; -//! +//! //! // This is the service definition. It looks a lot like a trait definition. //! // It defines one RPC, hello, which takes one arg, name, and returns a String. //! #[tarpc::service] @@ -95,10 +95,10 @@ //! async fn hello(name: String) -> String; //! } //! ``` -//! +//! //! This service definition generates a trait called `World`. Next we need to //! implement it for our Server struct. -//! +//! //! ```rust //! # #![feature(async_await)] //! # extern crate futures; @@ -122,26 +122,26 @@ //! // and is used to start the server. //! #[derive(Clone)] //! struct HelloServer; -//! +//! //! impl World for HelloServer { //! // Each defined rpc generates two items in the trait, a fn that serves the RPC, and //! // an associated type representing the future output by the fn. -//! +//! //! type HelloFut = Ready; -//! +//! //! fn hello(self, _: context::Context, name: String) -> Self::HelloFut { //! future::ready(format!("Hello, {}!", name)) //! } //! } //! ``` -//! +//! //! Lastly let's write our `main` that will start the server. While this example uses an //! [in-process //! channel](https://docs.rs/tarpc/0.18.0/tarpc/transport/channel/struct.UnboundedChannel.html), //! tarpc also ships a //! [transport](https://docs.rs/tarpc-bincode-transport/0.7.0/tarpc_bincode_transport/) //! that uses bincode over TCP. -//! +//! //! ```rust //! # #![feature(async_await)] //! # extern crate futures; @@ -176,32 +176,32 @@ //! #[tokio::main] //! async fn main() -> io::Result<()> { //! let (client_transport, server_transport) = tarpc::transport::channel::unbounded(); -//! +//! //! let server = server::new(server::Config::default()) //! // incoming() takes a stream of transports such as would be returned by //! // TcpListener::incoming (but a stream instead of an iterator). //! .incoming(stream::once(future::ready(server_transport))) //! .respond_with(HelloServer.serve()); -//! +//! //! tokio::spawn(server); -//! +//! //! // WorldClient is generated by the macro. It has a constructor `new` that takes a config and //! // any Transport as input //! let mut client = WorldClient::new(client::Config::default(), client_transport).spawn()?; -//! +//! //! // The client has an RPC method for each RPC defined in the annotated trait. It takes the same //! // args as defined, with the addition of a Context, which is always the first arg. The Context //! // specifies a deadline and trace information which can be helpful in debugging requests. //! let hello = client.hello(context::current(), "Stim".to_string()).await?; -//! +//! //! println!("{}", hello); -//! +//! //! Ok(()) //! } //! ``` -//! +//! //! ## Service Documentation -//! +//! //! Use `cargo doc` as you normally would to see the documentation created for all //! items expanded by a `service!` invocation. diff --git a/tarpc/tests/service_functional.rs b/tarpc/tests/service_functional.rs index f8278d6..de562ce 100644 --- a/tarpc/tests/service_functional.rs +++ b/tarpc/tests/service_functional.rs @@ -1,14 +1,14 @@ #![feature(async_await)] - use assert_matches::assert_matches; use futures::{ future::{ready, Ready}, prelude::*, }; -use std::{rc::Rc, io}; +use std::{io, rc::Rc}; use tarpc::{ - client::{self, NewClient}, context, + client::{self, NewClient}, + context, server::{self, BaseChannel, Channel, Handler}, transport::channel, }; @@ -45,7 +45,7 @@ async fn sequential() -> io::Result<()> { tokio::spawn( BaseChannel::new(server::Config::default(), rx) .respond_with(Server.serve()) - .execute() + .execute(), ); let mut client = ServiceClient::new(client::Config::default(), tx).spawn()?; @@ -148,7 +148,10 @@ fn in_memory_single_threaded() -> io::Result<()> { } }); - let NewClient{mut client, dispatch} = InMemoryClient::new(client::Config::default(), tx); + let NewClient { + mut client, + dispatch, + } = InMemoryClient::new(client::Config::default(), tx); runtime.spawn(async move { if let Err(e) = dispatch.await { warn!("Error while running client dispatch: {}", e)