71
71
72
72
#![ allow( unsafe_code) ]
73
73
#![ allow( unused_qualifications) ]
74
+ #![ allow( deprecated) ]
74
75
75
76
use super :: epoll;
76
77
use crate :: backend:: c;
@@ -80,6 +81,7 @@ use crate::fd::{AsFd, OwnedFd};
80
81
use crate :: io;
81
82
#[ cfg( feature = "alloc" ) ]
82
83
use alloc:: vec:: Vec ;
84
+ pub use buf:: EventBuffer ;
83
85
use core:: ffi:: c_void;
84
86
use core:: hash:: { Hash , Hasher } ;
85
87
use core:: slice;
@@ -191,20 +193,15 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> {
191
193
#[ cfg( feature = "alloc" ) ]
192
194
#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) , alias = "epoll_wait" ) ) ]
193
195
#[ inline]
194
- pub fn wait ( epoll : impl AsFd , event_list : & mut EventVec , timeout : c:: c_int ) -> io:: Result < ( ) > {
195
- // SAFETY: We're calling `epoll_wait` via FFI and we know how it
196
- // behaves.
196
+ pub fn wait < B : EventBuffer > (
197
+ epoll : impl AsFd ,
198
+ mut events : B ,
199
+ timeout : c:: c_int ,
200
+ ) -> io:: Result < B :: Out > {
197
201
unsafe {
198
- event_list. events . clear ( ) ;
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) ;
202
+ let nfds = syscalls:: epoll_wait ( epoll. as_fd ( ) , events. convert ( buf:: Internal ) , timeout) ?;
203
+ Ok ( events. filled ( nfds, buf:: Internal ) )
205
204
}
206
-
207
- Ok ( ( ) )
208
205
}
209
206
210
207
/// An iterator over the `Event`s in an `EventVec`.
@@ -342,6 +339,7 @@ struct SixtyFourBitPointer {
342
339
343
340
/// A vector of `Event`s, plus context for interpreting them.
344
341
#[ cfg( feature = "alloc" ) ]
342
+ #[ deprecated( note = "Use an array or vec directly instead." ) ]
345
343
pub struct EventVec {
346
344
events : Vec < Event > ,
347
345
}
@@ -442,3 +440,83 @@ fn test_epoll_layouts() {
442
440
#[ cfg( not( libc) ) ]
443
441
check_renamed_struct_renamed_field ! ( Event , epoll_event, data, data) ;
444
442
}
443
+
444
+ mod buf {
445
+ use super :: Event ;
446
+ use crate :: buffer:: split_init;
447
+ #[ cfg( feature = "alloc" ) ]
448
+ use alloc:: vec:: Vec ;
449
+ use core:: mem:: MaybeUninit ;
450
+
451
+ pub struct Internal ;
452
+
453
+ /// Implementation detail trait to support different return types.
454
+ ///
455
+ /// Check the [`Self::Out`] type for each implementation.
456
+ pub trait EventBuffer {
457
+ /// The return type of this input.
458
+ type Out ;
459
+
460
+ #[ doc( hidden) ]
461
+ fn convert ( & mut self , _: Internal ) -> & mut [ MaybeUninit < Event > ] ;
462
+
463
+ #[ doc( hidden) ]
464
+ unsafe fn filled ( self , count : usize , _: Internal ) -> Self :: Out ;
465
+ }
466
+
467
+ #[ cfg( feature = "alloc" ) ]
468
+ impl EventBuffer for & mut super :: EventVec {
469
+ type Out = ( ) ;
470
+
471
+ fn convert ( & mut self , _: Internal ) -> & mut [ MaybeUninit < Event > ] {
472
+ self . events . clear ( ) ;
473
+ self . events . spare_capacity_mut ( )
474
+ }
475
+
476
+ unsafe fn filled ( self , count : usize , _: Internal ) -> Self :: Out {
477
+ unsafe {
478
+ self . events . set_len ( count) ;
479
+ }
480
+ }
481
+ }
482
+
483
+ #[ cfg( feature = "alloc" ) ]
484
+ impl EventBuffer for & mut Vec < Event > {
485
+ type Out = ( ) ;
486
+
487
+ fn convert ( & mut self , _: Internal ) -> & mut [ MaybeUninit < Event > ] {
488
+ self . spare_capacity_mut ( )
489
+ }
490
+
491
+ unsafe fn filled ( self , count : usize , _: Internal ) -> Self :: Out {
492
+ unsafe {
493
+ self . set_len ( count) ;
494
+ }
495
+ }
496
+ }
497
+
498
+ impl < ' a > EventBuffer for & ' a mut [ Event ] {
499
+ type Out = & ' a mut [ Event ] ;
500
+
501
+ fn convert ( & mut self , _: Internal ) -> & mut [ MaybeUninit < Event > ] {
502
+ // SAFETY: we (and the kernel) never uninitialize any values
503
+ unsafe { core:: mem:: transmute :: < & mut [ Event ] , & mut [ MaybeUninit < Event > ] > ( self ) }
504
+ }
505
+
506
+ unsafe fn filled ( self , count : usize , _: Internal ) -> Self :: Out {
507
+ & mut self [ ..count]
508
+ }
509
+ }
510
+
511
+ impl < ' a > EventBuffer for & ' a mut [ MaybeUninit < Event > ] {
512
+ type Out = & ' a mut [ Event ] ;
513
+
514
+ fn convert ( & mut self , _: Internal ) -> & mut [ MaybeUninit < Event > ] {
515
+ self
516
+ }
517
+
518
+ unsafe fn filled ( self , count : usize , _: Internal ) -> Self :: Out {
519
+ unsafe { split_init ( self , count) } . 0
520
+ }
521
+ }
522
+ }
0 commit comments