Skip to content

Commit 682a9d4

Browse files
committed
Improve the dependent type theory stuff documentation
Signed-off-by: Alex Saveau <[email protected]>
1 parent 30d5a2c commit 682a9d4

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/event/epoll.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ use crate::fd::{AsFd, OwnedFd};
7979
use crate::io;
8080
#[cfg(feature = "alloc")]
8181
use alloc::vec::Vec;
82+
pub use buf::EventBuffer;
8283
use core::ffi::c_void;
8384
use core::hash::{Hash, Hasher};
8485
use core::slice;
@@ -192,14 +193,14 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> {
192193
///
193194
/// [Linux]: https://man7.org/linux/man-pages/man2/epoll_wait.2.html
194195
#[inline]
195-
pub fn wait<B: buf::EventBuffer>(
196+
pub fn wait<B: EventBuffer>(
196197
epoll: impl AsFd,
197198
mut events: B,
198199
timeout: c::c_int,
199200
) -> io::Result<B::Out> {
200201
unsafe {
201-
let nfds = syscalls::epoll_wait(epoll.as_fd(), events.convert(), timeout)?;
202-
Ok(events.filled(nfds))
202+
let nfds = syscalls::epoll_wait(epoll.as_fd(), events.convert(buf::Internal), timeout)?;
203+
Ok(events.filled(nfds, buf::Internal))
203204
}
204205
}
205206

@@ -442,24 +443,32 @@ mod buf {
442443
use alloc::vec::Vec;
443444
use core::mem::MaybeUninit;
444445

446+
pub struct Internal;
447+
448+
/// Implementation detail trait to support different return types.
449+
///
450+
/// Check the [`Self::Out`] type for each implementation.
445451
pub trait EventBuffer {
452+
/// The return type of this input.
446453
type Out;
447454

448-
fn convert(&mut self) -> &mut [MaybeUninit<Event>];
455+
#[doc(hidden)]
456+
fn convert(&mut self, _: Internal) -> &mut [MaybeUninit<Event>];
449457

450-
unsafe fn filled(self, count: usize) -> Self::Out;
458+
#[doc(hidden)]
459+
unsafe fn filled(self, count: usize, _: Internal) -> Self::Out;
451460
}
452461

453462
#[cfg(feature = "alloc")]
454463
impl EventBuffer for &mut super::EventVec {
455464
type Out = ();
456465

457-
fn convert(&mut self) -> &mut [MaybeUninit<Event>] {
466+
fn convert(&mut self, _: Internal) -> &mut [MaybeUninit<Event>] {
458467
self.events.clear();
459468
self.events.spare_capacity_mut()
460469
}
461470

462-
unsafe fn filled(self, count: usize) -> Self::Out {
471+
unsafe fn filled(self, count: usize, _: Internal) -> Self::Out {
463472
unsafe {
464473
self.events.set_len(count);
465474
}
@@ -470,11 +479,11 @@ mod buf {
470479
impl EventBuffer for &mut Vec<Event> {
471480
type Out = ();
472481

473-
fn convert(&mut self) -> &mut [MaybeUninit<Event>] {
482+
fn convert(&mut self, _: Internal) -> &mut [MaybeUninit<Event>] {
474483
self.spare_capacity_mut()
475484
}
476485

477-
unsafe fn filled(self, count: usize) -> Self::Out {
486+
unsafe fn filled(self, count: usize, _: Internal) -> Self::Out {
478487
unsafe {
479488
self.set_len(count);
480489
}
@@ -484,24 +493,24 @@ mod buf {
484493
impl<'a> EventBuffer for &'a mut [Event] {
485494
type Out = &'a mut [Event];
486495

487-
fn convert(&mut self) -> &mut [MaybeUninit<Event>] {
496+
fn convert(&mut self, _: Internal) -> &mut [MaybeUninit<Event>] {
488497
// SAFETY: we (and the kernel) never uninitialize any values
489498
unsafe { core::mem::transmute::<&mut [Event], &mut [MaybeUninit<Event>]>(self) }
490499
}
491500

492-
unsafe fn filled(self, count: usize) -> Self::Out {
501+
unsafe fn filled(self, count: usize, _: Internal) -> Self::Out {
493502
&mut self[..count]
494503
}
495504
}
496505

497506
impl<'a> EventBuffer for &'a mut [MaybeUninit<Event>] {
498507
type Out = &'a mut [Event];
499508

500-
fn convert(&mut self) -> &mut [MaybeUninit<Event>] {
509+
fn convert(&mut self, _: Internal) -> &mut [MaybeUninit<Event>] {
501510
self
502511
}
503512

504-
unsafe fn filled(self, count: usize) -> Self::Out {
513+
unsafe fn filled(self, count: usize, _: Internal) -> Self::Out {
505514
unsafe { split_init(self, count) }.0
506515
}
507516
}

0 commit comments

Comments
 (0)