-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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: add get_{ref,mut} methods to AsyncFdReadyGuard and AsyncFdReadyMutGuard. #3807
Conversation
…tGuard. The only way to use AsyncFdReadyMutGuard currently is through try_io, since it is impossible to access the underlying object. In some situations, it is desirable to use clear_ready and retain_ready directly instead of try_io. Enable this by adding get_ref and get_mut methods.
One option is to also add an |
Yes. So, would you rather see four methods I personally doubt that accessing the |
We can add all four. Even if it isn't very useful right now, it might get new methods in the future where it is useful. |
tokio/src/io/async_fd.rs
Outdated
/// Returns a shared reference to the inner [`AsyncFd`]. | ||
pub fn get_ref(&self) -> &AsyncFd<Inner> { | ||
self.async_fd | ||
} | ||
|
||
/// Returns a shared reference to the backing object of the inner [`AsyncFd`]. | ||
pub fn get_inner(&self) -> &Inner { | ||
self.get_ref().get_ref() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move the methods to the bottom of the impl block so they don't appear at the top of the documentation?
…m of the impl block.
The only way to use
AsyncFdReadyMutGuard
currently is throughtry_io
, since it is impossible to access the underlying object. In some situations, it is desirable to useclear_ready
andretain_ready
directly instead oftry_io
. Enable this by addingget_ref
andget_mut
methods.Motivation
When using
AsyncFd
with an I/O object that does not communicate readiness throughErrorKind::WouldBlock
, code usingtry_io
becomes hard to read. In the following example, thenext()
method returnsNone
when no new data is ready.As seen here, you have to explicitly convert
None
to aWouldBlock
error intry_io
, only to convert that error back intoPending
afterwards. This is confusing to read.Solution
By adding
AsyncFdReadyMutGuard::get_mut
, this can be expressed in a more straight-forward way:The
get_ref
methods are added for consistency.Questions
Should we return a reference to
AsyncFd<T>
orT
?try_io
usesAsyncFd<T>
in its argument, so the proposed solution is consistent. On the other hand, in probably all cases, this requires writingguard.get_mut().get_mut()
.