How to unbox/deference a `BoxFuture`?


i struggle with unboxing/deferencing a BoxFuture: Rust Playground

I think that the right combination of * or brackets are necessary, but i can not figure it out:

  • foo().await is a pin, not a u32
  • *foo().await is a Future, not a u32
  • (*foo().await).await does not compile, because it is not Size

Any idea?

You can’t “unbox” a BoxFuture, but of course you can use await on it. Your code is containing too many uses of the keyword async, you probably want to write fn foo() instead of async fn foo().

Thank you. The async are in there since this is a simplified issue of a real world problem if have.

async fn foo() -> BoxFuture<'static, u32> means that foo() produces a Future (due to async fn) whose Output in turn is a BoxFuture and only the Output of that is u32. You could also change the call to foo().await.await, but that seems like hacking around the underlying problem.

If you have further questions, maybe a less simplified version of your real world problem would help.

1 Like

Hm, ok .await.wait works, thanks. I have not realised, there is an additional future somewhere in there.

Well, the function needs to return BoxFuture because it is recursive. I struggled with using that return type.
Do you have any suggestion how to handle recursive async functions better?

Seems reasonable to use BoxFuture for that, have you actually tried just removing the extra async before fn like I suggested?

This indeed unnecessary. I initially thought you meant in the dumbed-down case the async is unnecessary.
Thank you.

Keep in mind that

async fn foo(...args) -> ...return_type {
    ...function body

more or less just a shorthand for

fn foo(...args) -> impl Future<Output=...return_type> + 'lifetime {
    async move {
        ...function body

hence the corresponding version of that where it returns a boxed future is

fn foo(...args) -> BoxFuture<'lifetime, ...return_type> {
    async move {
        ...function body

without an async before the fn :wink:


Yes, that makes sense. I was in "add a lot of async keywords" mode in order to make my code work with external async functions.
After making the return type a BoxFuture i did not think of removing the async for that one function.

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.