diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index db56359c5ad..ae8f5c6417c 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -176,6 +176,8 @@ use std::{task::Context, task::Poll}; /// [`AsyncWrite`]: trait@crate::io::AsyncWrite pub struct AsyncFd { registration: Registration, + // The inner value is always present. the Option is required for `drop` and `into_inner`. + // In all other methods `unwrap` is valid, and will never panic. inner: Option, } @@ -271,13 +273,12 @@ impl AsyncFd { } fn take_inner(&mut self) -> Option { - let fd = self.inner.as_ref().map(AsRawFd::as_raw_fd); + let inner = self.inner.take()?; + let fd = inner.as_raw_fd(); - if let Some(fd) = fd { - let _ = self.registration.deregister(&mut SourceFd(&fd)); - } + let _ = self.registration.deregister(&mut SourceFd(&fd)); - self.inner.take() + Some(inner) } /// Deregisters this file descriptor and returns ownership of the backing @@ -319,11 +320,10 @@ impl AsyncFd { ) -> Poll>> { let event = ready!(self.registration.poll_read_ready(cx))?; - Ok(AsyncFdReadyGuard { + Poll::Ready(Ok(AsyncFdReadyGuard { async_fd: self, event: Some(event), - }) - .into() + })) } /// Polls for read readiness. @@ -357,11 +357,10 @@ impl AsyncFd { ) -> Poll>> { let event = ready!(self.registration.poll_read_ready(cx))?; - Ok(AsyncFdReadyMutGuard { + Poll::Ready(Ok(AsyncFdReadyMutGuard { async_fd: self, event: Some(event), - }) - .into() + })) } /// Polls for write readiness. @@ -397,11 +396,10 @@ impl AsyncFd { ) -> Poll>> { let event = ready!(self.registration.poll_write_ready(cx))?; - Ok(AsyncFdReadyGuard { + Poll::Ready(Ok(AsyncFdReadyGuard { async_fd: self, event: Some(event), - }) - .into() + })) } /// Polls for write readiness. @@ -435,11 +433,10 @@ impl AsyncFd { ) -> Poll>> { let event = ready!(self.registration.poll_write_ready(cx))?; - Ok(AsyncFdReadyMutGuard { + Poll::Ready(Ok(AsyncFdReadyMutGuard { async_fd: self, event: Some(event), - }) - .into() + })) } /// Waits for any of the requested ready states, returning a @@ -1013,14 +1010,11 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { ) -> Result, TryIoError> { let result = f(self.async_fd); - if let Err(e) = result.as_ref() { - if e.kind() == io::ErrorKind::WouldBlock { + match result { + Err(err) if err.kind() == io::ErrorKind::WouldBlock => { self.clear_ready(); + Err(TryIoError(())) } - } - - match result { - Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError(())), result => Ok(result), } } @@ -1193,14 +1187,11 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> { ) -> Result, TryIoError> { let result = f(self.async_fd); - if let Err(e) = result.as_ref() { - if e.kind() == io::ErrorKind::WouldBlock { + match result { + Err(err) if err.kind() == io::ErrorKind::WouldBlock => { self.clear_ready(); + Err(TryIoError(())) } - } - - match result { - Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError(())), result => Ok(result), } }