This code fails to compile
use tokio_util::task::LocalPoolHandle;
pub struct Test<T> {
thread_pool: LocalPoolHandle,
writer_provider: T,
}
impl<T> Test<T>
{
async fn initialize_threads(&mut self) {
self.thread_pool.spawn_pinned(move || {
Self::handle_log_event()
});
}
async fn handle_log_event() -> () {
print!("Logging event");
}
}
error[E0310]: the parameter type `T` may not live long enough
--> src/manager/test.rs:33:9
|
33 | / self.thread_pool.spawn_pinned(move || {
34 | |
35 | | Self::handle_log_event()
36 | |
37 | | });
| | ^
| | |
| |__________the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
29 | impl<T: 'static> Test<T>
However when i put the async function in an async block, then everything works fine with same behavior.
use tokio_util::task::LocalPoolHandle;
pub struct Test<T> {
thread_pool: LocalPoolHandle,
writer_provider: T,
}
impl<T> Test<T>
{
async fn initialize_threads(&mut self) {
self.thread_pool.spawn_pinned(move || {
async {
Self::handle_log_event().await
}
});
}
async fn handle_log_event() -> () {
print!("Logging event");
}
}
i was under the impression that the only thing async blocks do is return a future that resolves to the value returned from the block. So the two above code snippets should be equivalent. So why would one result in a compilation error?