Let's say I have a trait IMyWriter
that has a function flush_to_disk
that gets an iterator of the pieces to write. Each piece that's written should yield a Result object.
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>>;
}
I'm having trouble with doing the mapping in the implementation:
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()
})
}
}
This obviously complains:
type parameter `Results`, found struct `std::iter::Map`
I can't for the life of me figure out how to return the results for the stream of asynchronous writes that happen.