Skip to content

Commit 38cc5b4

Browse files
committed
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.
1 parent ff41615 commit 38cc5b4

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
55

66
## [Unreleased] - ReleaseDate
77

8-
### Fixed
8+
### Changed
99

10-
- Relaxed lifetime requirements for `PollFd::new`.
10+
- `PollFd::new` now takes a `BorrowedFd` argument, with relaxed lifetime
11+
requirements relative to the previous version.
1112
([#2134](https://github.com/nix-rust/nix/pull/2134))
1213

1314
## [0.27.1] - 2023-08-28

src/poll.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,27 @@ impl<'fd> PollFd<'fd> {
3232
/// # };
3333
/// let (r, w) = pipe().unwrap();
3434
/// let r = unsafe { OwnedFd::from_raw_fd(r) };
35-
/// let pfd = PollFd::new(&r.as_fd(), PollFlags::POLLIN);
35+
/// let pfd = PollFd::new(r.as_fd(), PollFlags::POLLIN);
3636
/// let mut fds = [pfd];
3737
/// poll(&mut fds, -1).unwrap();
3838
/// let mut buf = [0u8; 80];
3939
/// read(r.as_raw_fd(), &mut buf[..]);
4040
/// ```
41-
// Unlike I/O functions, constructors like this must take `AsFd` by
42-
// reference. Otherwise, an `OwnedFd` argument would be dropped at the end
43-
// of the method, leaving the structure referencing a closed file
44-
// descriptor.
45-
// Different from other I/O-safe interfaces, here, we have to take `AsFd`
46-
// by reference to prevent the case where the `fd` is closed but it is
47-
// still in use. For example:
41+
// Unlike I/O functions, constructors like this must take `BorrowedFd`
42+
// instead of AsFd or &AsFd. Otherwise, an `OwnedFd` argument would be
43+
// dropped at the end of the method, leaving the structure referencing a
44+
// closed file descriptor. For example:
4845
//
4946
// ```rust
5047
// let (r, _) = pipe().unwrap();
5148
// let reader: OwnedFd = unsafe { OwnedFd::from_raw_fd(r) };
5249
// let pollfd = PollFd::new(reader, flag); // Drops the OwnedFd
5350
// // Do something with `pollfd`, which uses the CLOSED fd.
5451
// ```
55-
pub fn new<Fd: AsFd + 'fd>(fd: &Fd, events: PollFlags) -> PollFd<'fd> {
52+
pub fn new(fd: BorrowedFd<'fd>, events: PollFlags) -> PollFd<'fd> {
5653
PollFd {
5754
pollfd: libc::pollfd {
58-
fd: fd.as_fd().as_raw_fd(),
55+
fd: fd.as_raw_fd(),
5956
events: events.bits(),
6057
revents: PollFlags::empty().bits(),
6158
},

test/test_poll.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use nix::{
33
poll::{poll, PollFd, PollFlags},
44
unistd::{close, pipe, write},
55
};
6-
use std::os::unix::io::{BorrowedFd, FromRawFd, OwnedFd};
6+
use std::os::{
7+
fd::AsFd,
8+
unix::io::{BorrowedFd, FromRawFd, OwnedFd}
9+
};
710

811
macro_rules! loop_while_eintr {
912
($poll_expr: expr) => {
@@ -21,7 +24,7 @@ macro_rules! loop_while_eintr {
2124
fn test_poll() {
2225
let (r, w) = pipe().unwrap();
2326
let r = unsafe { OwnedFd::from_raw_fd(r) };
24-
let mut fds = [PollFd::new(&r, PollFlags::POLLIN)];
27+
let mut fds = [PollFd::new(r.as_fd(), PollFlags::POLLIN)];
2528

2629
// Poll an idle pipe. Should timeout
2730
let nfds = loop_while_eintr!(poll(&mut fds, 100));
@@ -55,7 +58,7 @@ fn test_ppoll() {
5558
let timeout = TimeSpec::milliseconds(1);
5659
let (r, w) = pipe().unwrap();
5760
let r = unsafe { OwnedFd::from_raw_fd(r) };
58-
let mut fds = [PollFd::new(&r, PollFlags::POLLIN)];
61+
let mut fds = [PollFd::new(r.as_fd(), PollFlags::POLLIN)];
5962

6063
// Poll an idle pipe. Should timeout
6164
let sigset = SigSet::empty();
@@ -75,7 +78,7 @@ fn test_ppoll() {
7578
#[test]
7679
fn test_pollfd_events() {
7780
let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
78-
let mut pfd = PollFd::new(&fd_zero, PollFlags::POLLIN);
81+
let mut pfd = PollFd::new(fd_zero.as_fd(), PollFlags::POLLIN);
7982
assert_eq!(pfd.events(), PollFlags::POLLIN);
8083
pfd.set_events(PollFlags::POLLOUT);
8184
assert_eq!(pfd.events(), PollFlags::POLLOUT);

0 commit comments

Comments
 (0)