use std::{error, fs, io, path}; 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 TaskStack where E: error::Error + Send + 'static, { canceller: CancellationToken, wait_group: Vec>>, } impl TaskStack where E: error::Error + Send + 'static, { pub fn new() -> TaskStack { TaskStack { canceller: CancellationToken::new(), wait_group: Vec::new(), } } pub fn spawn(&mut 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(mut self) -> Result<(), E> { self.canceller.cancel(); // reverse wait_group in place, so we stop the most recently added first. Since this method // consumes self this is fine. self.wait_group.reverse(); for f in self.wait_group { if let Err(err) = f.await.expect("task failed") { return Err(err); } } Ok(()) } }