protohackers/problem_06/src/shutdown.rs

31 lines
469 B
Rust
Raw Normal View History

2023-05-06 05:41:24 +00:00
use tokio::sync::broadcast;
#[derive(Debug)]
pub(crate) struct Shutdown {
shutdown: bool,
notify: broadcast::Receiver<()>,
2023-05-06 05:41:24 +00:00
}
impl Shutdown {
pub(crate) fn new(notify: broadcast::Receiver<()>) -> Shutdown {
Shutdown {
shutdown: false,
notify,
}
}
2023-05-06 05:41:24 +00:00
pub(crate) fn is_shutdown(&self) -> bool {
self.shutdown
}
2023-05-06 05:41:24 +00:00
pub(crate) async fn recv(&mut self) {
if self.shutdown {
return;
}
2023-05-06 05:41:24 +00:00
let _ = self.notify.recv().await;
2023-05-06 05:41:24 +00:00
self.shutdown = true;
}
2023-05-06 05:41:24 +00:00
}