I have a function that requires an asynchronous callback (a request handler); I'm currently trying to accept things that look like this:
async fn handle_request<'a>(request: Request, body: &'a mut (dyn AsyncRead + 'a)) -> HandlerResponse
It had been working up until the addition of the second parameter body
, which is causing me grief. The function that accepts the parameter looks like this:
pub async fn process_requests<H, F>(
mut connection: Box<dyn AsyncConnection>,
request_handler: &H,
) -> Result<(), DecodeError>
where
for<'a> H: Fn(crate::Request, &'a mut (dyn AsyncRead + 'a)) -> F + 'a,
F: Future<Output = HandlerResponse>,
{
Part way through this function, we call a helper function:
handle_request(&mut connection, request_handler, request)
which has a very similar signature; in particular, the signature for request_handler
is identical. It does some minor pre-processing before invoking request_handler
. When I attempt to compile this, I get:
error[E0310]: the parameter type `H` may not live long enough
|
106 | pub async fn process_requests<H, F>(
| - help: consider adding an explicit lifetime bound `H: 'static`...
...
142 | handle_request(&mut connection, request_handler, request)
| ^^^^^^^^^^^^^^
|
note: ...so that the type `H` will meet its required lifetime bounds
|
142 | handle_request(&mut connection, request_handler, request)
| ^^^^^^^^^^^^^^
Why do I need this / what do I do about this? Indeed adding 'static
to H:
in the where
does seem to silence the error, but is that the right thing to do? Couldn't the type implementing H
carry a reference, and 'static
would forbid that? I don't necessarily want to do that, but any amount of trying to annotate a lifetime that isn't 'static
onto H
has not worked; e.g., 'a + Fn(...) -> F + 'a
does not work, nor does adding a new generic 'b
lifetime. I'd rather not 'static
something that doesn't need it, but I don't see how to do that.
(I'm also a bit perplexed by the wording of the message — that the parameter type — not some argument or variable — doesn't live long enough. How does a type not live long enough?)
I've played with things a bit more, but I still can't get anything that actually compiles. This playground example shows another one of the more perplexing error messages that I'm running into. I've tried dropping some of the lifetime annotations, and moved the for<'a>
bit (I'm not sure what the difference is?).