@@ -183,7 +183,7 @@ pub struct IoHandle<T: Evented> {
183
183
entry : Arc < Entry > ,
184
184
185
185
/// The I/O event source.
186
- source : T ,
186
+ source : Option < T > ,
187
187
}
188
188
189
189
impl < T : Evented > IoHandle < T > {
@@ -196,13 +196,13 @@ impl<T: Evented> IoHandle<T> {
196
196
entry : REACTOR
197
197
. register ( & source)
198
198
. expect ( "cannot register an I/O event source" ) ,
199
- source,
199
+ source : Some ( source ) ,
200
200
}
201
201
}
202
202
203
203
/// Returns a reference to the inner I/O event source.
204
204
pub fn get_ref ( & self ) -> & T {
205
- & self . source
205
+ self . source . as_ref ( ) . unwrap ( )
206
206
}
207
207
208
208
/// Polls the I/O handle for reading.
@@ -286,13 +286,26 @@ impl<T: Evented> IoHandle<T> {
286
286
287
287
Ok ( ( ) )
288
288
}
289
+
290
+ /// Deregisters and returns the inner I/O source.
291
+ ///
292
+ /// This method is typically used to convert `IoHandle`s to raw file descriptors/handles.
293
+ pub fn into_inner ( mut self ) -> T {
294
+ let source = self . source . take ( ) . unwrap ( ) ;
295
+ REACTOR
296
+ . deregister ( & source, & self . entry )
297
+ . expect ( "cannot deregister I/O event source" ) ;
298
+ source
299
+ }
289
300
}
290
301
291
302
impl < T : Evented > Drop for IoHandle < T > {
292
303
fn drop ( & mut self ) {
293
- REACTOR
294
- . deregister ( & self . source , & self . entry )
295
- . expect ( "cannot deregister I/O event source" ) ;
304
+ if let Some ( ref source) = self . source {
305
+ REACTOR
306
+ . deregister ( source, & self . entry )
307
+ . expect ( "cannot deregister I/O event source" ) ;
308
+ }
296
309
}
297
310
}
298
311
@@ -313,7 +326,7 @@ impl<T: Evented + std::io::Read + Unpin> AsyncRead for IoHandle<T> {
313
326
) -> Poll < io:: Result < usize > > {
314
327
futures_core:: ready!( Pin :: new( & mut * self ) . poll_readable( cx) ?) ;
315
328
316
- match self . source . read ( buf) {
329
+ match self . source . as_mut ( ) . unwrap ( ) . read ( buf) {
317
330
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
318
331
self . clear_readable ( cx) ?;
319
332
Poll :: Pending
@@ -334,7 +347,7 @@ where
334
347
) -> Poll < io:: Result < usize > > {
335
348
futures_core:: ready!( Pin :: new( & mut * self ) . poll_readable( cx) ?) ;
336
349
337
- match ( & self . source ) . read ( buf) {
350
+ match self . source . as_ref ( ) . unwrap ( ) . read ( buf) {
338
351
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
339
352
self . clear_readable ( cx) ?;
340
353
Poll :: Pending
@@ -352,7 +365,7 @@ impl<T: Evented + std::io::Write + Unpin> AsyncWrite for IoHandle<T> {
352
365
) -> Poll < io:: Result < usize > > {
353
366
futures_core:: ready!( self . poll_writable( cx) ?) ;
354
367
355
- match self . source . write ( buf) {
368
+ match self . source . as_mut ( ) . unwrap ( ) . write ( buf) {
356
369
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
357
370
self . clear_writable ( cx) ?;
358
371
Poll :: Pending
@@ -364,7 +377,7 @@ impl<T: Evented + std::io::Write + Unpin> AsyncWrite for IoHandle<T> {
364
377
fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
365
378
futures_core:: ready!( self . poll_writable( cx) ?) ;
366
379
367
- match self . source . flush ( ) {
380
+ match self . source . as_mut ( ) . unwrap ( ) . flush ( ) {
368
381
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
369
382
self . clear_writable ( cx) ?;
370
383
Poll :: Pending
@@ -389,7 +402,7 @@ where
389
402
) -> Poll < io:: Result < usize > > {
390
403
futures_core:: ready!( self . poll_writable( cx) ?) ;
391
404
392
- match ( & self . source ) . write ( buf) {
405
+ match self . get_ref ( ) . write ( buf) {
393
406
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
394
407
self . clear_writable ( cx) ?;
395
408
Poll :: Pending
@@ -401,7 +414,7 @@ where
401
414
fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
402
415
futures_core:: ready!( self . poll_writable( cx) ?) ;
403
416
404
- match ( & self . source ) . flush ( ) {
417
+ match self . get_ref ( ) . flush ( ) {
405
418
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
406
419
self . clear_writable ( cx) ?;
407
420
Poll :: Pending
0 commit comments