17
17
//! accept, bind_v4, listen, socket, AddressFamily, Ipv4Addr, Protocol, SocketAddrV4,
18
18
//! SocketType,
19
19
//! };
20
+ //! use std::collections::HashMap;
20
21
//! use std::os::unix::io::AsRawFd;
21
22
//!
22
23
//! // Create a socket and listen on it.
26
27
//!
27
28
//! // Create an epoll object. Using `Owning` here means the epoll object will
28
29
//! // take ownership of the file descriptors registered with it.
29
- //! let epoll = Epoll::new(epoll::CreateFlags::CLOEXEC, epoll::Owning::new())?;
30
- //!
31
- //! // Remember the socket raw fd, which we use for comparisons only.
32
- //! let raw_listen_sock = listen_sock.as_fd().as_raw_fd();
30
+ //! let epoll = Epoll::new(epoll::CreateFlags::CLOEXEC)?;
33
31
//!
34
32
//! // Register the socket with the epoll object.
35
- //! epoll.add(listen_sock, epoll::EventFlags::IN)?;
33
+ //! epoll.add(&listen_sock, 1, epoll::EventFlags::IN)?;
34
+ //!
35
+ //! // Keep track of the sockets we've opened.
36
+ //! let mut next_id = 2;
37
+ //! let mut sockets = HashMap::new();
36
38
//!
37
39
//! // Process events.
38
40
//! let mut event_list = epoll::EventVec::with_capacity(4);
39
41
//! loop {
40
42
//! epoll.wait(&mut event_list, -1)?;
41
43
//! for (_event_flags, target) in &event_list {
42
- //! if target.as_raw_fd() == raw_listen_sock {
44
+ //! if target == 1 {
43
45
//! // Accept a new connection, set it to non-blocking, and
44
46
//! // register to be notified when it's ready to write to.
45
- //! let conn_sock = accept(&*target )?;
47
+ //! let conn_sock = accept(&listen_sock )?;
46
48
//! ioctl_fionbio(&conn_sock, true)?;
47
- //! epoll.add(conn_sock, epoll::EventFlags::OUT | epoll::EventFlags::ET)?;
49
+ //! epoll.add(&conn_sock, next_id, epoll::EventFlags::OUT | epoll::EventFlags::ET)?;
50
+ //!
51
+ //! // Keep track of the socket.
52
+ //! sockets.insert(next_id, conn_sock);
53
+ //! next_id += 1;
48
54
//! } else {
49
55
//! // Write a message to the stream and then unregister it.
50
- //! write(&*target, b"hello\n")?;
51
- //! let _ = epoll.del(target)?;
56
+ //! let target = sockets.remove(&target).unwrap();
57
+ //! write(&target, b"hello\n")?;
58
+ //! let _ = epoll.del(&target)?;
52
59
//! }
53
60
//! }
54
61
//! }
@@ -66,11 +73,7 @@ use crate::io;
66
73
use alloc:: vec:: Vec ;
67
74
use bitflags:: bitflags;
68
75
use core:: convert:: TryInto ;
69
- use core:: marker:: PhantomData ;
70
- use core:: ptr:: { null, null_mut} ;
71
-
72
- #[ doc( inline) ]
73
- pub use crate :: io:: context:: * ;
76
+ use core:: ptr:: null_mut;
74
77
75
78
bitflags ! {
76
79
/// `EPOLL_*` for use with [`Epoll::new`].
@@ -116,25 +119,23 @@ bitflags! {
116
119
117
120
/// An "epoll", an interface to an OS object allowing one to repeatedly wait
118
121
/// for events from a set of file descriptors efficiently.
119
- pub struct Epoll < Context : self :: Context > {
122
+ pub struct Epoll {
120
123
epoll_fd : OwnedFd ,
121
- context : Context ,
122
124
}
123
125
124
- impl < Context : self :: Context > Epoll < Context > {
126
+ impl Epoll {
125
127
/// `epoll_create1(flags)`—Creates a new `Epoll`.
126
128
///
127
129
/// Use the [`CreateFlags::CLOEXEC`] flag to prevent the resulting file
128
130
/// descriptor from being implicitly passed across `exec` boundaries.
129
131
#[ inline]
130
132
#[ doc( alias = "epoll_create1" ) ]
131
- pub fn new ( flags : CreateFlags , context : Context ) -> io:: Result < Self > {
133
+ pub fn new ( flags : CreateFlags ) -> io:: Result < Self > {
132
134
// Safety: We're calling `epoll_create1` via FFI and we know how it
133
135
// behaves.
134
136
unsafe {
135
137
Ok ( Self {
136
138
epoll_fd : ret_owned_fd ( c:: epoll_create1 ( flags. bits ( ) ) ) ?,
137
- context,
138
139
} )
139
140
}
140
141
}
@@ -145,27 +146,20 @@ impl<Context: self::Context> Epoll<Context> {
145
146
/// This registers interest in any of the events set in `events` occurring
146
147
/// on the file descriptor associated with `data`.
147
148
#[ doc( alias = "epoll_ctl" ) ]
148
- pub fn add (
149
- & self ,
150
- data : Context :: Data ,
151
- event_flags : EventFlags ,
152
- ) -> io:: Result < Ref < ' _ , Context :: Target > > {
149
+ pub fn add ( & self , source : & impl AsFd , data : u64 , event_flags : EventFlags ) -> io:: Result < ( ) > {
153
150
// Safety: We're calling `epoll_ctl` via FFI and we know how it
154
151
// behaves.
155
152
unsafe {
156
- let target = self . context . acquire ( data) ;
157
- let raw_fd = target. as_fd ( ) . as_raw_fd ( ) ;
158
- let encoded = self . context . encode ( target) ;
153
+ let raw_fd = source. as_fd ( ) . as_raw_fd ( ) ;
159
154
ret ( c:: epoll_ctl (
160
155
self . epoll_fd . as_fd ( ) . as_raw_fd ( ) ,
161
156
c:: EPOLL_CTL_ADD ,
162
157
raw_fd,
163
158
& mut c:: epoll_event {
164
159
events : event_flags. bits ( ) ,
165
- r#u64 : encoded ,
160
+ r#u64 : data ,
166
161
} ,
167
- ) ) ?;
168
- Ok ( self . context . decode ( encoded) )
162
+ ) )
169
163
}
170
164
}
171
165
@@ -174,13 +168,9 @@ impl<Context: self::Context> Epoll<Context> {
174
168
///
175
169
/// This sets the events of interest with `target` to `events`.
176
170
#[ doc( alias = "epoll_ctl" ) ]
177
- pub fn mod_ (
178
- & self ,
179
- target : Ref < ' _ , Context :: Target > ,
180
- event_flags : EventFlags ,
181
- ) -> io:: Result < ( ) > {
182
- let raw_fd = target. as_fd ( ) . as_raw_fd ( ) ;
183
- let encoded = self . context . encode ( target) ;
171
+ pub fn mod_ ( & self , source : & impl AsFd , data : u64 , event_flags : EventFlags ) -> io:: Result < ( ) > {
172
+ let raw_fd = source. as_fd ( ) . as_raw_fd ( ) ;
173
+
184
174
// Safety: We're calling `epoll_ctl` via FFI and we know how it
185
175
// behaves.
186
176
unsafe {
@@ -190,7 +180,7 @@ impl<Context: self::Context> Epoll<Context> {
190
180
raw_fd,
191
181
& mut c:: epoll_event {
192
182
events : event_flags. bits ( ) ,
193
- r#u64 : encoded ,
183
+ r#u64 : data ,
194
184
} ,
195
185
) )
196
186
}
@@ -201,19 +191,18 @@ impl<Context: self::Context> Epoll<Context> {
201
191
///
202
192
/// This also returns the owning `Data`.
203
193
#[ doc( alias = "epoll_ctl" ) ]
204
- pub fn del ( & self , target : Ref < ' _ , Context :: Target > ) -> io:: Result < Context :: Data > {
194
+ pub fn del ( & self , source : & impl AsFd ) -> io:: Result < ( ) > {
205
195
// Safety: We're calling `epoll_ctl` via FFI and we know how it
206
196
// behaves.
207
197
unsafe {
208
- let raw_fd = target . as_fd ( ) . as_raw_fd ( ) ;
198
+ let raw_fd = source . as_fd ( ) . as_raw_fd ( ) ;
209
199
ret ( c:: epoll_ctl (
210
200
self . epoll_fd . as_fd ( ) . as_raw_fd ( ) ,
211
201
c:: EPOLL_CTL_DEL ,
212
202
raw_fd,
213
203
null_mut ( ) ,
214
- ) ) ? ;
204
+ ) )
215
205
}
216
- Ok ( self . context . release ( target) )
217
206
}
218
207
219
208
/// `epoll_wait(self, events, timeout)`—Waits for registered events of
@@ -222,11 +211,7 @@ impl<Context: self::Context> Epoll<Context> {
222
211
/// For each event of interest, an element is written to `events`. On
223
212
/// success, this returns the number of written elements.
224
213
#[ doc( alias = "epoll_wait" ) ]
225
- pub fn wait < ' context > (
226
- & ' context self ,
227
- event_list : & mut EventVec < ' context , Context > ,
228
- timeout : c:: c_int ,
229
- ) -> io:: Result < ( ) > {
214
+ pub fn wait ( & self , event_list : & mut EventVec , timeout : c:: c_int ) -> io:: Result < ( ) > {
230
215
// Safety: We're calling `epoll_wait` via FFI and we know how it
231
216
// behaves.
232
217
unsafe {
@@ -238,84 +223,71 @@ impl<Context: self::Context> Epoll<Context> {
238
223
timeout,
239
224
) ) ?;
240
225
event_list. events . set_len ( nfds as usize ) ;
241
- event_list. context = & self . context ;
242
226
}
243
227
244
228
Ok ( ( ) )
245
229
}
246
230
}
247
231
248
232
#[ cfg( not( feature = "rustc-dep-of-std" ) ) ]
249
- impl < ' context , T : AsFd + Into < OwnedFd > + From < OwnedFd > > AsRawFd for Epoll < Owning < ' context , T > > {
233
+ impl AsRawFd for Epoll {
250
234
fn as_raw_fd ( & self ) -> RawFd {
251
235
self . epoll_fd . as_raw_fd ( )
252
236
}
253
237
}
254
238
255
239
#[ cfg( not( feature = "rustc-dep-of-std" ) ) ]
256
- impl < ' context , T : AsFd + Into < OwnedFd > + From < OwnedFd > > IntoRawFd for Epoll < Owning < ' context , T > > {
240
+ impl IntoRawFd for Epoll {
257
241
fn into_raw_fd ( self ) -> RawFd {
258
242
self . epoll_fd . into_raw_fd ( )
259
243
}
260
244
}
261
245
262
246
#[ cfg( not( feature = "rustc-dep-of-std" ) ) ]
263
- impl < ' context , T : AsFd + Into < OwnedFd > + From < OwnedFd > > FromRawFd for Epoll < Owning < ' context , T > > {
247
+ impl FromRawFd for Epoll {
264
248
unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
265
249
Self {
266
250
epoll_fd : OwnedFd :: from_raw_fd ( fd) ,
267
- context : Owning :: new ( ) ,
268
251
}
269
252
}
270
253
}
271
254
272
255
#[ cfg( not( feature = "rustc-dep-of-std" ) ) ]
273
- impl < ' context , T : AsFd + Into < OwnedFd > + From < OwnedFd > > AsFd for Epoll < Owning < ' context , T > > {
256
+ impl AsFd for Epoll {
274
257
fn as_fd ( & self ) -> BorrowedFd < ' _ > {
275
258
self . epoll_fd . as_fd ( )
276
259
}
277
260
}
278
261
279
262
#[ cfg( not( feature = "rustc-dep-of-std" ) ) ]
280
- impl < ' context , T : AsFd + Into < OwnedFd > + From < OwnedFd > > From < Epoll < Owning < ' context , T > > >
281
- for OwnedFd
282
- {
283
- fn from ( epoll : Epoll < Owning < ' context , T > > ) -> Self {
263
+ impl From < Epoll > for OwnedFd {
264
+ fn from ( epoll : Epoll ) -> Self {
284
265
epoll. epoll_fd
285
266
}
286
267
}
287
268
288
269
#[ cfg( not( feature = "rustc-dep-of-std" ) ) ]
289
- impl < ' context , T : AsFd + Into < OwnedFd > + From < OwnedFd > > From < OwnedFd >
290
- for Epoll < Owning < ' context , T > >
291
- {
270
+ impl From < OwnedFd > for Epoll {
292
271
fn from ( fd : OwnedFd ) -> Self {
293
- Self {
294
- epoll_fd : fd,
295
- context : Owning :: new ( ) ,
296
- }
272
+ Self { epoll_fd : fd }
297
273
}
298
274
}
299
275
300
276
/// An iterator over the `Event`s in an `EventVec`.
301
- pub struct Iter < ' context , Context : self :: Context > {
302
- iter : core:: slice:: Iter < ' context , Event > ,
303
- context : * const Context ,
304
- _phantom : PhantomData < & ' context Context > ,
277
+ pub struct Iter < ' a > {
278
+ iter : core:: slice:: Iter < ' a , Event > ,
305
279
}
306
280
307
- impl < ' context , Context : self :: Context > Iterator for Iter < ' context , Context > {
308
- type Item = ( EventFlags , Ref < ' context , Context :: Target > ) ;
281
+ impl < ' a > Iterator for Iter < ' a > {
282
+ type Item = ( EventFlags , u64 ) ;
309
283
310
284
fn next ( & mut self ) -> Option < Self :: Item > {
311
285
// Safety: `self.context` is guaranteed to be valid because we hold
312
286
// `'context` for it. And we know this event is associated with this
313
287
// context because `wait` sets both.
314
- self . iter . next ( ) . map ( |event| {
315
- ( event. event_flags , unsafe {
316
- ( * self . context ) . decode ( event. encoded )
317
- } )
318
- } )
288
+ self . iter
289
+ . next ( )
290
+ . map ( |event| ( event. event_flags , event. encoded ) )
319
291
}
320
292
}
321
293
@@ -341,20 +313,16 @@ struct Event {
341
313
}
342
314
343
315
/// A vector of `Event`s, plus context for interpreting them.
344
- pub struct EventVec < ' context , Context : self :: Context > {
316
+ pub struct EventVec {
345
317
events : Vec < Event > ,
346
- context : * const Context ,
347
- _phantom : PhantomData < & ' context Context > ,
348
318
}
349
319
350
- impl < ' context , Context : self :: Context > EventVec < ' context , Context > {
320
+ impl EventVec {
351
321
/// Constructs an `EventVec` with memory for `capacity` `Event`s.
352
322
#[ inline]
353
323
pub fn with_capacity ( capacity : usize ) -> Self {
354
324
Self {
355
325
events : Vec :: with_capacity ( capacity) ,
356
- context : null ( ) ,
357
- _phantom : PhantomData ,
358
326
}
359
327
}
360
328
@@ -390,11 +358,9 @@ impl<'context, Context: self::Context> EventVec<'context, Context> {
390
358
391
359
/// Returns an iterator over the `Event`s in this `EventVec`.
392
360
#[ inline]
393
- pub fn iter ( & self ) -> Iter < ' _ , Context > {
361
+ pub fn iter ( & self ) -> Iter < ' _ > {
394
362
Iter {
395
363
iter : self . events . iter ( ) ,
396
- context : self . context ,
397
- _phantom : PhantomData ,
398
364
}
399
365
}
400
366
@@ -411,9 +377,9 @@ impl<'context, Context: self::Context> EventVec<'context, Context> {
411
377
}
412
378
}
413
379
414
- impl < ' context , Context : self :: Context > IntoIterator for & ' context EventVec < ' context , Context > {
415
- type IntoIter = Iter < ' context , Context > ;
416
- type Item = ( EventFlags , Ref < ' context , Context :: Target > ) ;
380
+ impl < ' a > IntoIterator for & ' a EventVec {
381
+ type IntoIter = Iter < ' a > ;
382
+ type Item = ( EventFlags , u64 ) ;
417
383
418
384
#[ inline]
419
385
fn into_iter ( self ) -> Self :: IntoIter {
0 commit comments