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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
use std::fmt; use {sys, Token, Ready}; /// A collection of readiness events. /// /// `Events` is passed as an argument to [`Poll::poll`] and will be used to /// receive any new readiness events received since the last call to [`poll`]. /// Usually, a single `Events` instance is created at the same time as the /// [`Poll`] and the single instance is reused for each call to [`poll`]. /// /// See [`Poll`] for more documentation on polling. /// /// # Examples /// /// ``` /// use soio::{Events, Poll}; /// use std::time::Duration; /// /// let mut events = Events::with_capacity(1024); /// let poll = Poll::new().unwrap(); /// /// assert_eq!(0, events.len()); /// /// // Register `Evented` handles with `poll` /// /// poll.poll(&mut events, Some(Duration::from_millis(100))).unwrap(); /// /// for event in &events { /// println!("event={:?}", event); /// } /// ``` /// /// [`Poll::poll`]: struct.Poll.html#method.poll /// [`poll`]: struct.Poll.html#method.poll /// [`Poll`]: struct.Poll.html pub struct Events { pub inner: sys::Events, } /// [`Events`] iterator. /// /// This struct is created by the [`iter`] method on [`Events`]. /// /// # Examples /// /// ``` /// use soio::{Events, Poll}; /// use std::time::Duration; /// /// let mut events = Events::with_capacity(1024); /// let poll = Poll::new().unwrap(); /// /// // Register handles with `poll` /// /// poll.poll(&mut events, Some(Duration::from_millis(100))).unwrap(); /// /// for event in events.iter() { /// println!("event={:?}", event); /// } /// ``` /// /// [`Events`]: struct.Events.html /// [`iter`]: struct.Events.html#method.iter #[derive(Debug)] pub struct Iter<'a> { inner: &'a Events, pos: usize, } impl Events { /// Return a new `Events` capable of holding up to `capacity` events. /// /// # Examples /// /// ``` /// use soio::Events; /// /// let events = Events::with_capacity(1024); /// /// assert_eq!(1024, events.capacity()); /// ``` pub fn with_capacity(capacity: usize) -> Events { Events { inner: sys::Events::with_capacity(capacity), } } /// Returns the `Event` at the given index, or `None` if the index is out of /// bounds. /// /// # Examples /// /// ``` /// use soio::{Events, Poll}; /// use std::time::Duration; /// /// let mut events = Events::with_capacity(1024); /// let poll = Poll::new().unwrap(); /// /// // Register handles with `poll` /// /// let n = poll.poll(&mut events, Some(Duration::from_millis(100))).unwrap(); /// /// for i in 0..n { /// println!("event={:?}", events.get(i).unwrap()); /// } /// ``` pub fn get(&self, idx: usize) -> Option<Event> { self.inner.get(idx) } /// Returns the number of `Event` values currently in `self`. /// /// # Examples /// /// ``` /// use soio::Events; /// /// let events = Events::with_capacity(1024); /// /// assert_eq!(0, events.len()); /// ``` pub fn len(&self) -> usize { self.inner.len() } /// Returns the number of `Event` values that `self` can hold. /// /// ``` /// use soio::Events; /// /// let events = Events::with_capacity(1024); /// /// assert_eq!(1024, events.capacity()); /// ``` pub fn capacity(&self) -> usize { self.inner.capacity() } /// Returns `true` if `self` contains no `Event` values. /// /// # Examples /// /// ``` /// use soio::Events; /// /// let events = Events::with_capacity(1024); /// /// assert!(events.is_empty()); /// ``` pub fn is_empty(&self) -> bool { self.inner.is_empty() } /// Returns an iterator over the `Event` values. /// /// # Examples /// /// ``` /// use soio::{Events, Poll}; /// use std::time::Duration; /// /// let mut events = Events::with_capacity(1024); /// let poll = Poll::new().unwrap(); /// /// // Register handles with `poll` /// /// poll.poll(&mut events, Some(Duration::from_millis(100))).unwrap(); /// /// for event in events.iter() { /// println!("event={:?}", event); /// } /// ``` pub fn iter(&self) -> Iter { Iter { inner: self, pos: 0 } } } impl<'a> IntoIterator for &'a Events { type Item = Event; type IntoIter = Iter<'a>; fn into_iter(self) -> Self::IntoIter { self.iter() } } impl<'a> Iterator for Iter<'a> { type Item = Event; fn next(&mut self) -> Option<Event> { let ret = self.inner.get(self.pos); self.pos += 1; ret } } impl fmt::Debug for Events { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Events") .field("len", &self.len()) .field("capacity", &self.capacity()) .finish() } } /// An readiness event returned by [`Poll::poll`]. /// /// `Event` is a [readiness state] paired with a [`Token`]. It is returned by /// [`Poll::poll`]. /// /// For more documentation on polling and events, see [`Poll`]. /// /// # Examples /// /// ``` /// use soio::{Event, Ready, Token}; /// /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0)); /// /// assert_eq!(event.readiness(), Ready::readable() | Ready::writable()); /// assert_eq!(event.token(), Token(0)); /// ``` /// /// [`Poll::poll`]: struct.Poll.html#method.poll /// [`Poll`]: struct.Poll.html /// [readiness state ]: struct.Ready.html /// [`Token`]: struct.Token.html #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub struct Event { kind: Ready, token: Token } impl Event { /// Creates a new `Event` containing `readiness` and `token` /// /// # Examples /// /// ``` /// use soio::{Event, Ready, Token}; /// /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0)); /// /// assert_eq!(event.readiness(), Ready::readable() | Ready::writable()); /// assert_eq!(event.token(), Token(0)); /// ``` pub fn new(readiness: Ready, token: Token) -> Event { Event { kind: readiness, token: token, } } /// Returns the event's readiness. /// /// # Examples /// /// ``` /// use soio::{Event, Ready, Token}; /// /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0)); /// /// assert_eq!(event.readiness(), Ready::readable() | Ready::writable()); /// ``` pub fn readiness(&self) -> Ready { self.kind } /// Returns the event's token. /// /// # Examples /// /// ``` /// use soio::{Event, Ready, Token}; /// /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0)); /// /// assert_eq!(event.token(), Token(0)); /// ``` pub fn token(&self) -> Token { self.token } } // Used internally to mutate an `Event` in place // Not used on all platforms and was used on kqueue #[allow(dead_code)] pub fn kind_mut(event: &mut Event) -> &mut Ready { &mut event.kind }