Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate futures;
- extern crate tokio;
- use futures::future::ok;
- use futures::{Future, Stream};
- use tokio::prelude::future::lazy;
- use tokio::runtime::current_thread::Runtime;
- fn get_times2_future(number: u32) -> impl Future<Item = u32, Error = ()> {
- ok(number * 2)
- }
- fn get_times_future(number: u32, times: u32) -> impl Future<Item = u32, Error = ()> {
- ok(number * times)
- }
- fn main() {
- let numbers = vec![1, 2, 3];
- let mut runtime = Runtime::new().unwrap();
- let calculations = runtime.block_on(lazy(|| {
- let calc_futures = numbers.iter().map(|number| get_times2_future(*number));
- let first_level = futures::stream::futures_unordered(calc_futures);
- let second_level = first_level.and_then(|num| {
- Ok(futures::stream::futures_unordered(vec![
- get_times_future(num, 10),
- get_times_future(num, 100),
- get_times_future(num, 1000),
- ]))
- });
- second_level.flatten().for_each(|resp| {
- println!("{}", resp);
- Ok(())
- })
- }));
- if let Err(_) = calculations {
- println!("error")
- }
- }
Add Comment
Please, Sign In to add comment