mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-28 07:12:05 +01:00
* Return a concrete type from `server::listen`. * Change `FutureServiceExt::listen` to return `(SocketAddr, Listen)`, where `Listen` is a struct created by the `service!` macro that `impls Future<Item=(), Error=()>` and represents server execution. * Disable `conservative_impl_trait` as it's no longer used. * Update `FutureServiceExt` doc comment. * Update `SyncServiceExt` doc comment. Also annotate `server::Handle` with `#[must_use]`. * `cargo fmt`
67 lines
1.7 KiB
Rust
67 lines
1.7 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.
|
|
|
|
#![feature(plugin)]
|
|
#![plugin(tarpc_plugins)]
|
|
|
|
extern crate futures;
|
|
#[macro_use]
|
|
extern crate tarpc;
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate tokio_core;
|
|
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use std::sync::mpsc;
|
|
use std::thread;
|
|
use tarpc::{client, server};
|
|
use tarpc::client::sync::ClientExt;
|
|
|
|
service! {
|
|
rpc hello(name: String) -> String | NoNameGiven;
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
pub struct NoNameGiven;
|
|
|
|
impl fmt::Display for NoNameGiven {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{}", self.description())
|
|
}
|
|
}
|
|
|
|
impl Error for NoNameGiven {
|
|
fn description(&self) -> &str {
|
|
r#"The empty String, "", is not a valid argument to rpc `hello`."#
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct HelloServer;
|
|
|
|
impl SyncService for HelloServer {
|
|
fn hello(&self, name: String) -> Result<String, NoNameGiven> {
|
|
if name == "" {
|
|
Err(NoNameGiven)
|
|
} else {
|
|
Ok(format!("Hello, {}!", name))
|
|
}
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let (tx, rx) = mpsc::channel();
|
|
thread::spawn(move || {
|
|
let mut handle = HelloServer.listen("localhost:10000", server::Options::default())
|
|
.unwrap();
|
|
tx.send(handle.addr()).unwrap();
|
|
handle.run();
|
|
});
|
|
let mut client = SyncClient::connect(rx.recv().unwrap(), client::Options::default()).unwrap();
|
|
println!("{}", client.hello("Mom".to_string()).unwrap());
|
|
println!("{}", client.hello("".to_string()).unwrap_err());
|
|
}
|