|
| 1 | +#![feature(type_alias_impl_trait)] |
| 2 | +// #![feature(return_position_impl_trait_in_trait)] |
| 3 | + |
1 | 4 | pub mod asyncio; |
2 | 5 | pub mod future; |
| 6 | +pub mod runtimes; |
| 7 | +pub mod gat; |
| 8 | +pub mod async_trait_example; |
3 | 9 |
|
4 | 10 | pub use asyncio::*; |
5 | 11 | pub use future::*; |
| 12 | +pub use runtimes::*; |
| 13 | +pub use gat::*; |
| 14 | +pub use async_trait_example::*; |
6 | 15 |
|
7 | | -use futures::channel::mpsc; |
8 | | -use futures::executor::{self, ThreadPool}; |
9 | | -use futures::try_join; |
10 | | -use futures::StreamExt; |
11 | | -use futures::{ |
12 | | - future::FutureExt, // for `.fuse()` |
13 | | - pin_mut, |
14 | | - select, |
15 | | -}; |
16 | | - |
17 | | -pub fn tokio_async() { |
18 | | - let rt = tokio::runtime::Runtime::new().unwrap(); |
19 | | - rt.block_on(async { |
20 | | - println!("Hello from tokio!"); |
21 | | - |
22 | | - rt.spawn(async { |
23 | | - println!("Hello from a tokio task!"); |
24 | | - println!("in spawn") |
25 | | - }) |
26 | | - .await |
27 | | - .unwrap(); |
28 | | - }); |
29 | | - |
30 | | - rt.spawn_blocking(|| println!("in spawn_blocking")); |
31 | | -} |
32 | | - |
33 | | -pub fn futures_async() { |
34 | | - let pool = ThreadPool::new().expect("Failed to build pool"); |
35 | | - let (tx, rx) = mpsc::unbounded::<i32>(); |
36 | | - |
37 | | - let fut_values = async { |
38 | | - let fut_tx_result = async move { |
39 | | - (0..100).for_each(|v| { |
40 | | - tx.unbounded_send(v).expect("Failed to send"); |
41 | | - }) |
42 | | - }; |
43 | | - pool.spawn_ok(fut_tx_result); |
44 | | - |
45 | | - let fut_values = rx.map(|v| v * 2).collect(); |
46 | | - |
47 | | - fut_values.await |
48 | | - }; |
49 | | - |
50 | | - let values: Vec<i32> = executor::block_on(fut_values); |
51 | | - |
52 | | - println!("Values={:?}", values); |
53 | | -} |
54 | | - |
55 | | -pub fn futures_lite_async() { |
56 | | - futures_lite::future::block_on(async { println!("Hello from futures_lite") }) |
57 | | -} |
58 | | - |
59 | | -pub fn async_std() { |
60 | | - async_std::task::block_on(async { println!("Hello from async_std") }) |
61 | | -} |
62 | | - |
63 | | -pub fn smol_async() { |
64 | | - smol::block_on(async { println!("Hello from smol") }) |
65 | | -} |
66 | | - |
67 | | -struct Book(); |
68 | | -struct Music(); |
69 | | - |
70 | | -async fn get_book() -> Result<Book, String> { |
71 | | - println!("in get_book"); |
72 | | - Ok(Book()) |
73 | | -} |
74 | | -async fn get_music() -> Result<Music, String> { |
75 | | - println!("in get_music"); |
76 | | - Ok(Music()) |
77 | | -} |
78 | | -async fn get_book_and_music() -> Result<(Book, Music), String> { |
79 | | - let book_fut = get_book(); |
80 | | - let music_fut = get_music(); |
81 | | - try_join!(book_fut, music_fut) |
82 | | -} |
83 | | - |
84 | | -pub fn join() { |
85 | | - futures_lite::future::block_on(async { get_book_and_music().await }).unwrap(); |
86 | | -} |
87 | | - |
88 | | -pub fn select() { |
89 | | - futures_lite::future::block_on(async { |
90 | | - let t1 = get_book().fuse(); |
91 | | - let t2 = get_music().fuse(); |
92 | | - |
93 | | - pin_mut!(t1, t2); |
94 | | - |
95 | | - select! { |
96 | | - _x = t1 => println!("select get_book"), |
97 | | - _y = t2 => println!("select get_music"), |
98 | | - } |
99 | | - }); |
100 | | -} |
0 commit comments