summaryrefslogtreecommitdiffstats
path: root/src/bin/echo.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2016-08-31 00:19:29 -0700
committerAlex Crichton <alex@alexcrichton.com>2016-08-31 19:00:42 -0700
commit330ab823b0dfe45c0f4219e1601ecbbdd5ae457b (patch)
tree48e67b5fcf4213a141ef68b15195afa5837827e0 /src/bin/echo.rs
parent440a813c5ac23b07528dd710a9301f5e5c64126d (diff)
Update to futures master
* Remove `LoopData` as it's no longer necessary * Add `LoopHandle::spawn` to spawn new futures onto an event loop * Add `LoopData::spawn` to also spawn new futures onto an event loop * Rejigger the implementation of the event loop a bit (make a slab of futures), but otherwise everything else is pretty constant.
Diffstat (limited to 'src/bin/echo.rs')
-rw-r--r--src/bin/echo.rs15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/bin/echo.rs b/src/bin/echo.rs
index 710db79d..ad32a626 100644
--- a/src/bin/echo.rs
+++ b/src/bin/echo.rs
@@ -8,6 +8,7 @@ use std::net::SocketAddr;
use futures::Future;
use futures::stream::Stream;
+use tokio_core::Loop;
use tokio_core::io::{copy, TaskIo};
fn main() {
@@ -15,7 +16,8 @@ fn main() {
let addr = addr.parse::<SocketAddr>().unwrap();
// Create the event loop that will drive this server
- let mut l = tokio_core::Loop::new().unwrap();
+ let mut l = Loop::new().unwrap();
+ let pin = l.pin();
// Create a TCP listener which will listen for incoming connections
let server = l.handle().tcp_listen(&addr);
@@ -29,17 +31,20 @@ fn main() {
//
// We use the `io::copy` future to copy all data from the
// reading half onto the writing half.
- socket.incoming().for_each(|(socket, addr)| {
+ socket.incoming().for_each(move |(socket, addr)| {
let socket = futures::lazy(|| futures::finished(TaskIo::new(socket)));
let pair = socket.map(|s| s.split());
let amt = pair.and_then(|(reader, writer)| copy(reader, writer));
// Once all that is done we print out how much we wrote, and then
- // critically we *forget* this future which allows it to run
+ // critically we *spawn* this future which allows it to run
// concurrently with other connections.
- amt.map(move |amt| {
+ let msg = amt.map(move |amt| {
println!("wrote {} bytes to {}", amt, addr)
- }).forget();
+ }).map_err(|e| {
+ panic!("error: {}", e);
+ });
+ pin.spawn(msg);
Ok(())
})