-
Notifications
You must be signed in to change notification settings - Fork 666
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
Relax lifetime requirements for PollFd::new #2134
Conversation
f6b02bb
to
934d394
Compare
cc @SteveLauC |
934d394
to
a775d47
Compare
I think the argument should not be a reference at all, there is |
a775d47
to
c3262e6
Compare
Kinda think we are facing this issue everywhere, here is also a thread discussing this stuff: #2115 |
Actually, this is subtly wrong. And it's probably wrong for all constructors. If Now that I've reflected on this, the original approach of only taking a reference was a pretty good approach, because usually a |
@MaxVerevkin Here's an example that demonstrates why /// ```compile_fail,E0597
/// # use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
/// # use nix::{
/// # poll::{PollFd, PollFlags, poll},
/// # unistd::{pipe, read}
/// # };
/// let pfd = {
/// let (r, w) = pipe().unwrap();
/// let r = unsafe { OwnedFd::from_raw_fd(r) };
/// PollFd::new(r, PollFlags::POLLIN)
/// };
/// let mut fds = [pfd];
/// poll(&mut fds, -1).unwrap();
/// let mut buf = [0u8; 80];
/// read(pfd.as_fd().as_raw_fd(), &mut buf[..]);
/// ``` So either we must take by reference, or else the struct should include an |
Good observation. Yeah, by-value only make sense for
Please no :) By the way, don't know if it deserves its own issue. How do you reuse the same fd buffer with this new API? If I have a |
c3262e6
to
ff41615
Compare
Ok, I just pushed changes that restore the reference, and also rewords @SteveLauC 's explanatory comment a bit. |
This doesn't work either: let mut fds = Vec::new();
{
let file = std::fs::File::open("/etc/hostname").unwrap();
fds.push(PollFd::<'static>::new(&file, PollFlags::POLLIN));
drop(file);
}
poll(&mut fds, -1).unwrap(); This compiles because |
Ouch. That's a big problem. Maybe we should change |
That's definitely a solution. I wanted to propose it but I wasn't sure if it's a good one. |
38cc5b4
to
f542e13
Compare
This approach LGTM |
f542e13
to
818550b
Compare
&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.
818550b
to
c9c7d91
Compare
Fixes #2118