Debian Codegraph @

src/macros.rs

raw symbols SCIP
1 //! Macros for helping with common operations in Calloop.
2
3 /// Register a set of event sources. Effectively calls
4 /// [`EventSource::register()`] for all the sources provided.
5 ///
6 /// Usage:
7 ///
8 /// ```none,actually-rust-but-see-https://github.com/rust-lang/rust/issues/63193
9 /// calloop::batch_register!(
10 /// poll, token_factory,
11 /// self.source_one,
12 /// self.source_two,
13 /// self.source_three,
14 /// self.source_four,
15 /// )
16 /// ```
17 ///
18 /// Note that there is no scope for customisation; if you need to do special
19 /// things with a particular source, you'll need to leave it off the list. Also
20 /// note that this only does try-or-early-return error handling in the order
21 /// that you list the sources; if you need anything else, don't use this macro.
22 ///
23 /// [`EventSource::register()`]: crate::EventSource::register()
24 #[macro_export]
25 macro_rules! batch_registerrust-analyzer cargo calloop 0.10.5 macros/batch_register! {
26 ($poll:ident, $token_fac:ident, $( $source:expr ),* $(,)?) => {
27 {
28 $(
29 $source.register($poll, $token_fac)?;
30 )*
31 $crate::Result::<_>::Ok(())
32 }
33 };
34 }
35
36 /// Reregister a set of event sources. Effectively calls
37 /// [`EventSource::reregister()`] for all the sources provided.
38 ///
39 /// Usage:
40 ///
41 /// ```none,actually-rust-but-see-https://github.com/rust-lang/rust/issues/63193
42 /// calloop::batch_reregister!(
43 /// poll, token_factory,
44 /// self.source_one,
45 /// self.source_two,
46 /// self.source_three,
47 /// self.source_four,
48 /// )
49 /// ```
50 ///
51 /// Note that there is no scope for customisation; if you need to do special
52 /// things with a particular source, you'll need to leave it off the list. Also
53 /// note that this only does try-or-early-return error handling in the order
54 /// that you list the sources; if you need anything else, don't use this macro.
55 ///
56 /// [`EventSource::reregister()`]: crate::EventSource::reregister()
57 #[macro_export]
58 macro_rules! batch_reregisterrust-analyzer cargo calloop 0.10.5 macros/batch_reregister! {
59 ($poll:ident, $token_fac:ident, $( $source:expr ),* $(,)?) => {
60 {
61 $(
62 $source.reregister($poll, $token_fac)?;
63 )*
64 $crate::Result::<_>::Ok(())
65 }
66 };
67 }
68
69 /// Unregister a set of event sources. Effectively calls
70 /// [`EventSource::unregister()`] for all the sources provided.
71 ///
72 /// Usage:
73 ///
74 /// ```none,actually-rust-but-see-https://github.com/rust-lang/rust/issues/63193
75 /// calloop::batch_unregister!(
76 /// poll,
77 /// self.source_one,
78 /// self.source_two,
79 /// self.source_three,
80 /// self.source_four,
81 /// )
82 /// ```
83 ///
84 /// Note that there is no scope for customisation; if you need to do special
85 /// things with a particular source, you'll need to leave it off the list. Also
86 /// note that this only does try-or-early-return error handling in the order
87 /// that you list the sources; if you need anything else, don't use this macro.
88 ///
89 /// [`EventSource::unregister()`]: crate::EventSource::unregister()
90 #[macro_export]
91 macro_rules! batch_unregisterrust-analyzer cargo calloop 0.10.5 macros/batch_unregister! {
92 ($poll:ident, $( $source:expr ),* $(,)?) => {
93 {
94 $(
95 $source.unregister($poll)?;
96 )*
97 $crate::Result::<_>::Ok(())
98 }
99 };
100 }
101
102 #[cfg(test)]
103 mod testsrust-analyzer cargo calloop 0.10.5 macros/tests/ {
104 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#;
105
106 use craterust-analyzer cargo calloop 0.10.5 crate/::{
107 pingrust-analyzer cargo calloop 0.10.5 sources/ping/::{make_pingrust-analyzer cargo calloop 0.10.5 sources/ping/make_ping()., PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource#},
108 EventSourcerust-analyzer cargo calloop 0.10.5 sources/EventSource#, PostActionrust-analyzer cargo calloop 0.10.5 sources/PostAction#,
109 };
110
111 struct BatchSourcerust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource# {
112 ping0rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping0.: PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource#,
113 ping1rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping1.: PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource#,
114 ping2rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping2.: PingSourcerust-analyzer cargo calloop 0.10.5 sources/ping/PingSource#,
115 }
116
117 impl EventSourcerust-analyzer cargo calloop 0.10.5 sources/EventSource# for BatchSourcerust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource# {
118 type Eventrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]Event# = usize;
119 type Metadatarust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]Metadata# = ();
120 type Retrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]Ret# = ();
121 type Errorrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]Error# = 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#>;
122
123 fn process_eventsrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]process_events().<F>(
124 &mut self,
125 readiness: craterust-analyzer cargo calloop 0.10.5 crate/::Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness#,
126 token: craterust-analyzer cargo calloop 0.10.5 crate/::Tokenrust-analyzer cargo calloop 0.10.5 sys/Token#,
127 mut callback: F,
128 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<craterust-analyzer cargo calloop 0.10.5 crate/::PostActionrust-analyzer cargo calloop 0.10.5 sources/PostAction#, Selfrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]::Errorrust-analyzer cargo calloop 0.10.5 sources/EventSource#Error#>
129 where
130 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(Selfrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]::Eventrust-analyzer cargo calloop 0.10.5 sources/EventSource#Event#, &mut Selfrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]::Metadatarust-analyzer cargo calloop 0.10.5 sources/EventSource#Metadata#) -> Selfrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]::Retrust-analyzer cargo calloop 0.10.5 sources/EventSource#Ret#,
131 {
132 self.ping0rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping0.
133 .process_eventsrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[PingSource][EventSource]process_events().(readiness, token, |_, m| callback(0, m))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
134 self.ping1rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping1.
135 .process_eventsrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[PingSource][EventSource]process_events().(readiness, token, |_, m| callback(1, m))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
136 self.ping2rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping2.
137 .process_eventsrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[PingSource][EventSource]process_events().(readiness, token, |_, m| callback(2, m))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
138 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PostActionrust-analyzer cargo calloop 0.10.5 sources/PostAction#::Continuerust-analyzer cargo calloop 0.10.5 sources/PostAction#Continue#)
139 }
140
141 fn registerrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]register().(
142 &mut self,
143 poll: &mut craterust-analyzer cargo calloop 0.10.5 crate/::Pollrust-analyzer cargo calloop 0.10.5 sys/Poll#,
144 token_factory: &mut craterust-analyzer cargo calloop 0.10.5 crate/::TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#,
145 ) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> {
146 craterust-analyzer cargo calloop 0.10.5 crate/::batch_registerrust-analyzer cargo calloop 0.10.5 macros/batch_register!!(poll, token_factory, self.ping0rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping0., self.ping1rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping1., self.ping2rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping2.)
147 }
148
149 fn reregisterrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]reregister().(
150 &mut self,
151 poll: &mut craterust-analyzer cargo calloop 0.10.5 crate/::Pollrust-analyzer cargo calloop 0.10.5 sys/Poll#,
152 token_factory: &mut craterust-analyzer cargo calloop 0.10.5 crate/::TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#,
153 ) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> {
154 craterust-analyzer cargo calloop 0.10.5 crate/::batch_reregisterrust-analyzer cargo calloop 0.10.5 macros/batch_reregister!!(poll, token_factory, self.ping0rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping0., self.ping1rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping1., self.ping2rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping2.)
155 }
156
157 fn unregisterrust-analyzer cargo calloop 0.10.5 macros/tests/impl#[BatchSource][EventSource]unregister().(&mut self, poll: &mut craterust-analyzer cargo calloop 0.10.5 crate/::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#<()> {
158 craterust-analyzer cargo calloop 0.10.5 crate/::batch_unregisterrust-analyzer cargo calloop 0.10.5 macros/batch_unregister!!(poll, self.ping0rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping0., self.ping1rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping1., self.ping2rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping2.)
159 }
160 }
161
162 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
163 fn test_batch_operationsrust-analyzer cargo calloop 0.10.5 macros/tests/test_batch_operations().() {
164 let mut fired = [false; 3];
165
166 let (send0, ping0) = 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().();
167 let (send1, ping1) = 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().();
168 let (send2, ping2) = 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().();
169
170 let top = BatchSourcerust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource# {
171 ping0rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping0.,
172 ping1rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping1.,
173 ping2rust-analyzer cargo calloop 0.10.5 macros/tests/BatchSource#ping2.,
174 };
175
176 let mut event_loop = craterust-analyzer cargo calloop 0.10.5 crate/::EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::<[bool; 3]>::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().();
177 let handle = event_loop.handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().();
178
179 let token = handle
180 .insert_sourcerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]insert_source().(top, |idx, _, fired| {
181 fired[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = true;
182 })
183 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
184
185 send0.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
186 send1.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
187 send2.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
188
189 event_loop
190 .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#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]new().(0, 0), &mut fired)
191 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
192
193 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fired, [true; 3]);
194
195 fired = [false; 3];
196
197 handle.updaterust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]update().(&token).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
198
199 send0.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
200 send1.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
201 send2.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
202
203 event_loop
204 .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#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]new().(0, 0), &mut fired)
205 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
206
207 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fired, [true; 3]);
208
209 fired = [false; 3];
210
211 handle.removerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]remove().(token);
212
213 send0.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
214 send1.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
215 send2.pingrust-analyzer cargo calloop 0.10.5 sources/ping/eventfd/impl#[Ping]ping().();
216
217 event_loop
218 .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#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]new().(0, 0), &mut fired)
219 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
220
221 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fired, [false; 3]);
222 }
223 }