如何在使用泛型的tokio异步任务中满足生命周期限制。

huangapple go评论62阅读模式
英文:

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&lt;T: Clone + Send + Sync&gt;() {
    let (data_tx, mut data_rx) = tokiompsc::channel::&lt;T&gt;(10);
    tokio::spawn(async move {
        let mut destinations: Vec&lt;T&gt; = Vec::new();
        data_rx.recv();
    });
}

Compiler error:

error[E0310]: the parameter type `T` may not live long enough
  --&gt; src\history_generic.rs:18:5
   |
18 | /     tokio::spawn(async move {
19 | |         let mut destinations: Vec&lt;T&gt; = 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&lt;T: Clone + Send + Sync + &#39;static&gt;() {
   |                                        +++++++++

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 &#39;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&lt;T&gt; 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 + &#39;static to the bounds on T.

&#39;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 &#39;static bound cannot easily be moved between separate async tasks because that would restrict the lifetimes of those tasks.

More details on a &#39;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.

huangapple
  • 本文由 发表于 2023年5月13日 11:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定