view src/async_test/src/fu_test.rs @ 12:70ab6c2f7f6e

fu_test worked.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 18 Jan 2021 20:52:32 +0900
parents
children afac42f2b948
line wrap: on
line source

use threadpool::ThreadPool;
use futures::executor::block_on;
use rand::Rng;


use std::{
    future::Future,
    // pin::Pin,
    // sync::{Arc, Mutex},
    // task::{Context, Poll},
    // thread,
    // time::Duration,
};
use futures::TryStreamExt;

struct MyFuture ;

impl MyFuture {
    pub async fn call (&mut self) -> std::result::Result<std::string::String, std::string::String> {
        println!("poll called");
        let mut rng = rand::thread_rng();
        let i: i32 = rng.gen_range(0, 10);
        if i < 5 {
          Ok("success".to_string())
        } else {
          Err("failure".to_string())
        }
    }
}

async fn get_future() ->  Result<(), ()> {
    (MyFuture{}).call().await.map(|res| {
        println!("{}", res);
    }).map_err(|err| {
        println!("{}", err);
    })
}

pub fn fu_test() {

    let future = get_future();
    // ThreadPool::new(4).execute(future);
    block_on(future);
}