Expected trait object 'dyn Future', found opaque `impl Future`

Hi everyone, I encounter this error and tried to solve it but no progress. The context is that I try to return an async callback from a function.

// async callback
let callback = || async {...}
// a function that return the call back
pub fn example() -> (fn() -> dyn futures::Future<Output = DekuValue>){
  ...
  return (callback);
}

And it shows this error

1 Like

Return a Box<dyn futures::Future<Output = DekuValue>> instead. Trait objects can only be returned behind some kind of pointer.

1 Like

I did try the solution but wonder if there was anything missed. It throws another error.

Screen Shot 2022-12-19 at 13.55.49

What does the code look like? You have to box the whole async block

The code is written inside a procedural macro and it looks like this. The idea is I wrap the macro around a function and it would turn a function into an async function, then return it back.

  • #codeblock: the body content of the wrapped function
  • #vis: visibility of function
  • #fn_token: function token fn
  • #ident: function name

Screen Shot 2022-12-19 at 14.01.47

The closure needs to be something like

|| Box::new(async { #codeblock })

You have to do the boxing explicitly, it isn't automatic

1 Like

Awesome, this is solved. Thanks!

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.