@@ -183,7 +183,7 @@ pub struct IoHandle<T: Evented> {
183183 entry : Arc < Entry > ,
184184
185185 /// The I/O event source.
186- source : T ,
186+ source : Option < T > ,
187187}
188188
189189impl < T : Evented > IoHandle < T > {
@@ -196,13 +196,13 @@ impl<T: Evented> IoHandle<T> {
196196 entry : REACTOR
197197 . register ( & source)
198198 . expect ( "cannot register an I/O event source" ) ,
199- source,
199+ source : Some ( source ) ,
200200 }
201201 }
202202
203203 /// Returns a reference to the inner I/O event source.
204204 pub fn get_ref ( & self ) -> & T {
205- & self . source
205+ self . source . as_ref ( ) . unwrap ( )
206206 }
207207
208208 /// Polls the I/O handle for reading.
@@ -278,13 +278,26 @@ impl<T: Evented> IoHandle<T> {
278278
279279 Ok ( ( ) )
280280 }
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+ }
281292}
282293
283294impl < T : Evented > Drop for IoHandle < T > {
284295 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+ }
288301 }
289302}
290303
@@ -305,7 +318,7 @@ impl<T: Evented + std::io::Read + Unpin> AsyncRead for IoHandle<T> {
305318 ) -> Poll < io:: Result < usize > > {
306319 futures_core:: ready!( Pin :: new( & mut * self ) . poll_readable( cx) ?) ;
307320
308- match self . source . read ( buf) {
321+ match self . source . as_mut ( ) . unwrap ( ) . read ( buf) {
309322 Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
310323 self . clear_readable ( cx) ?;
311324 Poll :: Pending
@@ -326,7 +339,7 @@ where
326339 ) -> Poll < io:: Result < usize > > {
327340 futures_core:: ready!( Pin :: new( & mut * self ) . poll_readable( cx) ?) ;
328341
329- match ( & self . source ) . read ( buf) {
342+ match self . source . as_ref ( ) . unwrap ( ) . read ( buf) {
330343 Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
331344 self . clear_readable ( cx) ?;
332345 Poll :: Pending
@@ -344,7 +357,7 @@ impl<T: Evented + std::io::Write + Unpin> AsyncWrite for IoHandle<T> {
344357 ) -> Poll < io:: Result < usize > > {
345358 futures_core:: ready!( self . poll_writable( cx) ?) ;
346359
347- match self . source . write ( buf) {
360+ match self . source . as_mut ( ) . unwrap ( ) . write ( buf) {
348361 Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
349362 self . clear_writable ( cx) ?;
350363 Poll :: Pending
@@ -356,7 +369,7 @@ impl<T: Evented + std::io::Write + Unpin> AsyncWrite for IoHandle<T> {
356369 fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
357370 futures_core:: ready!( self . poll_writable( cx) ?) ;
358371
359- match self . source . flush ( ) {
372+ match self . source . as_mut ( ) . unwrap ( ) . flush ( ) {
360373 Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
361374 self . clear_writable ( cx) ?;
362375 Poll :: Pending
@@ -381,7 +394,7 @@ where
381394 ) -> Poll < io:: Result < usize > > {
382395 futures_core:: ready!( self . poll_writable( cx) ?) ;
383396
384- match ( & self . source ) . write ( buf) {
397+ match self . get_ref ( ) . write ( buf) {
385398 Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
386399 self . clear_writable ( cx) ?;
387400 Poll :: Pending
@@ -393,7 +406,7 @@ where
393406 fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
394407 futures_core:: ready!( self . poll_writable( cx) ?) ;
395408
396- match ( & self . source ) . flush ( ) {
409+ match self . get_ref ( ) . flush ( ) {
397410 Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => {
398411 self . clear_writable ( cx) ?;
399412 Poll :: Pending
0 commit comments