domani/src/util.rs

59 lines
1.5 KiB
Rust
Raw Normal View History

use std::{error, fs, io, path};
use tokio_util::sync::CancellationToken;
pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
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<E>
where
E: error::Error + Send + 'static,
{
canceller: CancellationToken,
wait_group: Vec<tokio::task::JoinHandle<Result<(), E>>>,
}
impl<E> TaskStack<E>
where
E: error::Error + Send + 'static,
{
pub fn new() -> TaskStack<E> {
TaskStack {
canceller: CancellationToken::new(),
wait_group: Vec::new(),
}
}
pub fn spawn<F, Fut>(&mut self, mut f: F)
where
Fut: futures::Future<Output = Result<(), E>> + 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(())
}
}