1
1
use nix:: {
2
2
errno:: Errno ,
3
3
poll:: { poll, PollFd , PollFlags } ,
4
- unistd:: { pipe, write} ,
4
+ unistd:: { close , pipe, write} ,
5
5
} ;
6
+ use std:: os:: unix:: io:: { BorrowedFd , FromRawFd , OwnedFd } ;
6
7
7
8
macro_rules! loop_while_eintr {
8
9
( $poll_expr: expr) => {
@@ -19,7 +20,8 @@ macro_rules! loop_while_eintr {
19
20
#[ test]
20
21
fn test_poll ( ) {
21
22
let ( r, w) = pipe ( ) . unwrap ( ) ;
22
- let mut fds = [ PollFd :: new ( r, PollFlags :: POLLIN ) ] ;
23
+ let r = unsafe { OwnedFd :: from_raw_fd ( r) } ;
24
+ let mut fds = [ PollFd :: new ( & r, PollFlags :: POLLIN ) ] ;
23
25
24
26
// Poll an idle pipe. Should timeout
25
27
let nfds = loop_while_eintr ! ( poll( & mut fds, 100 ) ) ;
@@ -32,6 +34,7 @@ fn test_poll() {
32
34
let nfds = poll ( & mut fds, 100 ) . unwrap ( ) ;
33
35
assert_eq ! ( nfds, 1 ) ;
34
36
assert ! ( fds[ 0 ] . revents( ) . unwrap( ) . contains( PollFlags :: POLLIN ) ) ;
37
+ close ( w) . unwrap ( ) ;
35
38
}
36
39
37
40
// ppoll(2) is the same as poll except for how it handles timeouts and signals.
@@ -51,7 +54,8 @@ fn test_ppoll() {
51
54
52
55
let timeout = TimeSpec :: milliseconds ( 1 ) ;
53
56
let ( r, w) = pipe ( ) . unwrap ( ) ;
54
- let mut fds = [ PollFd :: new ( r, PollFlags :: POLLIN ) ] ;
57
+ let r = unsafe { OwnedFd :: from_raw_fd ( r) } ;
58
+ let mut fds = [ PollFd :: new ( & r, PollFlags :: POLLIN ) ] ;
55
59
56
60
// Poll an idle pipe. Should timeout
57
61
let sigset = SigSet :: empty ( ) ;
@@ -65,19 +69,13 @@ fn test_ppoll() {
65
69
let nfds = ppoll ( & mut fds, Some ( timeout) , None ) . unwrap ( ) ;
66
70
assert_eq ! ( nfds, 1 ) ;
67
71
assert ! ( fds[ 0 ] . revents( ) . unwrap( ) . contains( PollFlags :: POLLIN ) ) ;
68
- }
69
-
70
- #[ test]
71
- fn test_pollfd_fd ( ) {
72
- use std:: os:: unix:: io:: AsRawFd ;
73
-
74
- let pfd = PollFd :: new ( 0x1234 , PollFlags :: empty ( ) ) ;
75
- assert_eq ! ( pfd. as_raw_fd( ) , 0x1234 ) ;
72
+ close ( w) . unwrap ( ) ;
76
73
}
77
74
78
75
#[ test]
79
76
fn test_pollfd_events ( ) {
80
- let mut pfd = PollFd :: new ( -1 , PollFlags :: POLLIN ) ;
77
+ let fd_zero = unsafe { BorrowedFd :: borrow_raw ( 0 ) } ;
78
+ let mut pfd = PollFd :: new ( & fd_zero, PollFlags :: POLLIN ) ;
81
79
assert_eq ! ( pfd. events( ) , PollFlags :: POLLIN ) ;
82
80
pfd. set_events ( PollFlags :: POLLOUT ) ;
83
81
assert_eq ! ( pfd. events( ) , PollFlags :: POLLOUT ) ;
0 commit comments