Fully commit to futures in hello world.

This commit is contained in:
Tim Kuehn
2016-12-29 12:08:31 +01:00
parent 77653282b6
commit 388c4be69a
2 changed files with 27 additions and 11 deletions

View File

@@ -87,7 +87,7 @@ races! See the tarpc_examples package for more examples.
## Example: Futures
Here's the same service, implemented using `FutureService`.
Here's the same service, implemented using futures.
```rust
#![feature(conservative_impl_trait, plugin)]
@@ -96,9 +96,12 @@ Here's the same service, implemented using `FutureService`.
extern crate futures;
#[macro_use]
extern crate tarpc;
extern crate tokio_core;
use futures::Future;
use tarpc::future::Connect;
use tarpc::util::{FirstSocketAddr, Never};
use tarpc::sync::Connect;
use tokio_core::reactor;
service! {
rpc hello(name: String) -> String;
@@ -116,10 +119,15 @@ impl FutureService for HelloServer {
}
fn main() {
let addr = "localhost:10000";
let _server = HelloServer.listen(addr.first_socket_addr());
let mut client = SyncClient::connect(addr).unwrap();
println!("{}", client.hello("Mom".to_string()).unwrap());
let addr = "localhost:10000".first_socket_addr();
let mut core = reactor::Core::new().unwrap();
HelloServer.listen_with(addr, core.handle()).unwrap();
core.run(
FutureClient::connect(&addr)
.map_err(tarpc::Error::from)
.and_then(|mut client| client.hello("Mom".to_string()))
.map(|resp| println!("{}", resp))
).unwrap();
}
```

View File

@@ -9,9 +9,12 @@
extern crate futures;
#[macro_use]
extern crate tarpc;
extern crate tokio_core;
use futures::Future;
use tarpc::future::Connect;
use tarpc::util::{FirstSocketAddr, Never};
use tarpc::sync::Connect;
use tokio_core::reactor;
service! {
rpc hello(name: String) -> String;
@@ -29,8 +32,13 @@ impl FutureService for HelloServer {
}
fn main() {
let addr = "localhost:10000";
let _server = HelloServer.listen(addr.first_socket_addr());
let mut client = SyncClient::connect(addr).unwrap();
println!("{}", client.hello("Mom".to_string()).unwrap());
let addr = "localhost:10000".first_socket_addr();
let mut core = reactor::Core::new().unwrap();
HelloServer.listen_with(addr, core.handle()).unwrap();
core.run(
FutureClient::connect(&addr)
.map_err(tarpc::Error::from)
.and_then(|mut client| client.hello("Mom".to_string()))
.map(|resp| println!("{}", resp))
).unwrap();
}