Consider the following code snippet.
use futures::{FutureExt, future::{self, BoxFuture}};
fn inject<'a, Env: 'a, A: 'a + Send>(v: A) -> Box<dyn FnOnce(&'a Env) -> BoxFuture<'a, A>> {
Box::new(move |_| future::ready(v).boxed())
}
This does not compile with the following error (on rust playground stable channel):
Compiling playground v0.0.1 (/playground)
error[E0310]: the parameter type `A` may not live long enough
--> src/main.rs:4:5
|
3 | fn inject<'a, Env: 'a, A: 'a + Send>(v: A) -> Box<dyn FnOnce(&'a Env) -> BoxFuture<'a, A>> {
| -- help: consider adding an explicit lifetime bound...: `A: 'static +`
4 | Box::new(move |_| future::ready(v).boxed())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `[closure@src/main.rs:4:14: 4:47 v:A]` will meet its required lifetime bounds
error: aborting due to previous error
For more information about this error, try `rustc --explain E0310`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
I have a hard time seeing why A needs the 'static lifetime here. The BoxFuture I'm returning is parameterized by the same lifetime 'a, which means it can only be used within the scope of 'a. And during that same scope, the value v: A is also valid, so why 'static?
Changing the constraint A: 'a + Send into A: 'static + Send does make the code compile, but seems unnecessary to me...
Thanks!