@@ -2,7 +2,7 @@ use crate::errno::Errno;
2
2
use crate :: Result ;
3
3
use libc:: { self , c_int} ;
4
4
use std:: mem;
5
- use std:: os:: unix:: io:: { FromRawFd , RawFd , OwnedFd , AsFd , AsRawFd } ;
5
+ use std:: os:: unix:: io:: { AsFd , AsRawFd , FromRawFd , OwnedFd , RawFd } ;
6
6
7
7
libc_bitflags ! (
8
8
pub struct EpollFlags : c_int {
@@ -78,22 +78,22 @@ impl EpollEvent {
78
78
/// # fn main() -> nix::Result<()> {
79
79
/// const DATA: u64 = 17;
80
80
/// const MILLIS: u64 = 100;
81
- ///
81
+ ///
82
82
/// // Create epoll
83
83
/// let epoll = Epoll::new(EpollCreateFlags::empty())?;
84
- ///
84
+ ///
85
85
/// // Create eventfd & Add event
86
- /// let eventfd = unsafe { OwnedFd::from_raw_fd( eventfd(0, EfdFlags::empty())?) };
86
+ /// let eventfd = unsafe { eventfd(0, EfdFlags::empty())? };
87
87
/// epoll.add(&eventfd, EpollEvent::new(EpollFlags::EPOLLIN,DATA))?;
88
- ///
88
+ ///
89
89
/// // Arm eventfd & Time wait
90
90
/// write(eventfd.as_raw_fd(), &1u64.to_ne_bytes())?;
91
91
/// let now = Instant::now();
92
- ///
92
+ ///
93
93
/// // Wait on event
94
94
/// let mut events = [EpollEvent::empty()];
95
95
/// epoll.wait(&mut events, MILLIS as isize)?;
96
- ///
96
+ ///
97
97
/// // Assert data correct & timeout didn't occur
98
98
/// assert_eq!(events[0].data(), DATA);
99
99
/// assert!(now.elapsed() < Duration::from_millis(MILLIS));
@@ -104,7 +104,7 @@ impl EpollEvent {
104
104
pub struct Epoll ( pub OwnedFd ) ;
105
105
impl Epoll {
106
106
/// Creates a new epoll instance and returns a file descriptor referring to that instance.
107
- ///
107
+ ///
108
108
/// [`epoll_create1`](https://man7.org/linux/man-pages/man2/epoll_create1.2.html).
109
109
pub fn new ( flags : EpollCreateFlags ) -> Result < Self > {
110
110
let res = unsafe { libc:: epoll_create1 ( flags. bits ( ) ) } ;
@@ -113,30 +113,38 @@ impl Epoll {
113
113
Ok ( Self ( owned_fd) )
114
114
}
115
115
/// Add an entry to the interest list of the epoll file descriptor for
116
- /// specified in events.
117
- ///
116
+ /// specified in events.
117
+ ///
118
118
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_ADD`.
119
119
pub fn add < Fd : AsFd > ( & self , fd : Fd , mut event : EpollEvent ) -> Result < ( ) > {
120
- self . epoll_ctl ( EpollOp :: EpollCtlAdd , fd, & mut event)
120
+ self . epoll_ctl ( EpollOp :: EpollCtlAdd , fd, & mut event)
121
121
}
122
122
/// Remove (deregister) the target file descriptor `fd` from the interest list.
123
- ///
123
+ ///
124
124
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_DEL` .
125
125
pub fn delete < Fd : AsFd > ( & self , fd : Fd ) -> Result < ( ) > {
126
- self . epoll_ctl ( EpollOp :: EpollCtlDel , fd, None )
126
+ self . epoll_ctl ( EpollOp :: EpollCtlDel , fd, None )
127
127
}
128
128
/// Change the settings associated with `fd` in the interest list to the new settings specified
129
129
/// in `event`.
130
- ///
130
+ ///
131
131
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_MOD`.
132
- pub fn modify < Fd : AsFd > ( & self , fd : Fd , event : & mut EpollEvent ) -> Result < ( ) > {
133
- self . epoll_ctl ( EpollOp :: EpollCtlMod , fd, event)
132
+ pub fn modify < Fd : AsFd > (
133
+ & self ,
134
+ fd : Fd ,
135
+ event : & mut EpollEvent ,
136
+ ) -> Result < ( ) > {
137
+ self . epoll_ctl ( EpollOp :: EpollCtlMod , fd, event)
134
138
}
135
139
/// Waits for I/O events, blocking the calling thread if no events are currently available.
136
140
/// (This can be thought of as fetching items from the ready list of the epoll instance.)
137
- ///
141
+ ///
138
142
/// [`epoll_wait`](https://man7.org/linux/man-pages/man2/epoll_wait.2.html)
139
- pub fn wait ( & self , events : & mut [ EpollEvent ] , timeout : isize ) -> Result < usize > {
143
+ pub fn wait (
144
+ & self ,
145
+ events : & mut [ EpollEvent ] ,
146
+ timeout : isize ,
147
+ ) -> Result < usize > {
140
148
let res = unsafe {
141
149
libc:: epoll_wait (
142
150
self . 0 . as_raw_fd ( ) ,
@@ -145,15 +153,15 @@ impl Epoll {
145
153
timeout as c_int ,
146
154
)
147
155
} ;
148
-
156
+
149
157
Errno :: result ( res) . map ( |r| r as usize )
150
158
}
151
159
/// This system call is used to add, modify, or remove entries in the interest list of the epoll
152
160
/// instance referred to by `self`. It requests that the operation `op` be performed for the
153
161
/// target file descriptor, `fd`.
154
- ///
162
+ ///
155
163
/// When possible prefer [`Epoll::add`], [`Epoll::delete`] and [`Epoll::modify`].
156
- ///
164
+ ///
157
165
/// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html)
158
166
fn epoll_ctl < ' a , Fd : AsFd , T > (
159
167
& self ,
@@ -165,9 +173,17 @@ impl Epoll {
165
173
T : Into < Option < & ' a mut EpollEvent > > ,
166
174
{
167
175
let event: Option < & mut EpollEvent > = event. into ( ) ;
168
- let ptr = event. map ( |x|& mut x. event as * mut libc:: epoll_event ) . unwrap_or ( std:: ptr:: null_mut ( ) ) ;
176
+ let ptr = event
177
+ . map ( |x| & mut x. event as * mut libc:: epoll_event )
178
+ . unwrap_or ( std:: ptr:: null_mut ( ) ) ;
169
179
unsafe {
170
- Errno :: result ( libc:: epoll_ctl ( self . 0 . as_raw_fd ( ) , op as c_int , fd. as_fd ( ) . as_raw_fd ( ) , ptr) ) . map ( drop)
180
+ Errno :: result ( libc:: epoll_ctl (
181
+ self . 0 . as_raw_fd ( ) ,
182
+ op as c_int ,
183
+ fd. as_fd ( ) . as_raw_fd ( ) ,
184
+ ptr,
185
+ ) )
186
+ . map ( drop)
171
187
}
172
188
}
173
189
}
@@ -231,4 +247,4 @@ pub fn epoll_wait(
231
247
} ;
232
248
233
249
Errno :: result ( res) . map ( |r| r as usize )
234
- }
250
+ }
0 commit comments