Update examples to track latest Tokio changes (#180)

The exampes included in the repository have lagged behind the changes
made. Specifically, they do not use the new runtime construct.

This patch updates examples to use the latest features of Tokio.
This commit is contained in:
Carl Lerche
2018-03-06 09:59:04 -08:00
committed by GitHub
parent 56c5797872
commit f1cb12e14f
15 changed files with 412 additions and 741 deletions
+13 -6
View File
@@ -10,15 +10,16 @@
//!
//! Each line you type in to the `nc` terminal should be echo'd back to you!
#![deny(warnings)]
#[macro_use]
extern crate futures;
extern crate tokio;
extern crate tokio_io;
use std::{env, io};
use std::net::SocketAddr;
use futures::{Future, Poll};
use tokio::prelude::*;
use tokio::net::UdpSocket;
struct Server {
@@ -56,11 +57,17 @@ fn main() {
let socket = UdpSocket::bind(&addr).unwrap();
println!("Listening on: {}", socket.local_addr().unwrap());
// Next we'll create a future to spawn (the one we defined above) and then
// we'll block our current thread waiting on the result of the future
Server {
let server = Server {
socket: socket,
buf: vec![0; 1024],
to_send: None,
}.wait().unwrap();
};
// This starts the server task.
//
// `map_err` handles the error by logging it and maps the future to a type
// that can be spawned.
//
// `tokio::run` spanws the task on the Tokio runtime and starts running.
tokio::run(server.map_err(|e| println!("server error = {:?}", e)));
}