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

PollFd::new requires too restrictive lifetime #2118

Closed
MaxVerevkin opened this issue Aug 29, 2023 · 2 comments · Fixed by #2134
Closed

PollFd::new requires too restrictive lifetime #2118

MaxVerevkin opened this issue Aug 29, 2023 · 2 comments · Fixed by #2134

Comments

@MaxVerevkin
Copy link

I was trying to do

if let Some(cmd_fd) = state.status_cmd_fd() {
    fds.push(PollFd::new(&cmd_fd, PollFlags::POLLIN));
}

where status_cmd_fd() returns Option<BorrowedFd<'_>>. This code does not compile because the reference &cmd_fd does not live long enough, but the lifetime of the reference should be irrelevant.


Current signature of PollFd::new is

pub fn new<Fd: AsFd>(fd: &'fd Fd, events: PollFlags) -> PollFd<'fd>

I think it should probably be

pub fn new<Fd: AsFd + 'fd>(fd: Fd, events: PollFlags) -> PollFd<'fd>
@asomers
Copy link
Member

asomers commented Sep 23, 2023

Duplicate of #2130

@asomers asomers marked this as a duplicate of #2130 Sep 23, 2023
@asomers asomers closed this as completed Sep 23, 2023
@asomers
Copy link
Member

asomers commented Sep 23, 2023

Oops; I read this too fast. It's not a duplicate.

@asomers asomers reopened this Sep 23, 2023
asomers added a commit that referenced this issue Sep 23, 2023
asomers added a commit that referenced this issue Sep 23, 2023
asomers added a commit that referenced this issue Sep 23, 2023
asomers added a commit that referenced this issue Sep 23, 2023
asomers added a commit that referenced this issue Sep 29, 2023
asomers added a commit that referenced this issue Sep 30, 2023
github-merge-queue bot pushed a commit that referenced this issue Oct 1, 2023
* Relax lifetime requirements for PollFd::new

Fixes #2118

* Take BorrowedFd as the argument for PollFd::new

&AsFd didn't work because there are 'static types, like std::fs::File,
which implement AsFd.  The created BorrowedFd type within the
PollFd::new method would have a very brief lifetime, but the PhantomData
would capture the lifetime of the std::fs::File.  Taking BorrowFd<'fd>
argument makes the lifetime explicit.

* fix legacy comment

---------

Co-authored-by: Steve Lau <stevelauc@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants