From 5b59369043aa5f1eeff05a67697c8c39e26563e5 Mon Sep 17 00:00:00 2001 From: hellerve Date: Fri, 25 Jan 2019 18:09:59 +0100 Subject: [PATCH] async-tcp & future-hello: use better futures functions --- async-tcp/src/main.rs | 11 +++++++++-- future-hello-world/src/main.rs | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/async-tcp/src/main.rs b/async-tcp/src/main.rs index 2eac0a7..653ddbc 100644 --- a/async-tcp/src/main.rs +++ b/async-tcp/src/main.rs @@ -3,11 +3,11 @@ extern crate bytes; extern crate futures; extern crate tokio; -use tokio::io::AsyncWrite; +use tokio::io::{self, AsyncWrite}; use tokio::net::{TcpStream, tcp::ConnectFuture}; use bytes::{Bytes, Buf}; use futures::{Future, Async, Poll}; -use std::io::{self, Cursor}; +use std::io::{Cursor}; enum HelloWorld { Connecting(ConnectFuture), @@ -47,4 +47,11 @@ fn main() { let hello_world = HelloWorld::Connecting(connect_future); tokio::run(hello_world.map_err(|e| println!("{0}", e))); + + let better_future = TcpStream::connect(&addr) + .and_then(|socket| io::write_all(socket, b"hello world")) + .map(|_| println!("write complete")) + .map_err(|_| println!("failed")); + + tokio::run(better_future); } diff --git a/future-hello-world/src/main.rs b/future-hello-world/src/main.rs index 6a46073..bbbdd0e 100644 --- a/future-hello-world/src/main.rs +++ b/future-hello-world/src/main.rs @@ -37,4 +37,6 @@ where fn main() { let future = Display(HelloWorld); tokio::run(future); + let better_future = HelloWorld.map(|value| println!("{}", value)); + tokio::run(better_future); }