I think the problem is the dyn Future return type of the closure which would be an maybe sized return type which isn’t allowed. Maybe you want impl Future for the return type. Can’t remember if that’s allowed off hand for closure returns though.
I knew that wouldn’t work as I was sending it. This seems to work though. Obviously it’s a stripped down example so it might not work when you add in all your code.
It seems BoxFuture requires a lifetime annotation. I tried 'static but got errors like this:
type ServiceFn = Arc<dyn (Fn(Request<MyBody>) -> BoxFuture<'static, Response<MyBody>>) + Send + Sync + 'static>;
<snip>
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
async fn event_handler(
<snip>
handler: ServiceFn, // type alias for the whole `Arc<...>` thing
)
{
^
note: hidden type `impl core::future::future::Future` captures the scope of call-site for function at 497:1
Also, the input handler of the above fn event_handler will eventually be cloned and then passed to a new async task.
Do I need to use 'a for the lifetime of BoxFuture and propagate to Foo<'a> ? I'm trying to limit the complexity if possible.
Just to follow up, I ended up using generics with trait bounds without Box or dyn or Arc. It turns out easier than I expected, as it is very much same with the current trait bounds with function parameters. The code looks like this: