Skip to content

Commit ccc8cf8

Browse files
authored
Replace epoll's EventVec with just Vec. (#1304)
`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.
1 parent f82d03b commit ccc8cf8

2 files changed

Lines changed: 6 additions & 127 deletions

File tree

src/event/epoll.rs

Lines changed: 5 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
//! let mut sockets = HashMap::new();
3737
//!
3838
//! // Process events.
39-
//! let mut event_list = epoll::EventVec::with_capacity(4);
39+
//! let mut event_list = Vec::with_capacity(4);
4040
//! loop {
4141
//! epoll::wait(&epoll, &mut event_list, -1)?;
4242
//! for event in &event_list {
@@ -81,7 +81,6 @@ use crate::io;
8181
use alloc::vec::Vec;
8282
use core::ffi::c_void;
8383
use core::hash::{Hash, Hasher};
84-
use core::slice;
8584

8685
/// `epoll_create1(flags)`—Creates a new epoll object.
8786
///
@@ -202,46 +201,20 @@ pub fn delete<EpollFd: AsFd, SourceFd: AsFd>(epoll: EpollFd, source: SourceFd) -
202201
#[inline]
203202
pub fn wait<EpollFd: AsFd>(
204203
epoll: EpollFd,
205-
event_list: &mut EventVec,
204+
event_list: &mut Vec<Event>,
206205
timeout: crate::ffi::c_int,
207206
) -> io::Result<()> {
208207
// SAFETY: We're calling `epoll_wait` via FFI and we know how it
209208
// behaves.
210209
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);
218213
}
219214

220215
Ok(())
221216
}
222217

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-
245218
/// A record of an event that occurred.
246219
#[repr(C)]
247220
#[cfg_attr(all(not(libc), target_arch = "x86_64"), repr(packed))]
@@ -357,100 +330,6 @@ struct SixtyFourBitPointer {
357330
_padding: u32,
358331
}
359332

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-
454333
#[cfg(test)]
455334
mod tests {
456335
use super::*;

tests/event/epoll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn server(ready: Arc<(Mutex<u16>, Condvar)>) {
4141
let mut next_data = epoll::EventData::new_u64(2);
4242
let mut targets = HashMap::new();
4343

44-
let mut event_list = epoll::EventVec::with_capacity(4);
44+
let mut event_list = Vec::with_capacity(4);
4545
loop {
4646
epoll::wait(&epoll, &mut event_list, -1).unwrap();
4747
for event in &event_list {

0 commit comments

Comments
 (0)