protohackers/problem_06/src/shutdown.rs
2023-05-21 20:27:51 +02:00

31 lines
522 B
Rust

use tokio::sync::broadcast;
use tracing::debug;
#[derive(Debug)]
pub(crate) struct Shutdown {
shutdown: bool,
notify: broadcast::Receiver<()>,
}
impl Shutdown {
pub(crate) fn new(notify: broadcast::Receiver<()>) -> Shutdown {
Shutdown {
shutdown: false,
notify,
}
}
pub(crate) fn is_shutdown(&self) -> bool {
self.shutdown
}
pub(crate) async fn recv(&mut self) {
if self.shutdown {
return;
}
debug!("waiting for shutdown");
let _ = self.notify.recv().await;
self.shutdown = true;
}
}