async-tcp & future-hello: use better futures functions

This commit is contained in:
2019-01-25 18:09:59 +01:00
parent b439b235f8
commit 5b59369043
2 changed files with 11 additions and 2 deletions

View File

@@ -3,11 +3,11 @@ extern crate bytes;
extern crate futures; extern crate futures;
extern crate tokio; extern crate tokio;
use tokio::io::AsyncWrite; use tokio::io::{self, AsyncWrite};
use tokio::net::{TcpStream, tcp::ConnectFuture}; use tokio::net::{TcpStream, tcp::ConnectFuture};
use bytes::{Bytes, Buf}; use bytes::{Bytes, Buf};
use futures::{Future, Async, Poll}; use futures::{Future, Async, Poll};
use std::io::{self, Cursor}; use std::io::{Cursor};
enum HelloWorld { enum HelloWorld {
Connecting(ConnectFuture), Connecting(ConnectFuture),
@@ -47,4 +47,11 @@ fn main() {
let hello_world = HelloWorld::Connecting(connect_future); let hello_world = HelloWorld::Connecting(connect_future);
tokio::run(hello_world.map_err(|e| println!("{0}", e))); 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);
} }

View File

@@ -37,4 +37,6 @@ where
fn main() { fn main() {
let future = Display(HelloWorld); let future = Display(HelloWorld);
tokio::run(future); tokio::run(future);
let better_future = HelloWorld.map(|value| println!("{}", value));
tokio::run(better_future);
} }