@@ -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.
@@ -278,13 +278,25 @@ impl<T: Evented> IoHandle<T> {
278
278
279
279
Ok ( ( ) )
280
280
}
281
+
282
+ /// Deregister and return the I/O source
283
+ ///
284
+ /// This method is to support IntoRawFd in struct that uses IoHandle
285
+ pub fn into_inner ( mut self ) -> T {
286
+ let source = self . source . take ( ) . unwrap ( ) ;
287
+ REACTOR . deregister ( & source, & self . entry )
288
+ . expect ( "cannot deregister I/O event source" ) ;
289
+ source
290
+ }
281
291
}
282
292
283
293
impl < T : Evented > Drop for IoHandle < T > {
284
294
fn drop ( & mut self ) {
285
- REACTOR
286
- . deregister ( & self . source , & self . entry )
287
- . expect ( "cannot deregister I/O event source" ) ;
295
+ if let Some ( ref source) = self . source {
296
+ REACTOR
297
+ . deregister ( source, & self . entry )
298
+ . expect ( "cannot deregister I/O event source" ) ;
299
+ }
288
300
}
289
301
}
290
302
@@ -305,7 +317,7 @@ impl<T: Evented + std::io::Read + Unpin> AsyncRead for IoHandle<T> {
305
317
) -> Poll < io:: Result < usize > > {
306
318
futures_core:: ready!( Pin :: new( & mut * self ) . poll_readable( cx) ?) ;
307
319
308
- match self . source . read ( buf) {
320
+ match self . source . as_mut ( ) . unwrap ( ) . read ( buf) {
309
321
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
310
322
self . clear_readable ( cx) ?;
311
323
Poll :: Pending
@@ -326,7 +338,7 @@ where
326
338
) -> Poll < io:: Result < usize > > {
327
339
futures_core:: ready!( Pin :: new( & mut * self ) . poll_readable( cx) ?) ;
328
340
329
- match ( & self . source ) . read ( buf) {
341
+ match self . source . as_ref ( ) . unwrap ( ) . read ( buf) {
330
342
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
331
343
self . clear_readable ( cx) ?;
332
344
Poll :: Pending
@@ -344,7 +356,7 @@ impl<T: Evented + std::io::Write + Unpin> AsyncWrite for IoHandle<T> {
344
356
) -> Poll < io:: Result < usize > > {
345
357
futures_core:: ready!( self . poll_writable( cx) ?) ;
346
358
347
- match self . source . write ( buf) {
359
+ match self . source . as_mut ( ) . unwrap ( ) . write ( buf) {
348
360
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
349
361
self . clear_writable ( cx) ?;
350
362
Poll :: Pending
@@ -356,7 +368,7 @@ impl<T: Evented + std::io::Write + Unpin> AsyncWrite for IoHandle<T> {
356
368
fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
357
369
futures_core:: ready!( self . poll_writable( cx) ?) ;
358
370
359
- match self . source . flush ( ) {
371
+ match self . source . as_mut ( ) . unwrap ( ) . flush ( ) {
360
372
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
361
373
self . clear_writable ( cx) ?;
362
374
Poll :: Pending
@@ -381,7 +393,7 @@ where
381
393
) -> Poll < io:: Result < usize > > {
382
394
futures_core:: ready!( self . poll_writable( cx) ?) ;
383
395
384
- match ( & self . source ) . write ( buf) {
396
+ match self . get_ref ( ) . write ( buf) {
385
397
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
386
398
self . clear_writable ( cx) ?;
387
399
Poll :: Pending
@@ -393,7 +405,7 @@ where
393
405
fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
394
406
futures_core:: ready!( self . poll_writable( cx) ?) ;
395
407
396
- match ( & self . source ) . flush ( ) {
408
+ match self . get_ref ( ) . flush ( ) {
397
409
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
398
410
self . clear_writable ( cx) ?;
399
411
Poll :: Pending
0 commit comments