1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
use super::Channel; use crate::{error::SendError as LifelineSendError, impl_storage_clone, impl_storage_take}; use crate::{impl_channel_clone, impl_channel_take}; use async_trait::async_trait; use postage::sink::Sink; use postage::stream::Stream; use postage::{barrier, broadcast, mpsc, oneshot, watch}; use std::fmt::Debug; impl<T: Send + 'static> Channel for mpsc::Sender<T> { type Tx = Self; type Rx = mpsc::Receiver<T>; fn channel(capacity: usize) -> (Self::Tx, Self::Rx) { mpsc::channel(capacity) } fn default_capacity() -> usize { 16 } } impl_channel_clone!(mpsc::Sender<T>); impl_channel_take!(mpsc::Receiver<T>); #[async_trait] impl<T> crate::Sender<T> for mpsc::Sender<T> where T: Debug + Send, { async fn send(&mut self, value: T) -> Result<(), LifelineSendError<T>> { Sink::send(self, value) .await .map_err(|err| LifelineSendError::Return(err.0)) } } #[async_trait] impl<T> crate::Receiver<T> for mpsc::Receiver<T> where T: Debug + Send, { async fn recv(&mut self) -> Option<T> { Stream::recv(self).await } } impl<T: Clone + Send + 'static> Channel for broadcast::Sender<T> { type Tx = Self; type Rx = broadcast::Receiver<T>; fn channel(capacity: usize) -> (Self::Tx, Self::Rx) { broadcast::channel(capacity) } fn default_capacity() -> usize { 16 } fn clone_rx(rx: &mut Option<Self::Rx>, tx: Option<&Self::Tx>) -> Option<Self::Rx> { rx.take().or_else(|| tx.map(|tx| tx.subscribe())) } } impl_channel_clone!(broadcast::Sender<T>); impl_channel_take!(broadcast::Receiver<T>); #[async_trait] impl<T> crate::Sender<T> for broadcast::Sender<T> where T: Clone + Debug + Send, { async fn send(&mut self, value: T) -> Result<(), LifelineSendError<T>> { Sink::send(self, value) .await .map(|_| ()) .map_err(|err| LifelineSendError::Return(err.0)) } } #[async_trait] impl<T> crate::Receiver<T> for broadcast::Receiver<T> where T: Clone + Debug + Send, { async fn recv(&mut self) -> Option<T> { Stream::recv(self).await } } impl<T: Send + 'static> Channel for oneshot::Sender<T> { type Tx = Self; type Rx = oneshot::Receiver<T>; fn channel(_capacity: usize) -> (Self::Tx, Self::Rx) { oneshot::channel() } fn default_capacity() -> usize { 1 } } impl_channel_take!(oneshot::Sender<T>); impl_channel_take!(oneshot::Receiver<T>); impl<T> Channel for watch::Sender<T> where T: Default + Clone + Send + Sync + 'static, { type Tx = Self; type Rx = watch::Receiver<T>; fn channel(_capacity: usize) -> (Self::Tx, Self::Rx) { watch::channel() } fn default_capacity() -> usize { 1 } } impl_channel_take!(watch::Sender<T>); impl_channel_clone!(watch::Receiver<T>); #[async_trait] impl<T> crate::Sender<T> for watch::Sender<T> where T: Clone + Debug + Send + Sync, { async fn send(&mut self, value: T) -> Result<(), LifelineSendError<T>> { Sink::send(self, value) .await .map_err(|_| LifelineSendError::Closed) } } #[async_trait] impl<T> crate::Receiver<T> for watch::Receiver<T> where T: Clone + Debug + Send + Sync, { async fn recv(&mut self) -> Option<T> { Stream::recv(self).await } } impl Channel for barrier::Sender { type Tx = Self; type Rx = barrier::Receiver; fn channel(_capacity: usize) -> (Self::Tx, Self::Rx) { barrier::channel() } fn default_capacity() -> usize { 1 } } impl_storage_take!(barrier::Sender); impl_storage_clone!(barrier::Receiver);