1

假设我有一个特征IMyWriter,它有一个函数flush_to_disk可以获取要编写的片段的迭代器。编写的每一部分都应该产生一个 Result 对象。

use async_trait::async_trait;
use async_std::prelude::StreamExt;

#[async_trait]
trait IMyWriter<'a> {
   async fn flush_to_disk<'b, 'c: 'a + 'b, Iter, Results>(&mut self, pieces: Iter) -> Results 
    where
      Iter: IntoIterator<Item = u64>,
      Results: StreamExt<Item=Result<(), std::io::Error>>;
}

我在实现中进行映射时遇到问题:

use async_std::io::File;
use async_trait::async_trait;

struct MyWriter {
    file: Mutex<File>;
}

#[async_trait]
impl IMyWriter for MyWriter {
   async fn flush_to_disk<'b, 'c: 'a + 'b, Iter, Results>(&mut self, pieces: Iter) -> Results 
    where
      Iter: IntoIterator<Item = u64>,
      Results: StreamExt<Item=Result<(), std::io::Error>> {
      pieces.into_iter().map(|piece| async {
         let f = self.file.lock().await;
         // Do I/O on file
         Ok()
      })
   }
}

这显然抱怨:

type parameter `Results`, found struct `std::iter::Map`

我一生都无法弄清楚如何为发生的异步写入流返回结果。

4

0 回答 0