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