英文:
How to satisfy lifetime bounds in tokio async tasks with generics
问题
我试图使用tokio::spawn运行一个持有Vec<T>的异步任务。根据编译错误消息,我的理解是我还没有满足生命周期边界,但我不确定正确的解决方法是什么。
编译器错误:
错误[E0310]: 参数类型 `T` 可能不会存在足够长的时间
--> src\history_generic.rs:18:5
|
18 | / tokio::spawn(async move {
19 | | let mut destinations: Vec<T> = Vec::new();
20 | | data_rx.recv();
21 | | });
| |______^ ... 以使类型 `T` 满足其所需的生命周期边界
|
帮助:考虑添加一个明确的生命周期限制...
|
16 | pub fn _history<T: Clone + Send + Sync + 'static>() {
| +++++++++
我已经查阅了学习Rust教程,但不理解如何解决这个问题。
英文:
I am trying to run an async task using tokio::spawn that holds a Vec<T>. My understanding from the compile error message is that I haven't satisfied the lifetime bounds, but I'm not sure what the correct way to achieve that is.
use tokio::sync::mpsc as tokiompsc;
pub fn _history<T: Clone + Send + Sync>() {
let (data_tx, mut data_rx) = tokiompsc::channel::<T>(10);
tokio::spawn(async move {
let mut destinations: Vec<T> = Vec::new();
data_rx.recv();
});
}
Compiler error:
error[E0310]: the parameter type `T` may not live long enough
--> src\history_generic.rs:18:5
|
18 | / tokio::spawn(async move {
19 | | let mut destinations: Vec<T> = Vec::new();
20 | | data_rx.recv();
21 | | });
| |______^ ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
16 | pub fn _history<T: Clone + Send + Sync + 'static>() {
| +++++++++
I have consulted the learn rust tutorial and do not understand how to fix the problem.
答案1
得分: 1
If you consult the documentation for tokio::spawn
, it requires a bound of 'static
on the future that it takes as an argument (in this case, your async move
closure). I believe that this means that T
needs to have the same bound since a Vec<T>
is captured as part of the closure, and is part of the state of the Future
. As the error message suggests, one way to avoid this would be to add + 'static
to the bounds on T
.
'static
indicates that the type is not bound by any other lifetime shorter than the lifetime of the program - basically, if you have a T
then that T
does not contain a reference to any other data that might disappear while the program is running. In general, types that do not satisfy a 'static
bound cannot easily be moved between separate async tasks because that would restrict the lifetimes of those tasks.
More details on a 'static
lifetime bound: https://doc.rust-lang.org/rust-by-example/scope/lifetime/static_lifetime.html#trait-bound. As this mentions, most 'normal' owned types satisfy this bound.
英文:
If you consult the documentation for tokio::spawn
, it requires a bound of 'static
on the future that it takes as an argument (in this case, your async move
closure). I believe that this means that T
needs to have the same bound since a Vec<T>
is captured as part of the closure, and is part of the state of the Future
. As the error message suggests, one way to avoid this would be to add + 'static
to the bounds on T
.
'static
indicates that the type is not bound by any other lifetime shorter than the lifetime of the program - basically, if you have a T
then that T
does not contain a reference to any other data that might disappear while the program is running. In general, types that do not satisfy a 'static
bound cannot easily be moved between separate async tasks because that would restrict the lifetimes of those tasks.
More details on a 'static
lifetime bound: https://doc.rust-lang.org/rust-by-example/scope/lifetime/static_lifetime.html#trait-bound. As this mentions, most 'normal' owned types satisfy this bound.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论