| 1 | //! An MPSC channel whose receiving end is an event source |
| 2 | //! |
| 3 | //! Create a channel using [`channel()`](channel), which returns a |
| 4 | //! [`Sender`] that can be cloned and sent accross threads if `T: Send`, |
| 5 | //! and a [`Channel`] that can be inserted into an [`EventLoop`](crate::EventLoop). |
| 6 | //! It will generate one event per message. |
| 7 | //! |
| 8 | //! A synchronous version of the channel is provided by [`sync_channel`], in which |
| 9 | //! the [`SyncSender`] will block when the channel is full. |
| 10 | |
| 11 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/; |
| 12 | |
| 13 | use craterust-analyzer cargo calloop 0.10.5 crate/::{EventSourcerust-analyzer cargo calloop 0.10.5 sources/EventSource#, Pollrust-analyzer cargo calloop 0.10.5 sys/Poll#, PostActionrust-analyzer cargo calloop 0.10.5 sources/PostAction#, Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness#, Tokenrust-analyzer cargo calloop 0.10.5 sys/Token#, TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#}; |
| 14 | |
| 15 | use superrust-analyzer cargo calloop 0.10.5 sources/::pingrust-analyzer cargo calloop 0.10.5 sources/ping/::{make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping()., Pingrust-analyzer cargo calloop 0.10.5 sources/ping/Ping#, PingErrorrust-analyzer cargo calloop 0.10.5 sources/ping/PingError#, PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource#}; |
| 16 | |
| 17 | /// The events generated by the channel event source |
| 18 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 19 | pub enum Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#<T> { |
| 20 | /// A message was received and is bundled here |
| 21 | Msgrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Msg#(T), |
| 22 | /// The channel was closed |
| 23 | /// |
| 24 | /// This means all the `Sender`s associated with this channel |
| 25 | /// have been dropped, no more messages will ever be received. |
| 26 | Closedrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Closed#, |
| 27 | } |
| 28 | |
| 29 | /// The sender end of a channel |
| 30 | /// |
| 31 | /// It can be cloned and sent accross threads (if `T` is). |
| 32 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 33 | pub struct Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#<T> { |
| 34 | senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#sender.: mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::Senderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Sender#<T>, |
| 35 | pingrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#ping.: Pingrust-analyzer cargo calloop 0.10.5 sources/ping/Ping#, |
| 36 | } |
| 37 | |
| 38 | impl<T> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#<T> { |
| 39 | #[cfg_attr(coverage, no_coverage)] |
| 40 | fn clonerust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Sender<T>`][Clone]clone().(&self) -> Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#<T> { |
| 41 | Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender# { |
| 42 | senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#sender.: self.senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#sender..clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`][Clone]clone().(), |
| 43 | pingrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#ping.: self.pingrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#ping..clonerust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping][Clone]clone().(), |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl<T> Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#<T> { |
| 49 | /// Send a message to the channel |
| 50 | /// |
| 51 | /// This will wake the event loop and deliver an `Event::Msg` to |
| 52 | /// it containing the provided value. |
| 53 | pub fn sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Sender<T>`]send().(&self, t: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::SendErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/SendError#<T>> { |
| 54 | self.senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#sender..sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Sender<T>`]send().(t).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|()| self.pingrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#ping..pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().()) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl<T> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#<T> { |
| 59 | fn droprust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Sender<T>`][Drop]drop().(&mut self) { |
| 60 | // ping on drop, to notify about channel closure |
| 61 | self.pingrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#ping..pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// The sender end of a synchronous channel |
| 66 | /// |
| 67 | /// It can be cloned and sent accross threads (if `T` is). |
| 68 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 69 | pub struct SyncSenderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#<T> { |
| 70 | senderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#sender.: mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::SyncSenderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/SyncSender#<T>, |
| 71 | pingrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#ping.: Pingrust-analyzer cargo calloop 0.10.5 sources/ping/Ping#, |
| 72 | } |
| 73 | |
| 74 | impl<T> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for SyncSenderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#<T> { |
| 75 | #[cfg_attr(coverage, no_coverage)] |
| 76 | fn clonerust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`][Clone]clone().(&self) -> SyncSenderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#<T> { |
| 77 | SyncSenderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender# { |
| 78 | senderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#sender.: self.senderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#sender..clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`][Clone]clone().(), |
| 79 | pingrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#ping.: self.pingrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#ping..clonerust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping][Clone]clone().(), |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl<T> SyncSenderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#<T> { |
| 85 | /// Send a message to the synchronous channel |
| 86 | /// |
| 87 | /// This will wake the event loop and deliver an `Event::Msg` to |
| 88 | /// it containing the provided value. If the channel is full, this |
| 89 | /// function will block until the event loop empties it and it can |
| 90 | /// deliver the message. |
| 91 | /// |
| 92 | /// Due to the blocking behavior, this method should not be used on the |
| 93 | /// same thread as the one running the event loop, as it could cause deadlocks. |
| 94 | pub fn sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`]send().(&self, t: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::SendErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/SendError#<T>> { |
| 95 | let ret = self.try_sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`]try_send().(t); |
| 96 | match ret { |
| 97 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 98 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::TrySendErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TrySendError#::Fullrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TrySendError#Full#(t)) => self.senderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#sender..sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`]send().(t).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|()| self.pingrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#ping..pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().()), |
| 99 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::TrySendErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TrySendError#::Disconnectedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TrySendError#Disconnected#(t)) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::SendErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/SendError#(t)), |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Send a message to the synchronous channel |
| 104 | /// |
| 105 | /// This will wake the event loop and deliver an `Event::Msg` to |
| 106 | /// it containing the provided value. If the channel is full, this |
| 107 | /// function will return an error, but the event loop will still be |
| 108 | /// signaled for readiness. |
| 109 | pub fn try_sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`]try_send().(&self, t: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::TrySendErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TrySendError#<T>> { |
| 110 | let ret = self.senderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#sender..try_sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`]try_send().(t); |
| 111 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::TrySendErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TrySendError#::Fullrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TrySendError#Full#(_)) = ret { |
| 112 | self.pingrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#ping..pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().(); |
| 113 | } |
| 114 | ret |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// The receiving end of the channel |
| 119 | /// |
| 120 | /// This is the event source to be inserted into your `EventLoop`. |
| 121 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 122 | pub struct Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#<T> { |
| 123 | receiverrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#receiver.: mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::Receiverrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Receiver#<T>, |
| 124 | sourcerust-analyzer cargo calloop 0.10.5 sources/channel/Channel#source.: PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource#, |
| 125 | } |
| 126 | |
| 127 | // This impl is safe because the Channel is only able to move around threads |
| 128 | // when it is not inserted into an event loop. (Otherwise it is stuck into |
| 129 | // a Source<_> and the internals of calloop, which are not Send). |
| 130 | // At this point, the Arc<Receiver> has a count of 1, and it is obviously |
| 131 | // safe to Send between threads. |
| 132 | unsafe impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#> Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# for Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#<T> {} |
| 133 | |
| 134 | impl<T> Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#<T> { |
| 135 | /// Proxy for [`mpsc::Receiver::recv`] to manually poll events. |
| 136 | /// |
| 137 | /// *Note*: Normally you would want to use the `Channel` by inserting |
| 138 | /// it into an event loop instead. Use this for example to immediately |
| 139 | /// dispatch pending events after creation. |
| 140 | pub fn recvrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`]recv().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::RecvErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/RecvError#> { |
| 141 | self.receiverrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#receiver..recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]recv().() |
| 142 | } |
| 143 | |
| 144 | /// Proxy for [`mpsc::Receiver::try_recv`] to manually poll events. |
| 145 | /// |
| 146 | /// *Note*: Normally you would want to use the `Channel` by inserting |
| 147 | /// it into an event loop instead. Use this for example to immediately |
| 148 | /// dispatch pending events after creation. |
| 149 | pub fn try_recvrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`]try_recv().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::TryRecvErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TryRecvError#> { |
| 150 | self.receiverrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#receiver..try_recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]try_recv().() |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /// Create a new asynchronous channel |
| 155 | pub fn channelrust-analyzer cargo calloop 0.10.5 sources/channel/channel().<T>() -> (Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#<T>, Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#<T>) { |
| 156 | let (sender, receiver) = mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::channelrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/channel().(); |
| 157 | let (ping, source) = make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to create a Ping."); |
| 158 | (Senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender# { senderrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#sender., pingrust-analyzer cargo calloop 0.10.5 sources/channel/Sender#ping. }, Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel# { receiverrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#receiver., sourcerust-analyzer cargo calloop 0.10.5 sources/channel/Channel#source. }) |
| 159 | } |
| 160 | |
| 161 | /// Create a new synchronous, bounded channel |
| 162 | pub fn sync_channelrust-analyzer cargo calloop 0.10.5 sources/channel/sync_channel().<T>(bound: usize) -> (SyncSenderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#<T>, Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#<T>) { |
| 163 | let (sender, receiver) = mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::sync_channelrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/sync_channel().(bound); |
| 164 | let (ping, source) = make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to create a Ping."); |
| 165 | (SyncSenderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender# { senderrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#sender., pingrust-analyzer cargo calloop 0.10.5 sources/channel/SyncSender#ping. }, Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel# { receiverrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#receiver., sourcerust-analyzer cargo calloop 0.10.5 sources/channel/Channel#source. }) |
| 166 | } |
| 167 | |
| 168 | impl<T> EventSourcerust-analyzer cargo calloop 0.10.5 sources/EventSource# for Channelrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#<T> { |
| 169 | type Eventrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]Event# = Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#<T>; |
| 170 | type Metadatarust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]Metadata# = (); |
| 171 | type Retrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]Ret# = (); |
| 172 | type Errorrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]Error# = ChannelErrorrust-analyzer cargo calloop 0.10.5 sources/channel/ChannelError#; |
| 173 | |
| 174 | fn process_eventsrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]process_events().<C>( |
| 175 | &mut self, |
| 176 | readiness: Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness#, |
| 177 | token: Tokenrust-analyzer cargo calloop 0.10.5 sys/Token#, |
| 178 | mut callback: C, |
| 179 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<PostActionrust-analyzer cargo calloop 0.10.5 sources/PostAction#, Selfrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]::Errorrust-analyzer cargo calloop 0.10.5 sources/EventSource#Error#> |
| 180 | where |
| 181 | C: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(Selfrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]::Eventrust-analyzer cargo calloop 0.10.5 sources/EventSource#Event#, &mut Selfrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]::Metadatarust-analyzer cargo calloop 0.10.5 sources/EventSource#Metadata#) -> Selfrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]::Retrust-analyzer cargo calloop 0.10.5 sources/EventSource#Ret#, |
| 182 | { |
| 183 | let receiver = &self.receiverrust-analyzer cargo calloop 0.10.5 sources/channel/Channel#receiver.; |
| 184 | self.sourcerust-analyzer cargo calloop 0.10.5 sources/channel/Channel#source. |
| 185 | .process_eventsrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[PingSource][EventSource]process_events().(readiness, token, |(), &mut ()| loop { |
| 186 | match receiver.try_recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]try_recv().() { |
| 187 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) => callback(Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#::Msgrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Msg#(val), &mut ()), |
| 188 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::TryRecvErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TryRecvError#::Emptyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TryRecvError#Empty#) => break, |
| 189 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::TryRecvErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TryRecvError#::Disconnectedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/TryRecvError#Disconnected#) => { |
| 190 | callback(Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#::Closedrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Closed#, &mut ()); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | }) |
| 195 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(ChannelErrorrust-analyzer cargo calloop 0.10.5 sources/channel/ChannelError#) |
| 196 | } |
| 197 | |
| 198 | fn registerrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]register().(&mut self, poll: &mut Pollrust-analyzer cargo calloop 0.10.5 sys/Poll#, token_factory: &mut TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> { |
| 199 | self.sourcerust-analyzer cargo calloop 0.10.5 sources/channel/Channel#source..registerrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[PingSource][EventSource]register().(poll, token_factory) |
| 200 | } |
| 201 | |
| 202 | fn reregisterrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]reregister().( |
| 203 | &mut self, |
| 204 | poll: &mut Pollrust-analyzer cargo calloop 0.10.5 sys/Poll#, |
| 205 | token_factory: &mut TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#, |
| 206 | ) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> { |
| 207 | self.sourcerust-analyzer cargo calloop 0.10.5 sources/channel/Channel#source..reregisterrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[PingSource][EventSource]reregister().(poll, token_factory) |
| 208 | } |
| 209 | |
| 210 | fn unregisterrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Channel<T>`][EventSource]unregister().(&mut self, poll: &mut Pollrust-analyzer cargo calloop 0.10.5 sys/Poll#) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> { |
| 211 | self.sourcerust-analyzer cargo calloop 0.10.5 sources/channel/Channel#source..unregisterrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[PingSource][EventSource]unregister().(poll) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /// An error arising from processing events for a channel. |
| 216 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(thiserrorrust-analyzer cargo thiserror 1.0.7 crate/::Errorrust-analyzer cargo thiserror-impl 1.0.7 Error:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 217 | #[error(transparent)] |
| 218 | pub struct ChannelErrorrust-analyzer cargo calloop 0.10.5 sources/channel/ChannelError#(PingErrorrust-analyzer cargo calloop 0.10.5 sources/ping/PingError#); |
| 219 | |
| 220 | #[cfg(test)] |
| 221 | mod testsrust-analyzer cargo calloop 0.10.5 sources/channel/tests/ { |
| 222 | use superrust-analyzer cargo calloop 0.10.5 sources/channel/::*; |
| 223 | |
| 224 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 225 | fn basic_channelrust-analyzer cargo calloop 0.10.5 sources/channel/tests/basic_channel().() { |
| 226 | let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::try_newrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]try_new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 227 | |
| 228 | let handle = event_loop.handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().(); |
| 229 | |
| 230 | let (tx, rx) = channelrust-analyzer cargo calloop 0.10.5 sources/channel/channel().::<()>(); |
| 231 | |
| 232 | // (got_msg, got_closed) |
| 233 | let mut got = (false, false); |
| 234 | |
| 235 | let _channel_token = handle |
| 236 | .insert_sourcerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]insert_source().(rx, move |evt, &mut (), got: &mut (bool, bool)| match evt { |
| 237 | Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#::Msgrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Msg#(()) => { |
| 238 | got.0 = true; |
| 239 | } |
| 240 | Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#::Closedrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Closed# => { |
| 241 | got.1 = true; |
| 242 | } |
| 243 | }) |
| 244 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 245 | |
| 246 | // nothing is sent, nothing is received |
| 247 | event_loop |
| 248 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.), &mut got) |
| 249 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 250 | |
| 251 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(got, (false, false)); |
| 252 | |
| 253 | // a message is send |
| 254 | tx.sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`Sender<T>`]send().(()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 255 | event_loop |
| 256 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.), &mut got) |
| 257 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 258 | |
| 259 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(got, (true, false)); |
| 260 | |
| 261 | // the sender is dropped |
| 262 | ::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(tx); |
| 263 | event_loop |
| 264 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.), &mut got) |
| 265 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 266 | |
| 267 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(got, (true, true)); |
| 268 | } |
| 269 | |
| 270 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 271 | fn basic_sync_channelrust-analyzer cargo calloop 0.10.5 sources/channel/tests/basic_sync_channel().() { |
| 272 | let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::try_newrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]try_new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 273 | |
| 274 | let handle = event_loop.handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().(); |
| 275 | |
| 276 | let (tx, rx) = sync_channelrust-analyzer cargo calloop 0.10.5 sources/channel/sync_channel().::<()>(2); |
| 277 | |
| 278 | let mut received = (0, false); |
| 279 | |
| 280 | let _channel_token = handle |
| 281 | .insert_sourcerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]insert_source().( |
| 282 | rx, |
| 283 | move |evt, &mut (), received: &mut (u32, bool)| match evt { |
| 284 | Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#::Msgrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Msg#(()) => { |
| 285 | received.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().1; |
| 286 | } |
| 287 | Eventrust-analyzer cargo calloop 0.10.5 sources/channel/Event#::Closedrust-analyzer cargo calloop 0.10.5 sources/channel/Event#Closed# => { |
| 288 | received.1 = true; |
| 289 | } |
| 290 | }, |
| 291 | ) |
| 292 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 293 | |
| 294 | // nothing is sent, nothing is received |
| 295 | event_loop |
| 296 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.), &mut received) |
| 297 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 298 | |
| 299 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(received.0, 0); |
| 300 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().received.1); |
| 301 | |
| 302 | // fill the channel |
| 303 | tx.sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`]send().(()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 304 | tx.sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`]send().(()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 305 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(tx.try_sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`]try_send().(()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 306 | |
| 307 | // empty it |
| 308 | event_loop |
| 309 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.), &mut received) |
| 310 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 311 | |
| 312 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(received.0, 2); |
| 313 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().received.1); |
| 314 | |
| 315 | // send a final message and drop the sender |
| 316 | tx.sendrust-analyzer cargo calloop 0.10.5 sources/channel/impl#[`SyncSender<T>`]send().(()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 317 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(tx); |
| 318 | |
| 319 | // final read of the channel |
| 320 | event_loop |
| 321 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.), &mut received) |
| 322 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 323 | |
| 324 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(received.0, 3); |
| 325 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(received.1); |
| 326 | } |
| 327 | } |