Skip to content

Commit 82bef00

Browse files
authored
io: minor tweaks to AsyncFd (#5932)
1 parent 40633fc commit 82bef00

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

tokio/src/io/async_fd.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ use std::{task::Context, task::Poll};
176176
/// [`AsyncWrite`]: trait@crate::io::AsyncWrite
177177
pub struct AsyncFd<T: AsRawFd> {
178178
registration: Registration,
179+
// The inner value is always present. the Option is required for `drop` and `into_inner`.
180+
// In all other methods `unwrap` is valid, and will never panic.
179181
inner: Option<T>,
180182
}
181183

@@ -271,13 +273,12 @@ impl<T: AsRawFd> AsyncFd<T> {
271273
}
272274

273275
fn take_inner(&mut self) -> Option<T> {
274-
let fd = self.inner.as_ref().map(AsRawFd::as_raw_fd);
276+
let inner = self.inner.take()?;
277+
let fd = inner.as_raw_fd();
275278

276-
if let Some(fd) = fd {
277-
let _ = self.registration.deregister(&mut SourceFd(&fd));
278-
}
279+
let _ = self.registration.deregister(&mut SourceFd(&fd));
279280

280-
self.inner.take()
281+
Some(inner)
281282
}
282283

283284
/// Deregisters this file descriptor and returns ownership of the backing
@@ -319,11 +320,10 @@ impl<T: AsRawFd> AsyncFd<T> {
319320
) -> Poll<io::Result<AsyncFdReadyGuard<'a, T>>> {
320321
let event = ready!(self.registration.poll_read_ready(cx))?;
321322

322-
Ok(AsyncFdReadyGuard {
323+
Poll::Ready(Ok(AsyncFdReadyGuard {
323324
async_fd: self,
324325
event: Some(event),
325-
})
326-
.into()
326+
}))
327327
}
328328

329329
/// Polls for read readiness.
@@ -357,11 +357,10 @@ impl<T: AsRawFd> AsyncFd<T> {
357357
) -> Poll<io::Result<AsyncFdReadyMutGuard<'a, T>>> {
358358
let event = ready!(self.registration.poll_read_ready(cx))?;
359359

360-
Ok(AsyncFdReadyMutGuard {
360+
Poll::Ready(Ok(AsyncFdReadyMutGuard {
361361
async_fd: self,
362362
event: Some(event),
363-
})
364-
.into()
363+
}))
365364
}
366365

367366
/// Polls for write readiness.
@@ -397,11 +396,10 @@ impl<T: AsRawFd> AsyncFd<T> {
397396
) -> Poll<io::Result<AsyncFdReadyGuard<'a, T>>> {
398397
let event = ready!(self.registration.poll_write_ready(cx))?;
399398

400-
Ok(AsyncFdReadyGuard {
399+
Poll::Ready(Ok(AsyncFdReadyGuard {
401400
async_fd: self,
402401
event: Some(event),
403-
})
404-
.into()
402+
}))
405403
}
406404

407405
/// Polls for write readiness.
@@ -435,11 +433,10 @@ impl<T: AsRawFd> AsyncFd<T> {
435433
) -> Poll<io::Result<AsyncFdReadyMutGuard<'a, T>>> {
436434
let event = ready!(self.registration.poll_write_ready(cx))?;
437435

438-
Ok(AsyncFdReadyMutGuard {
436+
Poll::Ready(Ok(AsyncFdReadyMutGuard {
439437
async_fd: self,
440438
event: Some(event),
441-
})
442-
.into()
439+
}))
443440
}
444441

445442
/// Waits for any of the requested ready states, returning a
@@ -1013,14 +1010,11 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> {
10131010
) -> Result<io::Result<R>, TryIoError> {
10141011
let result = f(self.async_fd);
10151012

1016-
if let Err(e) = result.as_ref() {
1017-
if e.kind() == io::ErrorKind::WouldBlock {
1013+
match result {
1014+
Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
10181015
self.clear_ready();
1016+
Err(TryIoError(()))
10191017
}
1020-
}
1021-
1022-
match result {
1023-
Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError(())),
10241018
result => Ok(result),
10251019
}
10261020
}
@@ -1193,14 +1187,11 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> {
11931187
) -> Result<io::Result<R>, TryIoError> {
11941188
let result = f(self.async_fd);
11951189

1196-
if let Err(e) = result.as_ref() {
1197-
if e.kind() == io::ErrorKind::WouldBlock {
1190+
match result {
1191+
Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
11981192
self.clear_ready();
1193+
Err(TryIoError(()))
11991194
}
1200-
}
1201-
1202-
match result {
1203-
Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError(())),
12041195
result => Ok(result),
12051196
}
12061197
}

0 commit comments

Comments
 (0)