use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use tokio::sync::mpsc::{Sender, error::SendError};
#[derive(Debug, Clone)]
pub struct MSender<T> {
s: Sender<T>,
c: Arc<AtomicBool>,
}
impl<T> MSender<T> {
pub fn new(s: Sender<T>) -> Self {
Self {
s,
c: Arc::new(AtomicBool::new(false)),
}
}
pub fn 已连接(&self, c: bool) {
self.c.store(c, Ordering::SeqCst);
}
pub async fn send(&self, m: T) -> Result<(), SendError<T>> {
if self.c.load(Ordering::SeqCst) {
self.s.send(m).await
} else {
Ok(())
}
}
}