lavina/src/util/mod.rs

31 lines
810 B
Rust
Raw Normal View History

2023-02-07 15:21:00 +00:00
use futures_util::FutureExt;
use tokio::sync::oneshot::{channel, Sender};
use crate::prelude::*;
2023-02-04 01:01:49 +00:00
pub mod table;
2023-02-07 15:21:00 +00:00
pub struct Terminator {
signal: Sender<()>,
completion: JoinHandle<Result<()>>,
}
impl Terminator {
pub fn from_raw(signal: Sender<()>, completion: JoinHandle<Result<()>>) -> Terminator {
Terminator { signal, completion }
}
pub fn new(completion: JoinHandle<Result<()>>) -> (Terminator, impl Future<Output = ()>) {
let (signal, rx) = channel();
(Terminator { signal, completion }, rx.map(|_| ()))
}
pub async fn terminate(self) -> Result<()> {
match self.signal.send(()) {
Ok(()) => {}
Err(_) => log::error!("Termination channel is dropped"),
}
self.completion.await??;
Ok(())
}
}