Potential GAT solution for Static lifetimes on self while using async?

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);
}

I don't think there's any new way to fix things due to GAT. The Future trait hasn't changed, and there's no associated type on Processor -- and if there was, I don't see how changing it to a GAT would be useful without adding a lifetime to ResponseWrapper, which was already a fix (insomuch as it allows the example to compile).

I figured as much, but I also figured it couldn't hurt to ask!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.