A while back, I posted the following question Help with static lifetime on self when using async , where essentially, I wanted to know if I could use the below method self.wacky() and still get the program to compile.
Now, with the introduction of GATs, is there somehow a way to fix this?
use std::future::Future;
use futures::future::{self, FutureExt};
use std::pin::Pin;
use std::task::{Context, Poll};
#[derive(Debug)]
struct Payload {
item: u32
}
struct ResponseWrapper {
inner: Pin<Box<dyn Future<Output=Payload> + Send>>
}
impl Future for ResponseWrapper {
type Output = Payload;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Future::poll(Pin::new(&mut self.inner), cx)
}
}
trait Processor {
fn process(&self, item: Payload) -> ResponseWrapper;
}
struct ConcreteProcessor;
impl Processor for ConcreteProcessor {
fn process(&self, item: Payload) -> ResponseWrapper {
let in_my_future = self.works(); // compiles
// let in_my_future = self.wacky(); Fails to compile due to static lifetime associated with async
ResponseWrapper { inner: in_my_future.boxed() }
}
}
impl ConcreteProcessor {
async fn wacky(&self) -> Payload {
future::ready(Payload { item: 200 }).await
}
fn works(&self) -> impl Future<Output=Payload> {
future::ready(Payload { item: 200 })
}
}
#[tokio::main]
async fn main() {
let mut processor = ConcreteProcessor;
let result = processor.process(Payload { item: 340 }).await;
println!("HUGE RES: {:?}", result);
}