|
1 | | -pub use tokio::sync::{mpsc, oneshot}; |
| 1 | +pub use tokio::sync::oneshot; |
| 2 | + |
| 3 | +pub mod mpsc { |
| 4 | + use std::{ |
| 5 | + sync::{Arc, Weak}, |
| 6 | + task::{Context, Poll}, |
| 7 | + }; |
| 8 | + use tokio::sync::mpsc::error::*; |
| 9 | + pub use tokio::sync::mpsc::{self, *}; |
| 10 | + |
| 11 | + pub struct UnboundedSender<T>(mpsc::UnboundedSender<T>, Arc<()>); |
| 12 | + pub struct UnboundedReceiver<T>(mpsc::UnboundedReceiver<T>); |
| 13 | + |
| 14 | + pub type TrackedUnboundedSender<T> = UnboundedSender<Tracked<T>>; |
| 15 | + pub type TrackedUnboundedReceiver<T> = UnboundedReceiver<Tracked<T>>; |
| 16 | + |
| 17 | + #[allow(dead_code)] |
| 18 | + pub struct Tracked<T>(pub T, pub Tracker); |
| 19 | + #[allow(dead_code)] |
| 20 | + pub struct Tracker(Weak<()>); |
| 21 | + |
| 22 | + impl<T> std::fmt::Debug for UnboundedSender<T> { |
| 23 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 24 | + write!(f, "{:?} (len: {})", self.0, self.len()) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + impl<T> std::fmt::Debug for UnboundedReceiver<T> { |
| 29 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 30 | + write!(f, "{:?} (len: {})", self.0, self.len()) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + impl<T> Clone for UnboundedSender<T> { |
| 35 | + fn clone(&self) -> Self { |
| 36 | + Self(self.0.clone(), self.1.clone()) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + impl<T> std::ops::Deref for Tracked<T> { |
| 41 | + type Target = T; |
| 42 | + |
| 43 | + fn deref(&self) -> &Self::Target { |
| 44 | + &self.0 |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + impl<T> std::ops::DerefMut for Tracked<T> { |
| 49 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 50 | + &mut self.0 |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + impl<T> UnboundedSender<T> { |
| 55 | + pub fn is_empty(&self) -> bool { |
| 56 | + self.len() == 0 |
| 57 | + } |
| 58 | + |
| 59 | + pub fn len(&self) -> usize { |
| 60 | + Arc::weak_count(&self.1) |
| 61 | + } |
| 62 | + |
| 63 | + pub fn send(&self, message: T) -> Result<(), SendError<T>> { |
| 64 | + self.0.send(message) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + impl<T> TrackedUnboundedSender<T> { |
| 69 | + pub fn tracked_send(&self, message: T) -> Result<(), SendError<T>> { |
| 70 | + let msg = Tracked(message, Tracker(Arc::downgrade(&self.1))); |
| 71 | + self.send(msg).map_err(|err| SendError(err.0 .0)) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + impl<T> UnboundedReceiver<T> { |
| 76 | + pub fn is_empty(&self) -> bool { |
| 77 | + self.0.is_empty() |
| 78 | + } |
| 79 | + |
| 80 | + pub fn len(&self) -> usize { |
| 81 | + self.0.len() |
| 82 | + } |
| 83 | + |
| 84 | + pub async fn recv(&mut self) -> Option<T> { |
| 85 | + self.0.recv().await |
| 86 | + } |
| 87 | + |
| 88 | + pub fn try_recv(&mut self) -> Result<T, TryRecvError> { |
| 89 | + self.0.try_recv() |
| 90 | + } |
| 91 | + |
| 92 | + pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> { |
| 93 | + self.0.poll_recv(cx) |
| 94 | + } |
| 95 | + |
| 96 | + pub fn blocking_recv(&mut self) -> Option<T> { |
| 97 | + self.0.blocking_recv() |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + pub fn unbounded_channel<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) { |
| 102 | + let (tx, rx) = mpsc::unbounded_channel(); |
| 103 | + |
| 104 | + (UnboundedSender(tx, Arc::new(())), UnboundedReceiver(rx)) |
| 105 | + } |
| 106 | + |
| 107 | + pub fn tracked_unbounded_channel<T>( |
| 108 | + ) -> (UnboundedSender<Tracked<T>>, UnboundedReceiver<Tracked<T>>) { |
| 109 | + let (tx, rx) = mpsc::unbounded_channel(); |
| 110 | + |
| 111 | + (UnboundedSender(tx, Arc::new(())), UnboundedReceiver(rx)) |
| 112 | + } |
| 113 | +} |
2 | 114 |
|
3 | 115 | pub mod broadcast { |
4 | 116 | pub use tokio::sync::broadcast::*; |
|
0 commit comments