Files
simple-rust-tests/__concurrent/async_study/examples/async3.rs
2022-03-19 10:47:04 +08:00

35 lines
682 B
Rust

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,
}
}