feat: add async study
This commit is contained in:
8
__concurrent/async_study/examples/async.rs
Normal file
8
__concurrent/async_study/examples/async.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
invoke().await
|
||||
}
|
||||
|
||||
async fn invoke() {
|
||||
println!("Hello World!");
|
||||
}
|
||||
23
__concurrent/async_study/examples/async2.rs
Normal file
23
__concurrent/async_study/examples/async2.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
invoke().await
|
||||
}
|
||||
|
||||
struct FutureImpl {}
|
||||
|
||||
impl Future for FutureImpl {
|
||||
type Output = ();
|
||||
|
||||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
println!("Hello World!");
|
||||
Poll::Ready(())
|
||||
}
|
||||
}
|
||||
|
||||
fn invoke() -> impl Future<Output=()> {
|
||||
FutureImpl {}
|
||||
}
|
||||
35
__concurrent/async_study/examples/async3.rs
Normal file
35
__concurrent/async_study/examples/async3.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
invoke().await
|
||||
}
|
||||
|
||||
struct FutureImpl {
|
||||
i: i32,
|
||||
}
|
||||
|
||||
impl Future for FutureImpl {
|
||||
type Output = ();
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
if self.i == 0 {
|
||||
let s = &mut *self;
|
||||
s.i += 1;
|
||||
println!("Hello World, pending");
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
} else {
|
||||
println!("Hello World, ready");
|
||||
Poll::Ready(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn invoke() -> impl Future<Output=()> {
|
||||
FutureImpl {
|
||||
i: 0,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user