I am trying to store an async closure in a struct. I would normally just store the future, but the function needs to be run multiple times.
Essentially I am trying to do the following:
struct Foo { bar : Box<dyn Fn() -> impl Future<Output = ()>> }
but I'm getting the following error:
impl Trait not allowed outside of function and inherent method return types
Is there a more idiomatic method than writing the whole Future out like:
struct Foo { bar : Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()>>>> }
?
This compiles until I try to instantiate the struct. The code:
Foo { bar : Box::new(|| async { println!("Hello Async!") }) }
gives the error: expected struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = ()> + 'static)>>` found opaque type `impl std::future::Future