@@ -79,6 +79,7 @@ use crate::fd::{AsFd, OwnedFd};
79
79
use crate :: io;
80
80
#[ cfg( feature = "alloc" ) ]
81
81
use alloc:: vec:: Vec ;
82
+ pub use buf:: EventBuffer ;
82
83
use core:: ffi:: c_void;
83
84
use core:: hash:: { Hash , Hasher } ;
84
85
use core:: slice;
@@ -192,14 +193,14 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> {
192
193
///
193
194
/// [Linux]: https://man7.org/linux/man-pages/man2/epoll_wait.2.html
194
195
#[ inline]
195
- pub fn wait < B : buf :: EventBuffer > (
196
+ pub fn wait < B : EventBuffer > (
196
197
epoll : impl AsFd ,
197
198
mut events : B ,
198
199
timeout : c:: c_int ,
199
200
) -> io:: Result < B :: Out > {
200
201
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 ) )
203
204
}
204
205
}
205
206
@@ -442,24 +443,32 @@ mod buf {
442
443
use alloc:: vec:: Vec ;
443
444
use core:: mem:: MaybeUninit ;
444
445
446
+ pub struct Internal ;
447
+
448
+ /// Implementation detail trait to support different return types.
449
+ ///
450
+ /// Check the [`Self::Out`] type for each implementation.
445
451
pub trait EventBuffer {
452
+ /// The return type of this input.
446
453
type Out ;
447
454
448
- fn convert ( & mut self ) -> & mut [ MaybeUninit < Event > ] ;
455
+ #[ doc( hidden) ]
456
+ fn convert ( & mut self , _: Internal ) -> & mut [ MaybeUninit < Event > ] ;
449
457
450
- unsafe fn filled ( self , count : usize ) -> Self :: Out ;
458
+ #[ doc( hidden) ]
459
+ unsafe fn filled ( self , count : usize , _: Internal ) -> Self :: Out ;
451
460
}
452
461
453
462
#[ cfg( feature = "alloc" ) ]
454
463
impl EventBuffer for & mut super :: EventVec {
455
464
type Out = ( ) ;
456
465
457
- fn convert ( & mut self ) -> & mut [ MaybeUninit < Event > ] {
466
+ fn convert ( & mut self , _ : Internal ) -> & mut [ MaybeUninit < Event > ] {
458
467
self . events . clear ( ) ;
459
468
self . events . spare_capacity_mut ( )
460
469
}
461
470
462
- unsafe fn filled ( self , count : usize ) -> Self :: Out {
471
+ unsafe fn filled ( self , count : usize , _ : Internal ) -> Self :: Out {
463
472
unsafe {
464
473
self . events . set_len ( count) ;
465
474
}
@@ -470,11 +479,11 @@ mod buf {
470
479
impl EventBuffer for & mut Vec < Event > {
471
480
type Out = ( ) ;
472
481
473
- fn convert ( & mut self ) -> & mut [ MaybeUninit < Event > ] {
482
+ fn convert ( & mut self , _ : Internal ) -> & mut [ MaybeUninit < Event > ] {
474
483
self . spare_capacity_mut ( )
475
484
}
476
485
477
- unsafe fn filled ( self , count : usize ) -> Self :: Out {
486
+ unsafe fn filled ( self , count : usize , _ : Internal ) -> Self :: Out {
478
487
unsafe {
479
488
self . set_len ( count) ;
480
489
}
@@ -484,24 +493,24 @@ mod buf {
484
493
impl < ' a > EventBuffer for & ' a mut [ Event ] {
485
494
type Out = & ' a mut [ Event ] ;
486
495
487
- fn convert ( & mut self ) -> & mut [ MaybeUninit < Event > ] {
496
+ fn convert ( & mut self , _ : Internal ) -> & mut [ MaybeUninit < Event > ] {
488
497
// SAFETY: we (and the kernel) never uninitialize any values
489
498
unsafe { core:: mem:: transmute :: < & mut [ Event ] , & mut [ MaybeUninit < Event > ] > ( self ) }
490
499
}
491
500
492
- unsafe fn filled ( self , count : usize ) -> Self :: Out {
501
+ unsafe fn filled ( self , count : usize , _ : Internal ) -> Self :: Out {
493
502
& mut self [ ..count]
494
503
}
495
504
}
496
505
497
506
impl < ' a > EventBuffer for & ' a mut [ MaybeUninit < Event > ] {
498
507
type Out = & ' a mut [ Event ] ;
499
508
500
- fn convert ( & mut self ) -> & mut [ MaybeUninit < Event > ] {
509
+ fn convert ( & mut self , _ : Internal ) -> & mut [ MaybeUninit < Event > ] {
501
510
self
502
511
}
503
512
504
- unsafe fn filled ( self , count : usize ) -> Self :: Out {
513
+ unsafe fn filled ( self , count : usize , _ : Internal ) -> Self :: Out {
505
514
unsafe { split_init ( self , count) } . 0
506
515
}
507
516
}
0 commit comments