changeset 12:70ab6c2f7f6e

fu_test worked.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 18 Jan 2021 20:52:32 +0900
parents 34aba7ec9efc
children afac42f2b948
files src/async_test/Cargo.toml src/async_test/src/fu_test.rs src/async_test/src/lib.rs src/async_test/src/main.rs src/t16thread/src/main.rs
diffstat 5 files changed, 53 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/async_test/Cargo.toml	Mon Jan 18 15:53:03 2021 +0900
+++ b/src/async_test/Cargo.toml	Mon Jan 18 20:52:32 2021 +0900
@@ -7,4 +7,6 @@
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-futures = "0.3"
+futures = "0.3.12"
+threadpool = "1.0"
+rand = "0.5.5"
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/async_test/src/fu_test.rs	Mon Jan 18 20:52:32 2021 +0900
@@ -0,0 +1,44 @@
+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);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/async_test/src/lib.rs	Mon Jan 18 20:52:32 2021 +0900
@@ -0,0 +1,2 @@
+
+pub mod fu_test;
\ No newline at end of file
--- a/src/async_test/src/main.rs	Mon Jan 18 15:53:03 2021 +0900
+++ b/src/async_test/src/main.rs	Mon Jan 18 20:52:32 2021 +0900
@@ -38,4 +38,5 @@
     let m = async_main();
     println!("waiting");
     block_on(m);
+    async_test::fu_test::fu_test();
 }
\ No newline at end of file
--- a/src/t16thread/src/main.rs	Mon Jan 18 15:53:03 2021 +0900
+++ b/src/t16thread/src/main.rs	Mon Jan 18 20:52:32 2021 +0900
@@ -29,8 +29,8 @@
     }
     handle.join().unwrap();  // without this, some data are dropped
     main1();
-    t16thread::mpsc_test::mainm();
-    t16thread::mpsc_test::mainc();
-    t16thread::racecondition::mainr();
+    mainm();
+    mainc();
+    mainr();
     t16thread::racecondition::mainu();
 }
\ No newline at end of file