| 1 | //! Ping to the event loop |
| 2 | //! |
| 3 | //! This is an event source that just produces `()` events whevener the associated |
| 4 | //! [`Ping::ping`](Ping#method.ping) method is called. If the event source is pinged multiple times |
| 5 | //! between a single dispatching, it'll only generate one event. |
| 6 | //! |
| 7 | //! This event source is a simple way of waking up the event loop from an other part of your program |
| 8 | //! (and is what backs the [`LoopSignal`](crate::LoopSignal)). It can also be used as a building |
| 9 | //! block to construct event sources whose source of event is not file descriptor, but rather an |
| 10 | //! userspace source (like an other thread). |
| 11 | |
| 12 | use nixrust-analyzer cargo nix 0.30.1 crate/::unistdrust-analyzer cargo nix 0.30.1 unistd/::closerust-analyzer cargo nix 0.30.1 unistd/close().; |
| 13 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/io/::RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#; |
| 14 | |
| 15 | // The ping source has platform-dependent implementations provided by modules |
| 16 | // under this one. These modules should expose: |
| 17 | // - a make_ping() function |
| 18 | // - a Ping type |
| 19 | // - a PingSource type |
| 20 | // |
| 21 | // See eg. the pipe implementation for these items' specific requirements. |
| 22 | |
| 23 | #[cfg(target_os = "linux")] |
| 24 | mod eventfdrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/; |
| 25 | #[cfg(target_os = "linux")] |
| 26 | use eventfdrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/ as platformrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/; |
| 27 | |
| 28 | #[cfg(not(target_os = "linux"))] |
| 29 | mod pipe; |
| 30 | #[cfg(not(target_os = "linux"))] |
| 31 | use pipe as platform; |
| 32 | |
| 33 | /// Create a new ping event source |
| 34 | /// |
| 35 | /// you are given a [`Ping`] instance, which can be cloned and used to ping the |
| 36 | /// event loop, and a [`PingSource`], which you can insert in your event loop to |
| 37 | /// receive the pings. |
| 38 | pub fn make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<(Pingrust-analyzer cargo calloop 0.10.5 sources/ping/Ping#, PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource#)> { |
| 39 | platformrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/::make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/make_ping().() |
| 40 | } |
| 41 | |
| 42 | /// The ping event source |
| 43 | /// |
| 44 | /// You can insert it in your event loop to receive pings. |
| 45 | /// |
| 46 | /// If you use it directly, it will automatically remove itself from the event loop |
| 47 | /// once all [`Ping`] instances are dropped. |
| 48 | pub type Pingrust-analyzer cargo calloop 0.10.5 sources/ping/Ping# = platformrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/::Pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/Ping#; |
| 49 | |
| 50 | /// The Ping handle |
| 51 | /// |
| 52 | /// This handle can be cloned and sent accross threads. It can be used to |
| 53 | /// send pings to the `PingSource`. |
| 54 | pub type PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource# = platformrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/::PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/PingSource#; |
| 55 | |
| 56 | /// An error arising from processing events for a ping. |
| 57 | #[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:)] |
| 58 | #[error(transparent)] |
| 59 | pub struct PingErrorrust-analyzer cargo calloop 0.10.5 sources/ping/PingError#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>); |
| 60 | |
| 61 | #[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:)] |
| 62 | struct CloseOnDroprust-analyzer cargo calloop 0.10.5 sources/ping/CloseOnDrop#(RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#); |
| 63 | |
| 64 | impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for CloseOnDroprust-analyzer cargo calloop 0.10.5 sources/ping/CloseOnDrop# { |
| 65 | fn droprust-analyzer cargo calloop 0.10.5 sources/ping/impl#[CloseOnDrop][Drop]drop().(&mut self) { |
| 66 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) = closerust-analyzer cargo nix 0.30.1 unistd/close().(self.0rust-analyzer cargo calloop 0.10.5 sources/ping/CloseOnDrop#0.) { |
| 67 | logrust-analyzer cargo log 0.4.17 crate/::warnrust-analyzer cargo log 0.4.17 macros/warn!!("[calloop] Failed to close ping fd: {:?}", e); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[cfg(test)] |
| 73 | mod testsrust-analyzer cargo calloop 0.10.5 sources/ping/tests/ { |
| 74 | use craterust-analyzer cargo calloop 0.10.5 crate/::transientrust-analyzer cargo calloop 0.10.5 sources/transient/::TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#; |
| 75 | use 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#; |
| 76 | |
| 77 | use superrust-analyzer cargo calloop 0.10.5 sources/ping/::*; |
| 78 | |
| 79 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 80 | fn pingrust-analyzer cargo calloop 0.10.5 sources/ping/tests/ping().() { |
| 81 | let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::<bool>::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().(); |
| 82 | |
| 83 | let (ping, source) = make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 84 | |
| 85 | event_loop |
| 86 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 87 | .insert_sourcerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]insert_source().(source, |(), &mut (), dispatched| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().dispatched = true) |
| 88 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 89 | |
| 90 | ping.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().(); |
| 91 | |
| 92 | let mut dispatched = false; |
| 93 | event_loop |
| 94 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 95 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 96 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(dispatched); |
| 97 | |
| 98 | // Ping has been drained an no longer generates events |
| 99 | let mut dispatched = false; |
| 100 | event_loop |
| 101 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 102 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 103 | 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().dispatched); |
| 104 | } |
| 105 | |
| 106 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 107 | fn ping_closedrust-analyzer cargo calloop 0.10.5 sources/ping/tests/ping_closed().() { |
| 108 | let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::<bool>::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().(); |
| 109 | |
| 110 | let (_, source) = make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 111 | event_loop |
| 112 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 113 | .insert_sourcerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]insert_source().(source, |(), &mut (), dispatched| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().dispatched = true) |
| 114 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 115 | |
| 116 | let mut dispatched = false; |
| 117 | |
| 118 | // If the sender is closed from the start, the ping should first trigger |
| 119 | // once, disabling itself but not invoking the callback |
| 120 | event_loop |
| 121 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 122 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 123 | 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().dispatched); |
| 124 | |
| 125 | // Then it should not trigger any more, so this dispatch should wait the whole 100ms |
| 126 | let now = 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/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(); |
| 127 | event_loop |
| 128 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(100), &mut dispatched) |
| 129 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 130 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(now.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().() >= 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#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(100)); |
| 131 | } |
| 132 | |
| 133 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 134 | fn ping_removedrust-analyzer cargo calloop 0.10.5 sources/ping/tests/ping_removed().() { |
| 135 | // This keeps track of whether the event fired. |
| 136 | let mut dispatched = false; |
| 137 | |
| 138 | let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::<bool>::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().(); |
| 139 | |
| 140 | let (sender, source) = make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 141 | let wrapper = TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::fromrust-analyzer cargo calloop 0.10.5 sources/transient/impl#[`TransientSource<T>`][`From<T>`]from().(source); |
| 142 | |
| 143 | // Check that the source starts off in the wrapper. |
| 144 | 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().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(wrapper, TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::Nonerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#None#)); |
| 145 | |
| 146 | // Put the source in the loop. |
| 147 | |
| 148 | let dispatcher = |
| 149 | craterust-analyzer cargo calloop 0.10.5 crate/::Dispatcherrust-analyzer cargo calloop 0.10.5 sources/Dispatcher#::newrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]new().(wrapper, |(), &mut (), dispatched| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().dispatched = true); |
| 150 | |
| 151 | let token = event_loop |
| 152 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 153 | .register_dispatcherrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]register_dispatcher().(dispatcher.clonerust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`][Clone]clone().()) |
| 154 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 155 | |
| 156 | // Drop the sender and check that it's actually removed. |
| 157 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(sender); |
| 158 | |
| 159 | // There should be no event, but the loop still needs to wake up to |
| 160 | // process the close event (just like in the ping_closed() test). |
| 161 | event_loop |
| 162 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 163 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 164 | 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().dispatched); |
| 165 | |
| 166 | // Pull the source wrapper out. |
| 167 | |
| 168 | event_loop.handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().().removerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]remove().(token); |
| 169 | let wrapper = dispatcher.into_source_innerrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]into_source_inner().(); |
| 170 | |
| 171 | // Check that the inner source is now gone. |
| 172 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(wrapper, TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::Nonerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#None#)); |
| 173 | } |
| 174 | |
| 175 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 176 | fn ping_fired_and_removedrust-analyzer cargo calloop 0.10.5 sources/ping/tests/ping_fired_and_removed().() { |
| 177 | // This is like ping_removed() with the single difference that we fire a |
| 178 | // ping and drop it between two successive dispatches of the loop. |
| 179 | |
| 180 | // This keeps track of whether the event fired. |
| 181 | let mut dispatched = false; |
| 182 | |
| 183 | let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::<bool>::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().(); |
| 184 | |
| 185 | let (sender, source) = make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 186 | let wrapper = TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::fromrust-analyzer cargo calloop 0.10.5 sources/transient/impl#[`TransientSource<T>`][`From<T>`]from().(source); |
| 187 | |
| 188 | // Check that the source starts off in the wrapper. |
| 189 | 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().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(wrapper, TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::Nonerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#None#)); |
| 190 | |
| 191 | // Put the source in the loop. |
| 192 | |
| 193 | let dispatcher = |
| 194 | craterust-analyzer cargo calloop 0.10.5 crate/::Dispatcherrust-analyzer cargo calloop 0.10.5 sources/Dispatcher#::newrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]new().(wrapper, |(), &mut (), dispatched| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().dispatched = true); |
| 195 | |
| 196 | let token = event_loop |
| 197 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 198 | .register_dispatcherrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]register_dispatcher().(dispatcher.clonerust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`][Clone]clone().()) |
| 199 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 200 | |
| 201 | // Send a ping AND drop the sender and check that it's actually removed. |
| 202 | sender.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().(); |
| 203 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(sender); |
| 204 | |
| 205 | // There should be an event, but the source should be removed from the |
| 206 | // loop immediately after. |
| 207 | event_loop |
| 208 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 209 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 210 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(dispatched); |
| 211 | |
| 212 | // Pull the source wrapper out. |
| 213 | |
| 214 | event_loop.handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().().removerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]remove().(token); |
| 215 | let wrapper = dispatcher.into_source_innerrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]into_source_inner().(); |
| 216 | |
| 217 | // Check that the inner source is now gone. |
| 218 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(wrapper, TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::Nonerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#None#)); |
| 219 | } |
| 220 | |
| 221 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 222 | fn ping_multiple_sendersrust-analyzer cargo calloop 0.10.5 sources/ping/tests/ping_multiple_senders().() { |
| 223 | // This is like ping_removed() but for testing the behaviour of multiple |
| 224 | // senders. |
| 225 | |
| 226 | // This keeps track of whether the event fired. |
| 227 | let mut dispatched = false; |
| 228 | |
| 229 | let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::<bool>::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().(); |
| 230 | |
| 231 | let (sender0, source) = make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 232 | let wrapper = TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::fromrust-analyzer cargo calloop 0.10.5 sources/transient/impl#[`TransientSource<T>`][`From<T>`]from().(source); |
| 233 | let sender1 = sender0.clonerust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping][Clone]clone().(); |
| 234 | let sender2 = sender1.clonerust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping][Clone]clone().(); |
| 235 | |
| 236 | // Check that the source starts off in the wrapper. |
| 237 | 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().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(wrapper, TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::Nonerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#None#)); |
| 238 | |
| 239 | // Put the source in the loop. |
| 240 | |
| 241 | let dispatcher = |
| 242 | craterust-analyzer cargo calloop 0.10.5 crate/::Dispatcherrust-analyzer cargo calloop 0.10.5 sources/Dispatcher#::newrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]new().(wrapper, |(), &mut (), dispatched| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().dispatched = true); |
| 243 | |
| 244 | let token = event_loop |
| 245 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 246 | .register_dispatcherrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]register_dispatcher().(dispatcher.clonerust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`][Clone]clone().()) |
| 247 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 248 | |
| 249 | // Send a ping AND drop the sender and check that it's actually removed. |
| 250 | sender0.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().(); |
| 251 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(sender0); |
| 252 | |
| 253 | // There should be an event, and the source should remain in the loop. |
| 254 | event_loop |
| 255 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 256 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 257 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(dispatched); |
| 258 | |
| 259 | // Now test that the clones still work. Drop after the dispatch loop |
| 260 | // instead of before, this time. |
| 261 | dispatched = false; |
| 262 | |
| 263 | sender1.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().(); |
| 264 | |
| 265 | event_loop |
| 266 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 267 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 268 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(dispatched); |
| 269 | |
| 270 | // Finally, drop all of them without sending anything. |
| 271 | |
| 272 | dispatched = false; |
| 273 | |
| 274 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(sender1); |
| 275 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(sender2); |
| 276 | |
| 277 | event_loop |
| 278 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(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 dispatched) |
| 279 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 280 | 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().dispatched); |
| 281 | |
| 282 | // Pull the source wrapper out. |
| 283 | |
| 284 | event_loop.handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().().removerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]remove().(token); |
| 285 | let wrapper = dispatcher.into_source_innerrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]into_source_inner().(); |
| 286 | |
| 287 | // Check that the inner source is now gone. |
| 288 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(wrapper, TransientSourcerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#::Nonerust-analyzer cargo calloop 0.10.5 sources/transient/TransientSource#None#)); |
| 289 | } |
| 290 | } |