diff --git a/src/event/epoll.rs b/src/event/epoll.rs index 56fc930a1..e5516f524 100644 --- a/src/event/epoll.rs +++ b/src/event/epoll.rs @@ -79,6 +79,7 @@ use crate::fd::{AsFd, OwnedFd}; use crate::io; #[cfg(feature = "alloc")] use alloc::vec::Vec; +pub use buf::EventBuffer; use core::ffi::c_void; use core::hash::{Hash, Hasher}; use core::slice; @@ -192,14 +193,14 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> { /// /// [Linux]: https://man7.org/linux/man-pages/man2/epoll_wait.2.html #[inline] -pub fn wait( +pub fn wait( epoll: impl AsFd, mut events: B, timeout: c::c_int, ) -> io::Result { unsafe { - let nfds = syscalls::epoll_wait(epoll.as_fd(), events.convert(), timeout)?; - Ok(events.filled(nfds)) + let nfds = syscalls::epoll_wait(epoll.as_fd(), events.convert(buf::Internal), timeout)?; + Ok(events.filled(nfds, buf::Internal)) } } @@ -442,24 +443,32 @@ mod buf { use alloc::vec::Vec; use core::mem::MaybeUninit; + pub struct Internal; + + /// Implementation detail trait to support different return types. + /// + /// Check the [`Self::Out`] type for each implementation. pub trait EventBuffer { + /// The return type of this input. type Out; - fn convert(&mut self) -> &mut [MaybeUninit]; + #[doc(hidden)] + fn convert(&mut self, _: Internal) -> &mut [MaybeUninit]; - unsafe fn filled(self, count: usize) -> Self::Out; + #[doc(hidden)] + unsafe fn filled(self, count: usize, _: Internal) -> Self::Out; } #[cfg(feature = "alloc")] impl EventBuffer for &mut super::EventVec { type Out = (); - fn convert(&mut self) -> &mut [MaybeUninit] { + fn convert(&mut self, _: Internal) -> &mut [MaybeUninit] { self.events.clear(); self.events.spare_capacity_mut() } - unsafe fn filled(self, count: usize) -> Self::Out { + unsafe fn filled(self, count: usize, _: Internal) -> Self::Out { unsafe { self.events.set_len(count); } @@ -470,11 +479,11 @@ mod buf { impl EventBuffer for &mut Vec { type Out = (); - fn convert(&mut self) -> &mut [MaybeUninit] { + fn convert(&mut self, _: Internal) -> &mut [MaybeUninit] { self.spare_capacity_mut() } - unsafe fn filled(self, count: usize) -> Self::Out { + unsafe fn filled(self, count: usize, _: Internal) -> Self::Out { unsafe { self.set_len(count); } @@ -484,12 +493,12 @@ mod buf { impl<'a> EventBuffer for &'a mut [Event] { type Out = &'a mut [Event]; - fn convert(&mut self) -> &mut [MaybeUninit] { + fn convert(&mut self, _: Internal) -> &mut [MaybeUninit] { // SAFETY: we (and the kernel) never uninitialize any values unsafe { core::mem::transmute::<&mut [Event], &mut [MaybeUninit]>(self) } } - unsafe fn filled(self, count: usize) -> Self::Out { + unsafe fn filled(self, count: usize, _: Internal) -> Self::Out { &mut self[..count] } } @@ -497,11 +506,11 @@ mod buf { impl<'a> EventBuffer for &'a mut [MaybeUninit] { type Out = &'a mut [Event]; - fn convert(&mut self) -> &mut [MaybeUninit] { + fn convert(&mut self, _: Internal) -> &mut [MaybeUninit] { self } - unsafe fn filled(self, count: usize) -> Self::Out { + unsafe fn filled(self, count: usize, _: Internal) -> Self::Out { unsafe { split_init(self, count) }.0 } }