crossio_core/
event.rs

1//! Event types produced by backends when sources become ready.
2use crate::{interest::Interest, token::Token};
3
4/// A single readiness notification produced by a backend.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct Event {
7    token: Token,
8    readiness: Interest,
9}
10
11impl Event {
12    /// Creates an event with the provided token and readiness flags.
13    pub const fn new(token: Token, readiness: Interest) -> Self {
14        Self { token, readiness }
15    }
16
17    /// Returns the token that was associated with the ready source.
18    pub const fn token(&self) -> Token {
19        self.token
20    }
21
22    /// Returns the readiness information reported by the backend.
23    pub const fn readiness(&self) -> Interest {
24        self.readiness
25    }
26}
27
28/// Collection of events returned from a poll operation.
29#[derive(Debug, Default, Clone)]
30pub struct Events {
31    entries: Vec<Event>,
32}
33
34impl Events {
35    /// Constructs an empty events list.
36    pub fn new() -> Self {
37        Self { entries: Vec::new() }
38    }
39
40    /// Pre-allocates storage for `capacity` events.
41    pub fn with_capacity(capacity: usize) -> Self {
42        Self {
43            entries: Vec::with_capacity(capacity),
44        }
45    }
46
47    /// Returns the number of events currently stored.
48    pub fn len(&self) -> usize {
49        self.entries.len()
50    }
51
52    /// Returns true when no events are stored.
53    pub fn is_empty(&self) -> bool {
54        self.entries.is_empty()
55    }
56
57    /// Clears all collected events without releasing allocation.
58    pub fn clear(&mut self) {
59        self.entries.clear();
60    }
61
62    /// Iterator over the collected events.
63    pub fn iter(&self) -> impl Iterator<Item = &Event> {
64        self.entries.iter()
65    }
66
67    /// Adds a new event to the collection. Intended for backend implementations.
68    pub fn push(&mut self, event: Event) {
69        self.entries.push(event);
70    }
71}
72
73impl IntoIterator for Events {
74    type Item = Event;
75    type IntoIter = std::vec::IntoIter<Event>;
76
77    fn into_iter(self) -> Self::IntoIter {
78        self.entries.into_iter()
79    }
80}
81
82impl<'a> IntoIterator for &'a Events {
83    type Item = &'a Event;
84    type IntoIter = std::slice::Iter<'a, Event>;
85
86    fn into_iter(self) -> Self::IntoIter {
87        self.entries.iter()
88    }
89}