Skip to content

Commit

Permalink
io: minor tweaks to AsyncFd (#5932)
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Aug 14, 2023
1 parent 40633fc commit 82bef00
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions tokio/src/io/async_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ use std::{task::Context, task::Poll};
/// [`AsyncWrite`]: trait@crate::io::AsyncWrite
pub struct AsyncFd<T: AsRawFd> {
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<T>,
}

Expand Down Expand Up @@ -271,13 +273,12 @@ impl<T: AsRawFd> AsyncFd<T> {
}

fn take_inner(&mut self) -> Option<T> {
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
Expand Down Expand Up @@ -319,11 +320,10 @@ impl<T: AsRawFd> AsyncFd<T> {
) -> Poll<io::Result<AsyncFdReadyGuard<'a, T>>> {
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.
Expand Down Expand Up @@ -357,11 +357,10 @@ impl<T: AsRawFd> AsyncFd<T> {
) -> Poll<io::Result<AsyncFdReadyMutGuard<'a, T>>> {
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.
Expand Down Expand Up @@ -397,11 +396,10 @@ impl<T: AsRawFd> AsyncFd<T> {
) -> Poll<io::Result<AsyncFdReadyGuard<'a, T>>> {
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.
Expand Down Expand Up @@ -435,11 +433,10 @@ impl<T: AsRawFd> AsyncFd<T> {
) -> Poll<io::Result<AsyncFdReadyMutGuard<'a, T>>> {
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
Expand Down Expand Up @@ -1013,14 +1010,11 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> {
) -> Result<io::Result<R>, 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),
}
}
Expand Down Expand Up @@ -1193,14 +1187,11 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> {
) -> Result<io::Result<R>, 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),
}
}
Expand Down

0 comments on commit 82bef00

Please sign in to comment.