|
36 | 36 | //! let mut sockets = HashMap::new(); |
37 | 37 | //! |
38 | 38 | //! // Process events. |
39 | | -//! let mut event_list = epoll::EventVec::with_capacity(4); |
| 39 | +//! let mut event_list = Vec::with_capacity(4); |
40 | 40 | //! loop { |
41 | 41 | //! epoll::wait(&epoll, &mut event_list, -1)?; |
42 | 42 | //! for event in &event_list { |
@@ -81,7 +81,6 @@ use crate::io; |
81 | 81 | use alloc::vec::Vec; |
82 | 82 | use core::ffi::c_void; |
83 | 83 | use core::hash::{Hash, Hasher}; |
84 | | -use core::slice; |
85 | 84 |
|
86 | 85 | /// `epoll_create1(flags)`—Creates a new epoll object. |
87 | 86 | /// |
@@ -202,46 +201,20 @@ pub fn delete<EpollFd: AsFd, SourceFd: AsFd>(epoll: EpollFd, source: SourceFd) - |
202 | 201 | #[inline] |
203 | 202 | pub fn wait<EpollFd: AsFd>( |
204 | 203 | epoll: EpollFd, |
205 | | - event_list: &mut EventVec, |
| 204 | + event_list: &mut Vec<Event>, |
206 | 205 | timeout: crate::ffi::c_int, |
207 | 206 | ) -> io::Result<()> { |
208 | 207 | // SAFETY: We're calling `epoll_wait` via FFI and we know how it |
209 | 208 | // behaves. |
210 | 209 | unsafe { |
211 | | - event_list.events.clear(); |
212 | | - let nfds = syscalls::epoll_wait( |
213 | | - epoll.as_fd(), |
214 | | - event_list.events.spare_capacity_mut(), |
215 | | - timeout, |
216 | | - )?; |
217 | | - event_list.events.set_len(nfds); |
| 210 | + event_list.clear(); |
| 211 | + let nfds = syscalls::epoll_wait(epoll.as_fd(), event_list.spare_capacity_mut(), timeout)?; |
| 212 | + event_list.set_len(nfds); |
218 | 213 | } |
219 | 214 |
|
220 | 215 | Ok(()) |
221 | 216 | } |
222 | 217 |
|
223 | | -/// An iterator over the [`epoll::Event`]s in an [`epoll::EventVec`]. |
224 | | -pub struct Iter<'a> { |
225 | | - /// Use `Copied` to copy the struct, since `Event` is `packed` on some |
226 | | - /// platforms, and it's common for users to directly destructure it, which |
227 | | - /// would lead to errors about forming references to packed fields. |
228 | | - iter: core::iter::Copied<slice::Iter<'a, Event>>, |
229 | | -} |
230 | | - |
231 | | -impl<'a> Iterator for Iter<'a> { |
232 | | - type Item = epoll::Event; |
233 | | - |
234 | | - #[inline] |
235 | | - fn next(&mut self) -> Option<Self::Item> { |
236 | | - self.iter.next() |
237 | | - } |
238 | | - |
239 | | - #[inline] |
240 | | - fn size_hint(&self) -> (usize, Option<usize>) { |
241 | | - self.iter.size_hint() |
242 | | - } |
243 | | -} |
244 | | - |
245 | 218 | /// A record of an event that occurred. |
246 | 219 | #[repr(C)] |
247 | 220 | #[cfg_attr(all(not(libc), target_arch = "x86_64"), repr(packed))] |
@@ -357,100 +330,6 @@ struct SixtyFourBitPointer { |
357 | 330 | _padding: u32, |
358 | 331 | } |
359 | 332 |
|
360 | | -/// A vector of `epoll::Event`s, plus context for interpreting them. |
361 | | -#[cfg(feature = "alloc")] |
362 | | -pub struct EventVec { |
363 | | - events: Vec<Event>, |
364 | | -} |
365 | | - |
366 | | -#[cfg(feature = "alloc")] |
367 | | -impl EventVec { |
368 | | - /// Constructs an `epoll::EventVec` from raw pointer, length, and capacity. |
369 | | - /// |
370 | | - /// # Safety |
371 | | - /// |
372 | | - /// This function calls [`Vec::from_raw_parts`] with its arguments. |
373 | | - /// |
374 | | - /// [`Vec::from_raw_parts`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.from_raw_parts |
375 | | - #[inline] |
376 | | - pub unsafe fn from_raw_parts(ptr: *mut Event, len: usize, capacity: usize) -> Self { |
377 | | - Self { |
378 | | - events: Vec::from_raw_parts(ptr, len, capacity), |
379 | | - } |
380 | | - } |
381 | | - |
382 | | - /// Constructs an `epoll::EventVec` with memory for `capacity` |
383 | | - /// `epoll::Event`s. |
384 | | - #[inline] |
385 | | - pub fn with_capacity(capacity: usize) -> Self { |
386 | | - Self { |
387 | | - events: Vec::with_capacity(capacity), |
388 | | - } |
389 | | - } |
390 | | - |
391 | | - /// Returns the current `epoll::Event` capacity of this `epoll::EventVec`. |
392 | | - #[inline] |
393 | | - pub fn capacity(&self) -> usize { |
394 | | - self.events.capacity() |
395 | | - } |
396 | | - |
397 | | - /// Reserves enough memory for at least `additional` more `epoll::Event`s. |
398 | | - #[inline] |
399 | | - pub fn reserve(&mut self, additional: usize) { |
400 | | - self.events.reserve(additional); |
401 | | - } |
402 | | - |
403 | | - /// Reserves enough memory for exactly `additional` more `epoll::Event`s. |
404 | | - #[inline] |
405 | | - pub fn reserve_exact(&mut self, additional: usize) { |
406 | | - self.events.reserve_exact(additional); |
407 | | - } |
408 | | - |
409 | | - /// Clears all the `epoll::Events` out of this `epoll::EventVec`. |
410 | | - #[inline] |
411 | | - pub fn clear(&mut self) { |
412 | | - self.events.clear(); |
413 | | - } |
414 | | - |
415 | | - /// Shrinks the capacity of this `epoll::EventVec` as much as possible. |
416 | | - #[inline] |
417 | | - pub fn shrink_to_fit(&mut self) { |
418 | | - self.events.shrink_to_fit(); |
419 | | - } |
420 | | - |
421 | | - /// Returns an iterator over the `epoll::Event`s in this `epoll::EventVec`. |
422 | | - #[inline] |
423 | | - pub fn iter(&self) -> Iter<'_> { |
424 | | - Iter { |
425 | | - iter: self.events.iter().copied(), |
426 | | - } |
427 | | - } |
428 | | - |
429 | | - /// Returns the number of `epoll::Event`s logically contained in this |
430 | | - /// `epoll::EventVec`. |
431 | | - #[inline] |
432 | | - pub fn len(&mut self) -> usize { |
433 | | - self.events.len() |
434 | | - } |
435 | | - |
436 | | - /// Tests whether this `epoll::EventVec` is logically empty. |
437 | | - #[inline] |
438 | | - pub fn is_empty(&mut self) -> bool { |
439 | | - self.events.is_empty() |
440 | | - } |
441 | | -} |
442 | | - |
443 | | -#[cfg(feature = "alloc")] |
444 | | -impl<'a> IntoIterator for &'a EventVec { |
445 | | - type IntoIter = Iter<'a>; |
446 | | - type Item = epoll::Event; |
447 | | - |
448 | | - #[inline] |
449 | | - fn into_iter(self) -> Self::IntoIter { |
450 | | - self.iter() |
451 | | - } |
452 | | -} |
453 | | - |
454 | 333 | #[cfg(test)] |
455 | 334 | mod tests { |
456 | 335 | use super::*; |
|
0 commit comments