mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-08 09:30:45 +01:00
Refactor
This commit is contained in:
29
src/bots_manager/closable_sender.rs
Normal file
29
src/bots_manager/closable_sender.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
pub struct ClosableSender<T> {
|
||||
origin: std::sync::Arc<std::sync::RwLock<Option<mpsc::UnboundedSender<T>>>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for ClosableSender<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
origin: self.origin.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ClosableSender<T> {
|
||||
pub fn new(sender: mpsc::UnboundedSender<T>) -> Self {
|
||||
Self {
|
||||
origin: std::sync::Arc::new(std::sync::RwLock::new(Some(sender))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self) -> Option<mpsc::UnboundedSender<T>> {
|
||||
self.origin.read().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
self.origin.write().unwrap().take();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user