use std::{error, fs, io, path}; use futures::stream::futures_unordered::FuturesUnordered; use tokio_util::sync::CancellationToken; pub fn open_file(path: &path::Path) -> io::Result> { match fs::File::open(path) { Ok(file) => Ok(Some(file)), Err(err) => match err.kind() { io::ErrorKind::NotFound => Ok(None), _ => Err(err), }, } } pub struct TaskSet where E: error::Error + Send + 'static, { canceller: CancellationToken, wait_group: FuturesUnordered>>, } impl TaskSet where E: error::Error + Send + 'static, { pub fn new() -> TaskSet { TaskSet { canceller: CancellationToken::new(), wait_group: FuturesUnordered::new(), } } pub fn spawn(&self, mut f: F) where Fut: futures::Future> + Send + 'static, F: FnMut(CancellationToken) -> Fut, { let canceller = self.canceller.clone(); let handle = tokio::spawn(f(canceller)); self.wait_group.push(handle); } pub async fn stop(self) -> Vec { self.canceller.cancel(); let mut res = Vec::new(); for f in self.wait_group { if let Err(err) = f.await.expect("task failed") { res.push(err); } } res } }