Hi, I'm trying to learn future, I want manually run two future concurrently. but with following code, I got some weird error. here is my code, and playground linke is:
error msg also pasted here. but I already have trait bound in where clause. I know I don't have any runtime in this code, because I want keep the example code as simple as possible. but the idea should work? right? Thanks
54 | match self.a.poll(cx) {
| ^^^^ method not found in `A`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `poll`, perhaps you need to restrict type parameter `A` with it:
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
fn main() {
let f1 = async {
println!("async1");
};
let f2 = async {
println!("async2");
};
let join = Join2::new(f1, f2);
}
struct Join2<A, B>
where
A: std::future::Future,
B: std::future::Future,
{
a: A,
ar: Option<A::Output>,
b: B,
br: Option<B::Output>,
}
impl<A, B> Join2<A, B>
where
A: std::future::Future,
B: std::future::Future,
{
fn new(a: A, b: B) -> Self {
Join2 {
a,
ar: None,
b,
br: None,
}
}
}
impl<A, B> Future for Join2<A, B>
where
A: std::future::Future,
B: std::future::Future,
{
type Output = (A::Output, B::Output);
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut done = true;
if self.ar.is_none() {
// let mut future = unsafe { Pin::new_unchecked(&mut self.a) };
// match Future::poll(future, &mut cx) {
match self.a.poll(cx) {
Poll::Ready(val) => {
self.ar = Some(val);
}
Poll::Pending => {
done = false;
}
}
}
if self.br.is_none() {
// let mut future = unsafe { Pin::new_unchecked(&mut self.b) };
// match Future::poll(future, &mut cx) {
match self.b.poll(cx) {
Poll::Ready(val) => {
self.br = Some(val);
}
Poll::Pending => {
done = false;
}
}
}
if done {
Poll::Ready((self.ar.unwrap(), self.br.unwrap()))
} else {
Poll::Pending
}
}
}