Debian Codegraph @

third_party/rust/neqo-common/src/event.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::marker::PhantomData;
8
9 /// An event provider is able to generate a stream of events.
10 pub trait Provider {
11 type Event;
12
13 /// Get the next event.
14 #[must_use]
15 fn next_event(&mut self) -> Option<Self::Event>;
16
17 /// Determine whether there are pending events.
18 #[must_use]
19 fn has_events(&self) -> bool;
20
21 /// Construct an iterator that produces all events.
22 fn events(&'_ mut self) -> Iter<'_, Self, Self::Event> {
23 Iter::new(self)
24 }
25 }
26
27 pub struct Iter<'a, P, E>
28 where
29 P: ?Sized,
30 {
31 p: &'a mut P,
32 _e: PhantomData<E>,
33 }
34
35 impl<'a, P, E> Iter<'a, P, E>
36 where
37 P: Provider<Event = E> + ?Sized,
38 {
39 fn new(p: &'a mut P) -> Self {
40 Self { p, _e: PhantomData }
41 }
42 }
43
44 impl<P, E> Iterator for Iter<'_, P, E>
45 where
46 P: Provider<Event = E>,
47 {
48 type Item = E;
49 fn next(&mut self) -> Option<Self::Item> {
50 self.p.next_event()
51 }
52 }