January 2018
Beginner to intermediate
454 pages
10h 8m
English
Let's now see the handle_client() function we've just mentioned:
use std::result; use futures::prelude::await; #[async] use tokio_core::net::TcpStream; fn handle_client(stream: TcpStream) -> result::Result<(), ()> { await!(client(stream)) .map_err(|error| println!("Error handling client: {}", error)) }
It is a simple wrapper over the client future. Here, we used a new macro, await!, which allows us to write asynchronous code in an asynchronous way. When the result of the future inside await!() is not ready, the event loop will execute other stuff, and when it's ready it will continue executing the code after the await!(). In this case, we print the error returned by the client future. This is why we needed a wrapper.
Now, ...