异步迭代 BTreeSet 时出现奇怪的生命周期错误

ale*_*pha 18 generics lifetime rust async-await

我希望以下代码中的异步块实现SendPlayground):

use std::collections::BTreeSet;
use std::future::ready;

pub fn test<T: Sync>(set: &BTreeSet<T>) -> impl Send + '_ {
    async move {
        for _ in set {
            ready(()).await;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它给出了以下错误:

   Compiling playground v0.0.1 (/playground)
error[E0311]: the parameter type `T` may not live long enough
 --> src/lib.rs:4:44
  |
4 | pub fn test<T: Sync>(set: &BTreeSet<T>) -> impl Send + '_ {
  |             --                             ^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
  |             |
  |             help: consider adding an explicit lifetime bound...: `T: 'a +`

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.
Run Code Online (Sandbox Code Playgroud)

我根本不明白这个错误。添加生命周期界限并不能解决问题 ( Playground ),除非添加的生命周期界限是'static( Playground )。

我试着更换BTreeSetVecVecDequeLinkedListHashSetBinaryHeap。全部编译没有错误。有什么特别之处BTreeSet

Ult*_*rus 1

看来该错误是 Rust 中的一个错误——async函数相当新,并且似乎存在许多奇怪或不正确的编译器错误的问题,尤其是泛型。我认为这可能是问题 #71058问题 #64552

我发现经常出现这样的终身错误,只是意味着编译器在说“救命!我很困惑”。

这是一个无偿更改的示例,我认为其功能相同:

use std::collections::BTreeSet;
use std::future::ready;

type ItemType = dyn Sync;

pub fn test<ItemType>(set: &BTreeSet<ItemType>) -> impl Send + '_ {
    async move {
        for _ in set {
            ready(()).await;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这会产生一个错误,我认为该错误更接近于编译器的错误(但仍然不正确)Playground

error: future cannot be sent between threads safely
 --> src/lib.rs:6:52
  |
6 | pub fn test<ItemType>(set: &BTreeSet<ItemType>) -> impl Send + '_ {
  |                                                    ^^^^^^^^^^^^^^ future created by async block is not `Send`
  |
note: captured value is not `Send`
 --> src/lib.rs:8:18
  |
8 |         for _ in set {
  |                  ^^^ has type `&BTreeSet<ItemType>` which is not `Send`
help: consider restricting type parameter `ItemType`
  |
6 | pub fn test<ItemType: std::marker::Sync>(set: &BTreeSet<ItemType>) -> impl Send + '_ {
  |                     ^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

上面的 Rust 错误表明未来不会Send出现这种情况,如果异步闭包捕获了不支持的数据结构Send在本例中,它捕获支持Send的BTreeSet 。为什么会发生这种情况BTreeSet,而不是Vec或您提到的其他数据结构之一,可能是其实现语法中的一些微小差异导致编译器出错。

您创建了一个很好的最小示例,因此不确定您要实现什么目标。这是一个可能有帮助的解决方法:

use std::collections::BTreeSet;
use std::future::ready;
use std::vec::Vec;
use futures::future::join_all;

pub async fn test<T: Sync>(set: &BTreeSet<T>) -> impl Send + '_ {
    let mut future_list = Vec::new();
    for _ in set {
        let new_future = async move {
            ready(()).await;
        };
        future_list.push(new_future);
    };
    join_all(future_list).await
}
Run Code Online (Sandbox Code Playgroud)