|
70 | 70 | //! ```
|
71 | 71 |
|
72 | 72 | #![allow(unsafe_code)]
|
| 73 | +#![allow(deprecated)] |
73 | 74 |
|
74 | 75 | use crate::backend::c;
|
75 | 76 | pub use crate::backend::event::epoll::*;
|
@@ -190,21 +191,16 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> {
|
190 | 191 | /// - [Linux]
|
191 | 192 | ///
|
192 | 193 | /// [Linux]: https://man7.org/linux/man-pages/man2/epoll_wait.2.html
|
193 |
| -#[cfg(feature = "alloc")] |
194 |
| -#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] |
195 | 194 | #[inline]
|
196 |
| -pub fn wait(epoll: impl AsFd, event_list: &mut EventVec, timeout: c::c_int) -> io::Result<()> { |
| 195 | +pub fn wait<B: buf::EventBuffer>( |
| 196 | + epoll: impl AsFd, |
| 197 | + mut events: B, |
| 198 | + timeout: c::c_int, |
| 199 | +) -> io::Result<B::Out> { |
197 | 200 | unsafe {
|
198 |
| - event_list.events.set_len(0); |
199 |
| - let nfds = syscalls::epoll_wait( |
200 |
| - epoll.as_fd(), |
201 |
| - event_list.events.spare_capacity_mut(), |
202 |
| - timeout, |
203 |
| - )?; |
204 |
| - event_list.events.set_len(nfds); |
| 201 | + let nfds = syscalls::epoll_wait(epoll.as_fd(), events.convert(), timeout)?; |
| 202 | + Ok(events.filled(nfds)) |
205 | 203 | }
|
206 |
| - |
207 |
| - Ok(()) |
208 | 204 | }
|
209 | 205 |
|
210 | 206 | /// An iterator over the `Event`s in an `EventVec`.
|
@@ -337,6 +333,7 @@ struct SixtyFourBitPointer {
|
337 | 333 |
|
338 | 334 | /// A vector of `Event`s, plus context for interpreting them.
|
339 | 335 | #[cfg(feature = "alloc")]
|
| 336 | +#[deprecated(note = "Use an array or vec directly instead.")] |
340 | 337 | pub struct EventVec {
|
341 | 338 | events: Vec<Event>,
|
342 | 339 | }
|
@@ -437,3 +434,62 @@ fn test_epoll_layouts() {
|
437 | 434 | #[cfg(not(libc))]
|
438 | 435 | check_renamed_struct_renamed_field!(Event, epoll_event, data, data);
|
439 | 436 | }
|
| 437 | + |
| 438 | +mod buf { |
| 439 | + use super::Event; |
| 440 | + #[cfg(feature = "alloc")] |
| 441 | + use alloc::vec::Vec; |
| 442 | + use core::mem::MaybeUninit; |
| 443 | + |
| 444 | + pub trait EventBuffer { |
| 445 | + type Out; |
| 446 | + |
| 447 | + fn convert(&mut self) -> &mut [MaybeUninit<Event>]; |
| 448 | + |
| 449 | + unsafe fn filled(self, count: usize) -> Self::Out; |
| 450 | + } |
| 451 | + |
| 452 | + #[cfg(feature = "alloc")] |
| 453 | + impl EventBuffer for &mut super::EventVec { |
| 454 | + type Out = (); |
| 455 | + |
| 456 | + fn convert(&mut self) -> &mut [MaybeUninit<Event>] { |
| 457 | + self.events.clear(); |
| 458 | + self.events.spare_capacity_mut() |
| 459 | + } |
| 460 | + |
| 461 | + unsafe fn filled(self, count: usize) -> Self::Out { |
| 462 | + unsafe { |
| 463 | + self.events.set_len(count); |
| 464 | + } |
| 465 | + } |
| 466 | + } |
| 467 | + |
| 468 | + #[cfg(feature = "alloc")] |
| 469 | + impl EventBuffer for &mut Vec<Event> { |
| 470 | + type Out = (); |
| 471 | + |
| 472 | + fn convert(&mut self) -> &mut [MaybeUninit<Event>] { |
| 473 | + self.spare_capacity_mut() |
| 474 | + } |
| 475 | + |
| 476 | + unsafe fn filled(self, count: usize) -> Self::Out { |
| 477 | + unsafe { |
| 478 | + self.set_len(count); |
| 479 | + } |
| 480 | + } |
| 481 | + } |
| 482 | + |
| 483 | + impl<'a> EventBuffer for &'a mut [MaybeUninit<Event>] { |
| 484 | + type Out = &'a mut [Event]; |
| 485 | + |
| 486 | + fn convert(&mut self) -> &mut [MaybeUninit<Event>] { |
| 487 | + self |
| 488 | + } |
| 489 | + |
| 490 | + unsafe fn filled(self, count: usize) -> Self::Out { |
| 491 | + let filled = &mut self[..count]; |
| 492 | + unsafe { core::mem::transmute::<&mut [MaybeUninit<Event>], &mut [Event]>(filled) } |
| 493 | + } |
| 494 | + } |
| 495 | +} |
0 commit comments