How to choose between future and flow in Rust?

I just started experimenting with futures / tokyo in Rust. I can do really simple things with futures or just streams. I was wondering how you can choose between future and flow.

How can I extend the toy from the tokio documentation to use tokio_timer::Timer

to fulfill the requested HTTPS request?

extern crate futures;
extern crate native_tls;
extern crate tokio_core;
extern crate tokio_io;
extern crate tokio_tls;

use std::io;
use std::net::ToSocketAddrs;

use futures::Future;
use native_tls::TlsConnector;
use tokio_core::net::TcpStream;
use tokio_core::reactor::Core;
use tokio_tls::TlsConnectorExt;

fn main() {
    let mut core = Core::new().unwrap();
    let handle = core.handle();
    let addr = "www.rust-lang.org:443".to_socket_addrs().unwrap().next().unwrap();

    let cx = TlsConnector::builder().unwrap().build().unwrap();
    let socket = TcpStream::connect(&addr, &handle);

    let tls_handshake = socket.and_then(|socket| {
        let tls = cx.connect_async("www.rust-lang.org", socket);
        tls.map_err(|e| {
            io::Error::new(io::ErrorKind::Other, e)
        })
    });
    let request = tls_handshake.and_then(|socket| {
        tokio_io::io::write_all(socket, "\
            GET / HTTP/1.0\r\n\
            Host: www.rust-lang.org\r\n\
            \r\n\
        ".as_bytes())
    });
    let response = request.and_then(|(socket, _request)| {
        tokio_io::io::read_to_end(socket, Vec::new())
    });

    let (_socket, data) = core.run(response).unwrap();
    println!("{}", String::from_utf8_lossy(&data));
}

      

+3


source to share


2 answers


You can convert Future

to Stream

and then select two streams:



extern crate futures; // 0.2.1

use futures::{Future, FutureExt, Stream, StreamExt};

fn select_stream_or_future_as_stream<S, F>(
    stream: S,
    future: F,
) -> impl Stream<Item = S::Item, Error = S::Error>
where
    S: Stream + 'static,
    F: Future<Item = S::Item, Error = S::Error> + 'static,
{
    future.into_stream().select(stream)
}

      

+3


source


The way I adapted the example code below may be useful for beginners.



let timer = tokio_timer::Timer::default();
    // Error out when timeout is reached
    let timeout = timer.sleep(time::Duration::from_millis(950)).then(|_| {
        future::err(io::Error::new(io::ErrorKind::Other, "Timeout"))
    });

    let handle = core.handle();

    // this returns IoFuture = BoxFuture<T, io::Error>;
    let addresses = tokio_dns::CpuPoolResolver::new(1 as usize).resolve("www.google.cz");
    let socket = addresses.and_then(|all_addresses| {
        let mut ipv4_addresses =  all_addresses.iter().filter(|x| is_ipv4(**x));
        let addr = ipv4_addresses.next().unwrap();
        let sock = TcpStream::connect(&SocketAddr::new(*addr, 443), &handle);
        sock.map_err(|e| {
            println!("{:?}", e);
            io::Error::new(io::ErrorKind::Other, e)
        })
    });

    let tls_handshake = socket.and_then(|socket| {
        println!("Got socket");
        let cx = TlsConnector::builder().unwrap().build().unwrap();
        let tls = cx.connect_async("www.google.cz", socket);
        tls.map_err(|e| {
            println!("{:?}", e);
            io::Error::new(io::ErrorKind::Other, e)
        })
    });

    let request = tls_handshake.and_then(|socket| {
        println!("SSL Handshake Successful");
        let write_all = tokio_io::io::write_all(socket, "\
            GET / HTTP/1.0\r\n\
            Host: www.google.cz\r\n\
            \r\n\
        ".as_bytes());
        println!("Wrote to socket");
        write_all.map_err(|e| {
            println!("{:?}", e);
            io::Error::new(io::ErrorKind::Other, e)
        })
    });

    let response = request.and_then(|(socket, _request)| {
        let read_till_end = tokio_io::io::read_to_end(socket, Vec::new());
        println!("Read till end of socket");
        read_till_end
    });

    let waiter = response.select(timeout).map(|(win, _)| {
        let (_socket, data) = win;
        data
    });

    let result = core.run(waiter);

      

+1


source







All Articles