util.rs

 1use std::ops::Deref;
 2use std::sync::mpsc::Sender;
 3
 4use parking_lot::Mutex;
 5use thread_local::ThreadLocal;
 6
 7pub struct UnboundedSyncSender<T: Send> {
 8    clonable_sender: Mutex<Sender<T>>,
 9    local_senders: ThreadLocal<Sender<T>>,
10}
11
12impl<T: Send> UnboundedSyncSender<T> {
13    pub fn new(sender: Sender<T>) -> Self {
14        Self {
15            clonable_sender: Mutex::new(sender),
16            local_senders: ThreadLocal::new(),
17        }
18    }
19}
20
21impl<T: Send> Deref for UnboundedSyncSender<T> {
22    type Target = Sender<T>;
23
24    fn deref(&self) -> &Self::Target {
25        self.local_senders
26            .get_or(|| self.clonable_sender.lock().clone())
27    }
28}