From de84577407285c4e9f9addabdb265cdb2ed0a67e Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 30 Jan 2025 21:04:17 -0800 Subject: [PATCH] Replace epoll's `EventVec` with just `Vec`. `EventVec` was an artifact of an older API. At this point, it was just a trivial wrapper around a `Vec`, so we can drop it and just use `Vec` now. --- src/event/epoll.rs | 131 ++----------------------------------------- tests/event/epoll.rs | 2 +- 2 files changed, 6 insertions(+), 127 deletions(-) diff --git a/src/event/epoll.rs b/src/event/epoll.rs index dd7d1ee50..daa200adb 100644 --- a/src/event/epoll.rs +++ b/src/event/epoll.rs @@ -36,7 +36,7 @@ //! let mut sockets = HashMap::new(); //! //! // Process events. -//! let mut event_list = epoll::EventVec::with_capacity(4); +//! let mut event_list = Vec::with_capacity(4); //! loop { //! epoll::wait(&epoll, &mut event_list, -1)?; //! for event in &event_list { @@ -81,7 +81,6 @@ use crate::io; use alloc::vec::Vec; use core::ffi::c_void; use core::hash::{Hash, Hasher}; -use core::slice; /// `epoll_create1(flags)`—Creates a new epoll object. /// @@ -202,46 +201,20 @@ pub fn delete(epoll: EpollFd, source: SourceFd) - #[inline] pub fn wait( epoll: EpollFd, - event_list: &mut EventVec, + event_list: &mut Vec, timeout: crate::ffi::c_int, ) -> io::Result<()> { // SAFETY: We're calling `epoll_wait` via FFI and we know how it // behaves. unsafe { - event_list.events.clear(); - let nfds = syscalls::epoll_wait( - epoll.as_fd(), - event_list.events.spare_capacity_mut(), - timeout, - )?; - event_list.events.set_len(nfds); + event_list.clear(); + let nfds = syscalls::epoll_wait(epoll.as_fd(), event_list.spare_capacity_mut(), timeout)?; + event_list.set_len(nfds); } Ok(()) } -/// An iterator over the [`epoll::Event`]s in an [`epoll::EventVec`]. -pub struct Iter<'a> { - /// Use `Copied` to copy the struct, since `Event` is `packed` on some - /// platforms, and it's common for users to directly destructure it, which - /// would lead to errors about forming references to packed fields. - iter: core::iter::Copied>, -} - -impl<'a> Iterator for Iter<'a> { - type Item = epoll::Event; - - #[inline] - fn next(&mut self) -> Option { - self.iter.next() - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - self.iter.size_hint() - } -} - /// A record of an event that occurred. #[repr(C)] #[cfg_attr(all(not(libc), target_arch = "x86_64"), repr(packed))] @@ -357,100 +330,6 @@ struct SixtyFourBitPointer { _padding: u32, } -/// A vector of `epoll::Event`s, plus context for interpreting them. -#[cfg(feature = "alloc")] -pub struct EventVec { - events: Vec, -} - -#[cfg(feature = "alloc")] -impl EventVec { - /// Constructs an `epoll::EventVec` from raw pointer, length, and capacity. - /// - /// # Safety - /// - /// This function calls [`Vec::from_raw_parts`] with its arguments. - /// - /// [`Vec::from_raw_parts`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.from_raw_parts - #[inline] - pub unsafe fn from_raw_parts(ptr: *mut Event, len: usize, capacity: usize) -> Self { - Self { - events: Vec::from_raw_parts(ptr, len, capacity), - } - } - - /// Constructs an `epoll::EventVec` with memory for `capacity` - /// `epoll::Event`s. - #[inline] - pub fn with_capacity(capacity: usize) -> Self { - Self { - events: Vec::with_capacity(capacity), - } - } - - /// Returns the current `epoll::Event` capacity of this `epoll::EventVec`. - #[inline] - pub fn capacity(&self) -> usize { - self.events.capacity() - } - - /// Reserves enough memory for at least `additional` more `epoll::Event`s. - #[inline] - pub fn reserve(&mut self, additional: usize) { - self.events.reserve(additional); - } - - /// Reserves enough memory for exactly `additional` more `epoll::Event`s. - #[inline] - pub fn reserve_exact(&mut self, additional: usize) { - self.events.reserve_exact(additional); - } - - /// Clears all the `epoll::Events` out of this `epoll::EventVec`. - #[inline] - pub fn clear(&mut self) { - self.events.clear(); - } - - /// Shrinks the capacity of this `epoll::EventVec` as much as possible. - #[inline] - pub fn shrink_to_fit(&mut self) { - self.events.shrink_to_fit(); - } - - /// Returns an iterator over the `epoll::Event`s in this `epoll::EventVec`. - #[inline] - pub fn iter(&self) -> Iter<'_> { - Iter { - iter: self.events.iter().copied(), - } - } - - /// Returns the number of `epoll::Event`s logically contained in this - /// `epoll::EventVec`. - #[inline] - pub fn len(&mut self) -> usize { - self.events.len() - } - - /// Tests whether this `epoll::EventVec` is logically empty. - #[inline] - pub fn is_empty(&mut self) -> bool { - self.events.is_empty() - } -} - -#[cfg(feature = "alloc")] -impl<'a> IntoIterator for &'a EventVec { - type IntoIter = Iter<'a>; - type Item = epoll::Event; - - #[inline] - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/tests/event/epoll.rs b/tests/event/epoll.rs index 7aa64b523..4d9378648 100644 --- a/tests/event/epoll.rs +++ b/tests/event/epoll.rs @@ -41,7 +41,7 @@ fn server(ready: Arc<(Mutex, Condvar)>) { let mut next_data = epoll::EventData::new_u64(2); let mut targets = HashMap::new(); - let mut event_list = epoll::EventVec::with_capacity(4); + let mut event_list = Vec::with_capacity(4); loop { epoll::wait(&epoll, &mut event_list, -1).unwrap(); for event in &event_list {