base_coroutine/event_loop/event/
events.rs

1use super::event::Event;
2use crate::event_loop::selector as sys;
3
4use std::fmt;
5
6/// A collection of readiness events.
7///
8/// `Events` is passed as an argument to [`Poll::poll`] and will be used to
9/// receive any new readiness events received since the last poll. Usually, a
10/// single `Events` instance is created at the same time as a [`Poll`] and
11/// reused on each call to [`Poll::poll`].
12///
13/// See [`Poll`] for more documentation on polling.
14///
15/// [`Poll::poll`]: ../struct.Poll.html#method.poll
16/// [`Poll`]: ../struct.Poll.html
17///
18/// # Examples
19///
20/// # use std::error::Error;
21/// # fn main() -> Result<(), Box<dyn Error>> {
22/// use mio::{Events, Poll};
23/// use std::time::Duration;
24///
25/// let mut events = Events::with_capacity(1024);
26/// let mut poll = Poll::new()?;
27/// #
28/// # assert!(events.is_empty());
29///
30/// // Register `event::Source`s with `poll`.
31///
32/// poll.poll(&mut events, Some(Duration::from_millis(100)))?;
33///
34/// for event in events.iter() {
35///     println!("Got an event for {:?}", event.token());
36/// }
37/// #     Ok(())
38/// # }
39/// ```
40pub struct Events {
41    inner: sys::Events,
42}
43
44/// [`Events`] iterator.
45///
46/// This struct is created by the [`iter`] method on [`Events`].
47///
48/// [`Events`]: struct.Events.html
49/// [`iter`]: struct.Events.html#method.iter
50///
51/// # Examples
52///
53/// # use std::error::Error;
54/// # fn main() -> Result<(), Box<dyn Error>> {
55/// use base_coroutine::{Events, Poll};
56/// use std::time::Duration;
57///
58/// let mut events = Events::with_capacity(1024);
59/// let mut poll = Poll::new()?;
60///
61/// // Register handles with `poll`.
62///
63/// poll.poll(&mut events, Some(Duration::from_millis(100)))?;
64///
65/// for event in events.iter() {
66///     println!("Got an event for {:?}", event.token());
67/// }
68/// #     Ok(())
69/// # }
70/// ```
71#[derive(Debug, Clone)]
72pub struct Iter<'a> {
73    inner: &'a Events,
74    pos: usize,
75}
76
77impl Events {
78    /// Return a new `Events` capable of holding up to `capacity` events.
79    ///
80    /// # Examples
81    ///
82    /// ```
83    /// use base_coroutine::Events;
84    ///
85    /// let events = Events::with_capacity(1024);
86    /// assert_eq!(1024, events.capacity());
87    /// ```
88    pub fn with_capacity(capacity: usize) -> Events {
89        Events {
90            inner: sys::Events::with_capacity(capacity),
91        }
92    }
93
94    /// Returns the number of `Event` values that `self` can hold.
95    ///
96    /// ```
97    /// use base_coroutine::Events;
98    ///
99    /// let events = Events::with_capacity(1024);
100    /// assert_eq!(1024, events.capacity());
101    /// ```
102    pub fn capacity(&self) -> usize {
103        self.inner.capacity()
104    }
105
106    /// Returns `true` if `self` contains no `Event` values.
107    ///
108    /// # Examples
109    ///
110    /// ```
111    /// use base_coroutine::Events;
112    ///
113    /// let events = Events::with_capacity(1024);
114    /// assert!(events.is_empty());
115    /// ```
116    pub fn is_empty(&self) -> bool {
117        self.inner.is_empty()
118    }
119
120    /// Returns an iterator over the `Event` values.
121    ///
122    /// # Examples
123    ///
124    /// # use std::error::Error;
125    /// # fn main() -> Result<(), Box<dyn Error>> {
126    /// use base_coroutine::{Events, Poll};
127    /// use std::time::Duration;
128    ///
129    /// let mut events = Events::with_capacity(1024);
130    /// let mut poll = Poll::new()?;
131    ///
132    /// // Register handles with `poll`.
133    ///
134    /// poll.poll(&mut events, Some(Duration::from_millis(100)))?;
135    ///
136    /// for event in events.iter() {
137    ///     println!("Got an event for {:?}", event.token());
138    /// }
139    /// #     Ok(())
140    /// # }
141    /// ```
142    pub fn iter(&self) -> Iter<'_> {
143        Iter {
144            inner: self,
145            pos: 0,
146        }
147    }
148
149    /// Clearing all `Event` values from container explicitly.
150    ///
151    /// # Notes
152    ///
153    /// Events are cleared before every `poll`, so it is not required to call
154    /// this manually.
155    ///
156    /// # Examples
157    ///
158    /// # use std::error::Error;
159    /// # fn main() -> Result<(), Box<dyn Error>> {
160    /// use base_coroutine::{Events, Poll};
161    /// use std::time::Duration;
162    ///
163    /// let mut events = Events::with_capacity(1024);
164    /// let mut poll = Poll::new()?;
165    ///
166    /// // Register handles with `poll`.
167    ///
168    /// poll.poll(&mut events, Some(Duration::from_millis(100)))?;
169    ///
170    /// // Clear all events.
171    /// events.clear();
172    /// assert!(events.is_empty());
173    /// #     Ok(())
174    /// # }
175    /// ```
176    pub fn clear(&mut self) {
177        self.inner.clear();
178    }
179
180    /// Returns the inner `sys::Events`.
181    pub(crate) fn sys(&mut self) -> &mut sys::Events {
182        &mut self.inner
183    }
184}
185
186impl<'a> IntoIterator for &'a Events {
187    type Item = &'a Event;
188    type IntoIter = Iter<'a>;
189
190    fn into_iter(self) -> Self::IntoIter {
191        self.iter()
192    }
193}
194
195impl<'a> Iterator for Iter<'a> {
196    type Item = &'a Event;
197
198    fn next(&mut self) -> Option<Self::Item> {
199        let ret = self
200            .inner
201            .inner
202            .get(self.pos)
203            .map(Event::from_sys_event_ref);
204        self.pos += 1;
205        ret
206    }
207
208    fn size_hint(&self) -> (usize, Option<usize>) {
209        let size = self.inner.inner.len();
210        (size, Some(size))
211    }
212
213    fn count(self) -> usize {
214        self.inner.inner.len()
215    }
216}
217
218impl fmt::Debug for Events {
219    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220        f.debug_list().entries(self).finish()
221    }
222}