@@ -29,7 +29,7 @@ struct Readers {
29
29
/// (cf. `Watcher::poll_read_ready`)
30
30
ready : bool ,
31
31
/// The `Waker`s blocked on reading.
32
- wakers : Vec < Waker >
32
+ wakers : Vec < Waker > ,
33
33
}
34
34
35
35
/// The set of `Waker`s interested in write readiness.
@@ -39,7 +39,7 @@ struct Writers {
39
39
/// (cf. `Watcher::poll_write_ready`)
40
40
ready : bool ,
41
41
/// The `Waker`s blocked on writing.
42
- wakers : Vec < Waker >
42
+ wakers : Vec < Waker > ,
43
43
}
44
44
45
45
/// The state of a networking driver.
@@ -88,8 +88,14 @@ impl Reactor {
88
88
// Allocate an entry and insert it into the slab.
89
89
let entry = Arc :: new ( Entry {
90
90
token,
91
- readers : Mutex :: new ( Readers { ready : false , wakers : Vec :: new ( ) } ) ,
92
- writers : Mutex :: new ( Writers { ready : false , wakers : Vec :: new ( ) } ) ,
91
+ readers : Mutex :: new ( Readers {
92
+ ready : false ,
93
+ wakers : Vec :: new ( ) ,
94
+ } ) ,
95
+ writers : Mutex :: new ( Writers {
96
+ ready : false ,
97
+ wakers : Vec :: new ( ) ,
98
+ } ) ,
93
99
} ) ;
94
100
vacant. insert ( entry. clone ( ) ) ;
95
101
@@ -249,6 +255,43 @@ impl<T: Evented> Watcher<T> {
249
255
Poll :: Pending
250
256
}
251
257
258
+ /// Polls the inner I/O source for a non-blocking read operation.
259
+ ///
260
+ /// If the operation returns an error of the `io::ErrorKind::WouldBlock` kind, the current task
261
+ /// will be registered for wakeup when the I/O source becomes readable.
262
+ pub fn poll_read_with_mut < ' a , F , R > (
263
+ & ' a mut self ,
264
+ cx : & mut Context < ' _ > ,
265
+ mut f : F ,
266
+ ) -> Poll < io:: Result < R > >
267
+ where
268
+ F : FnMut ( & mut T ) -> io:: Result < R > ,
269
+ {
270
+ // If the operation isn't blocked, return its result.
271
+ match f ( self . source . as_mut ( ) . unwrap ( ) ) {
272
+ Err ( err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => { }
273
+ res => return Poll :: Ready ( res) ,
274
+ }
275
+
276
+ // Lock the waker list.
277
+ let mut readers = self . entry . readers . lock ( ) . unwrap ( ) ;
278
+
279
+ // Try running the operation again.
280
+ match f ( self . source . as_mut ( ) . unwrap ( ) ) {
281
+ Err ( err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => { }
282
+ res => return Poll :: Ready ( res) ,
283
+ }
284
+
285
+ // Register the task if it isn't registered already.
286
+ if readers. wakers . iter ( ) . all ( |w| !w. will_wake ( cx. waker ( ) ) ) {
287
+ readers. wakers . push ( cx. waker ( ) . clone ( ) ) ;
288
+ }
289
+
290
+ readers. ready = false ;
291
+
292
+ Poll :: Pending
293
+ }
294
+
252
295
/// Polls the inner I/O source for a non-blocking write operation.
253
296
///
254
297
/// If the operation returns an error of the `io::ErrorKind::WouldBlock` kind, the current task
@@ -286,6 +329,43 @@ impl<T: Evented> Watcher<T> {
286
329
Poll :: Pending
287
330
}
288
331
332
+ /// Polls the inner I/O source for a non-blocking write operation.
333
+ ///
334
+ /// If the operation returns an error of the `io::ErrorKind::WouldBlock` kind, the current task
335
+ /// will be registered for wakeup when the I/O source becomes writable.
336
+ pub fn poll_write_with_mut < ' a , F , R > (
337
+ & ' a mut self ,
338
+ cx : & mut Context < ' _ > ,
339
+ mut f : F ,
340
+ ) -> Poll < io:: Result < R > >
341
+ where
342
+ F : FnMut ( & mut T ) -> io:: Result < R > ,
343
+ {
344
+ // If the operation isn't blocked, return its result.
345
+ match f ( self . source . as_mut ( ) . unwrap ( ) ) {
346
+ Err ( err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => { }
347
+ res => return Poll :: Ready ( res) ,
348
+ }
349
+
350
+ // Lock the waker list.
351
+ let mut writers = self . entry . writers . lock ( ) . unwrap ( ) ;
352
+
353
+ // Try running the operation again.
354
+ match f ( self . source . as_mut ( ) . unwrap ( ) ) {
355
+ Err ( err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => { }
356
+ res => return Poll :: Ready ( res) ,
357
+ }
358
+
359
+ // Register the task if it isn't registered already.
360
+ if writers. wakers . iter ( ) . all ( |w| !w. will_wake ( cx. waker ( ) ) ) {
361
+ writers. wakers . push ( cx. waker ( ) . clone ( ) ) ;
362
+ }
363
+
364
+ writers. ready = false ;
365
+
366
+ Poll :: Pending
367
+ }
368
+
289
369
/// Polls the inner I/O source until a non-blocking read can be performed.
290
370
///
291
371
/// If non-blocking reads are currently not possible, the `Waker`
@@ -296,7 +376,7 @@ impl<T: Evented> Watcher<T> {
296
376
// Lock the waker list.
297
377
let mut readers = self . entry . readers . lock ( ) . unwrap ( ) ;
298
378
if readers. ready {
299
- return Poll :: Ready ( ( ) )
379
+ return Poll :: Ready ( ( ) ) ;
300
380
}
301
381
// Register the task if it isn't registered already.
302
382
if readers. wakers . iter ( ) . all ( |w| !w. will_wake ( cx. waker ( ) ) ) {
@@ -314,7 +394,7 @@ impl<T: Evented> Watcher<T> {
314
394
// Lock the waker list.
315
395
let mut writers = self . entry . writers . lock ( ) . unwrap ( ) ;
316
396
if writers. ready {
317
- return Poll :: Ready ( ( ) )
397
+ return Poll :: Ready ( ( ) ) ;
318
398
}
319
399
// Register the task if it isn't registered already.
320
400
if writers. wakers . iter ( ) . all ( |w| !w. will_wake ( cx. waker ( ) ) ) {
0 commit comments