Extremely barebones IRC server project for learning Rust.
// Adapted from: https://github.com/tokio-rs/mini-redis/blob/069a8e5ee0de445ad3e642548d196ef6b4fa8bc1/src/shutdown.rs

use tokio::sync::broadcast;

pub struct Shutdown {
    /// The receive half of the channel used to listen for shutdown.
    notify: broadcast::Receiver<i32>,
    received: bool,
}

impl Shutdown {
    /// Create a new `Shutdown` backed by the given `broadcast::Receiver`.
    pub fn new(notify: broadcast::Receiver<i32>) -> Self {
        Self {
            notify,
            received: false,
        }
    }

    pub fn replicate(&self) -> Self {
        Self {
            notify: self.notify.resubscribe(),
            received: self.received,
        }
    }

    /// Receive the shutdown notice, waiting if necessary.
    pub async fn recv(&mut self) {
        if self.received {
            return;
        }

        // Cannot receive a "lag error" as only one value is ever sent.
        let _ = self.notify.recv().await;

        self.received = true;
    }
}