Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: AsyncFd minor tweaks #5932

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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