I want to call async function in Future::poll(), but the poll() is not async function, so I have to poll the async fn, but got error:
error[E0599]: no method named `poll` found for opaque type `impl Future` in the current scope
--> src\lib.rs:18:22
|
18 | let a = fut1.poll(cx);
| ^^^^ method not found in `impl Future`
I try to Pin::new(async fn()).poll(), got another error:
error[E0277]: `from_generator::GenFuture<[static generator@src\lib.rs:33:23: 36:2 {ResumeTy, u64, Duration, impl Future, ()}]>` cannot be unpinned
--> src\lib.rs:22:23
|
22 | let pinfut1 = Pin::new(&mut fut1);
| ^^^^^^^^ within `impl Future`, the trait `Unpin` is not implemented for `from_generator::GenFuture<[static generator@src\lib.rs:33:23: 36:2 {ResumeTy, u64, Duration, impl Future, ()}]>`
...
33 | async fn fn1() -> i32 {
| --- within this `impl Future`
code:
use std::future::Future;
use std::task::{Context, Poll};
use std::pin::Pin;
use std::sync::{Arc,Mutex};
#[pin_project::pin_project]
struct Person<'a> {
name: &'a str,
age: i32,
}
impl<'a> Future for Person<'a> {
type Output = i32;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
*this.age += 1;
// error: poll() ^^^^ method not found in `impl Future`
// let mut fut1 = fn1();
// let a = fut1.poll(cx);
// error: Pin::new() ^^^^^^^^ within `impl Future`, the trait `Unpin` is not implemented for `from_generator::GenFuture
let mut fut1 = fn1();
let pinfut1 = Pin::new(&mut fut1);
let a = pinfut1.poll(cx);
if *this.age > 4 {
Poll::Ready(*this.age)
} else {
Poll::Pending
}
}
}
async fn fn1() -> i32 {
async_std::task::sleep(std::time::Duration::from_secs(2)).await;
123
}
fn main() {
let p1 = Person {name: "jack", age: Default::default()};
async_std::task::block_on(async {
let a = p1.await;
dbg!(a);
});
}
impl Future
, the traitUnpin
is not implemented forfrom_generator::GenFuture<[static generator@src/main.rs:29:11: 34:6]>
– Cooncan