summaryrefslogtreecommitdiffstats
path: root/third_party/rust/neqo-common/src/event.rs
blob: ea8d491822789d9c24c76ffb83b37f891f9644be (plain)
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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::marker::PhantomData;

/// An event provider is able to generate a stream of events.
pub trait Provider {
    type Event;

    /// Get the next event.
    #[must_use]
    fn next_event(&mut self) -> Option<Self::Event>;

    /// Determine whether there are pending events.
    #[must_use]
    fn has_events(&self) -> bool;

    /// Construct an iterator that produces all events.
    fn events(&'_ mut self) -> Iter<'_, Self, Self::Event> {
        Iter::new(self)
    }
}

pub struct Iter<'a, P, E>
where
    P: ?Sized,
{
    p: &'a mut P,
    _e: PhantomData<E>,
}

impl<'a, P, E> Iter<'a, P, E>
where
    P: Provider<Event = E> + ?Sized,
{
    fn new(p: &'a mut P) -> Self {
        Self { p, _e: PhantomData }
    }
}

impl<'a, P, E> Iterator for Iter<'a, P, E>
where
    P: Provider<Event = E>,
{
    type Item = E;
    fn next(&mut self) -> Option<Self::Item> {
        self.p.next_event()
    }
}