Files
tarpc/src/util.rs
Tim 7aabfb3c14 Rewrite using tokio (#44)
* Rewrite tarpc on top of tokio.

* Add examples

* Move error types to their own module.

Also, cull unused error variants.

* Remove unused fn

* Remove CanonicalRpcError* types. They're 100% useless.

* Track tokio master (WIP)

* The great error revamp.

Removed the canonical rpc error type. Instead, the user declares
the error type for each rpc:

In the above example, the error type is Baz. Declaring an error is
optional; if none is specified, it defaults to Never, a convenience
struct that wraps the never type (exclamation mark) to impl Serialize, Deserialize,
Error, etc. Also adds the convenience type StringError for easily
using a String as an error type.

* Add missing license header

* Minor cleanup

* Rename StringError => Message

* Create a sync::Connect trait.

Along with this, the existing Connect trait moves to future::Connect. The future
and sync modules are reexported from the crate root.

Additionally, the utility errors Never and Message are no longer reexported from
the crate root.

* Update readme

* Track tokio/futures master. Add a Spawn utility trait to replace the removed forget.

* Fix pre-push hook

* Add doc comment to SyncServiceExt.

* Fix up some documentation

* Track tokio-proto master

* Don't set tcp nodelay

* Make future::Connect take an associated type for the future.

* Unbox FutureClient::connect return type

* Use type alias instead of newtype struct for ClientFuture

* Fix benches/latency.rs

* Write a plugin to convert lower_snake_case idents/types to UpperCamelCase.

Use it to add associated types to FutureService instead of boxing the return futures.

* Specify plugin = true in snake_to_camel/Cargo.toml. Weird things happen otherwise.

* Add clippy.toml
2016-09-04 16:09:50 -07:00

84 lines
2.1 KiB
Rust

// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.
use futures;
use std::fmt;
use std::error::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// A trait for easy spawning of futures.
///
/// `Future`s don't actually perform their computations until spawned via an `Executor`. Typically,
/// an executor will be associated with an event loop. The `fn` provided by `Spawn` handles
/// spawning the future on the static event loop on which tarpc clients and servers run by default.
pub trait Spawn {
/// Spawns a future on the default event loop.
fn spawn(self);
}
impl<F> Spawn for F
where F: futures::Future + Send + 'static
{
fn spawn(self) {
::protocol::LOOP_HANDLE.spawn(move |_| self.then(|_| Ok::<(), ()>(())))
}
}
/// A bottom type that impls `Error`, `Serialize`, and `Deserialize`. It is impossible to
/// instantiate this type.
#[derive(Debug)]
pub struct Never(!);
impl Error for Never {
fn description(&self) -> &str {
unreachable!()
}
}
impl fmt::Display for Never {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
unreachable!()
}
}
impl Serialize for Never {
fn serialize<S>(&self, _: &mut S) -> Result<(), S::Error>
where S: Serializer
{
unreachable!()
}
}
// Please don't try to deserialize this. :(
impl Deserialize for Never {
fn deserialize<D>(_: &mut D) -> Result<Self, D::Error>
where D: Deserializer
{
panic!("Never cannot be instantiated!");
}
}
/// A `String` that impls `std::error::Error`. Useful for quick-and-dirty error propagation.
#[derive(Debug, Serialize, Deserialize)]
pub struct Message(pub String);
impl Error for Message {
fn description(&self) -> &str {
&self.0
}
}
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<S: Into<String>> From<S> for Message {
fn from(s: S) -> Self {
Message(s.into())
}
}