Hey, to learn async better I wanted to implement a custom future which can retry another future after a delay. I know you can do this easily with one async fn retry(impl AsyncFn()) but this does not help understanding async.
What I wanted the api to look like:
FutureRetry::new(async || http.send(body).await).await?
However I could only get it to work when the closure does not capture anything and returns ownership of its arguments like so.
FutureRetry::new(async |(http, body)| ((http, body), http.send(body).await)).await?
Compiling version
When I try to capture the environment using FnMut() -> Future
FutureRetry::new(|| async http.send(body).await).await?
Rust tells me that the FnMut() closure cant return types referencing its environment, which makes sense because the future returned is referencing the closure environment, this seems like compiler limitation, because those references are valid when the function returns.
Ok so let's use async closures then.
With AsyncFnMut() now this returns a future which mutably borrows from self so far so good, but I also need to store this future in my own custom future to poll later, this doesn't work because now I have 2 mutable references, 1 in the future and second one when I try to assign it self.current_future = self.future_factory(). I guess I'm trying to have self referential types which is not possible in safe rust.
I know this could maybe be solved with AsyncFnOnce and cloning everything so I don't store references in the returned future, but I don't want to do this.
What am I missing here, is it really not possible to have such an api where a custom future impl polls another future which mutably borrows its environment from self in safe rust today?
Thanks in advance