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

extend AsyncFdReadyGuard method rettypes lifetimes #4267

Merged
merged 1 commit into from Nov 23, 2021
Merged

extend AsyncFdReadyGuard method rettypes lifetimes #4267

merged 1 commit into from Nov 23, 2021

Conversation

axelf4
Copy link
Contributor

@axelf4 axelf4 commented Nov 22, 2021

Motivation

The implicit elided lifetimes of the AsyncFd references in return types of methods on AsyncFdReadyGuard resolved to that of &self, which is needlessly small.

Solution

Change the lifetimes of the references. Should not be a breaking change thanks to subtyping.


On a parallel note, I would also like to get an opinion on whether this pull request should be made to include a new into_try_io() method on AsyncFdReadyMutGuard:

pub fn into_try_io<R>(
    self,
    f: impl FnOnce(&'a mut Inner) -> io::Result<R>,
) -> Result<io::Result<R>, TryIoError> {
    let AsyncFdReadyMutGuard {
        async_fd,
        mut event,
    } = self;
    let result = f(async_fd.inner.as_mut().unwrap());

    match result {
        Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
            if let Some(event) = event.take() {
                async_fd.registration.clear_readiness(event);
            }
            Err(TryIoError(()))
        }
        result => return Ok(result),
    }
}

since the currently existing try_io() method is limiting when writing streaming streams, as the result of the closure cannot reference anything that outlives the guard handle. One example where this perhaps would have been useful is in the implementation of the capture-stream feature of the pcap crate.

The implicit elided lifetimes of the `AsyncFd` references in return
types of methods on `AsyncFdReadyGuard` resolved to that of `&self`.
However that lifetime is smaller than `'a` since `self` contains an `&'a
AsyncFd` reference. This will not change so the change also does not
lessen future proofing.
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-io Module: tokio/io labels Nov 22, 2021
@Darksonn
Copy link
Contributor

Note that we cannot make an analogous change for AsyncFdReadyGuardMut because of the uniqueness requirement of mutable references.

@Darksonn Darksonn merged commit a77b2fb into tokio-rs:master Nov 23, 2021
@axelf4 axelf4 deleted the extend-asyncfdreadyguard-lifetimes branch November 23, 2021 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-io Module: tokio/io
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants