1 use {Poll, Token};
2 use std::{fmt, io, ops};
3
4 /// A value that may be registered with `Poll`
5 ///
6 /// Values that implement `Evented` can be registered with `Poll`. Users of Mio
7 /// should not use the `Evented` trait functions directly. Instead, the
8 /// equivalent functions on `Poll` should be used.
9 ///
10 /// See [`Poll`] for more details.
11 ///
12 /// # Implementing `Evented`
13 ///
14 /// There are two types of `Evented` values.
15 ///
16 /// * **System** handles, which are backed by sockets or other system handles.
17 /// These `Evented` handles will be monitored by the system selector. In this
18 /// case, an implementation of `Evented` delegates to a lower level handle.
19 ///
20 /// * **User** handles, which are driven entirely in user space using
21 /// [`Registration`] and [`SetReadiness`]. In this case, the implementer takes
22 /// responsibility for driving the readiness state changes.
23 ///
24 /// [`Poll`]: ../struct.Poll.html
25 /// [`Registration`]: ../struct.Registration.html
26 /// [`SetReadiness`]: ../struct.SetReadiness.html
27 ///
28 /// # Examples
29 ///
30 /// Implementing `Evented` on a struct containing a socket:
31 ///
32 /// ```
33 /// use mio::{Ready, Poll, PollOpt, Token};
34 /// use mio::event::Evented;
35 /// use mio::net::TcpStream;
36 ///
37 /// use std::io;
38 ///
39 /// pub struct MyEvented {
40 /// socket: TcpStream,
41 /// }
42 ///
43 /// impl Evented for MyEvented {
44 /// fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
45 /// -> io::Result<()>
46 /// {
47 /// // Delegate the `register` call to `socket`
48 /// self.socket.register(poll, token, interest, opts)
49 /// }
50 ///
51 /// fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
52 /// -> io::Result<()>
53 /// {
54 /// // Delegate the `reregister` call to `socket`
55 /// self.socket.reregister(poll, token, interest, opts)
56 /// }
57 ///
58 /// fn deregister(&self, poll: &Poll) -> io::Result<()> {
59 /// // Delegate the `deregister` call to `socket`
60 /// self.socket.deregister(poll)
61 /// }
62 /// }
63 /// ```
64 ///
65 /// Implement `Evented` using [`Registration`] and [`SetReadiness`].
66 ///
67 /// ```
68 /// use mio::{Ready, Registration, Poll, PollOpt, Token};
69 /// use mio::event::Evented;
70 ///
71 /// use std::io;
72 /// use std::time::Instant;
73 /// use std::thread;
74 ///
75 /// pub struct Deadline {
76 /// when: Instant,
77 /// registration: Registration,
78 /// }
79 ///
80 /// impl Deadline {
81 /// pub fn new(when: Instant) -> Deadline {
82 /// let (registration, set_readiness) = Registration::new2();
83 ///
84 /// thread::spawn(move || {
85 /// let now = Instant::now();
86 ///
87 /// if now < when {
88 /// thread::sleep(when - now);
89 /// }
90 ///
91 /// set_readiness.set_readiness(Ready::readable());
92 /// });
93 ///
94 /// Deadline {
95 /// when: when,
96 /// registration: registration,
97 /// }
98 /// }
99 ///
100 /// pub fn is_elapsed(&self) -> bool {
101 /// Instant::now() >= self.when
102 /// }
103 /// }
104 ///
105 /// impl Evented for Deadline {
106 /// fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
107 /// -> io::Result<()>
108 /// {
109 /// self.registration.register(poll, token, interest, opts)
110 /// }
111 ///
112 /// fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
113 /// -> io::Result<()>
114 /// {
115 /// self.registration.reregister(poll, token, interest, opts)
116 /// }
117 ///
118 /// fn deregister(&self, poll: &Poll) -> io::Result<()> {
119 /// self.registration.deregister(poll)
120 /// }
121 /// }
122 /// ```
123 pub trait Evented {
124 /// Register `self` with the given `Poll` instance.
125 ///
126 /// This function should not be called directly. Use [`Poll::register`]
127 /// instead. Implementors should handle registration by either delegating
128 /// the call to another `Evented` type or creating a [`Registration`].
129 ///
130 /// [`Poll::register`]: ../struct.Poll.html#method.register
131 /// [`Registration`]: ../struct.Registration.html
register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>132 fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>;
133
134 /// Re-register `self` with the given `Poll` instance.
135 ///
136 /// This function should not be called directly. Use [`Poll::reregister`]
137 /// instead. Implementors should handle re-registration by either delegating
138 /// the call to another `Evented` type or calling
139 /// [`SetReadiness::set_readiness`].
140 ///
141 /// [`Poll::reregister`]: ../struct.Poll.html#method.reregister
142 /// [`SetReadiness::set_readiness`]: ../struct.SetReadiness.html#method.set_readiness
reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>143 fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>;
144
145 /// Deregister `self` from the given `Poll` instance
146 ///
147 /// This function should not be called directly. Use [`Poll::deregister`]
148 /// instead. Implementors should handle deregistration by either delegating
149 /// the call to another `Evented` type or by dropping the [`Registration`]
150 /// associated with `self`.
151 ///
152 /// [`Poll::deregister`]: ../struct.Poll.html#method.deregister
153 /// [`Registration`]: ../struct.Registration.html
deregister(&self, poll: &Poll) -> io::Result<()>154 fn deregister(&self, poll: &Poll) -> io::Result<()>;
155 }
156
157 impl Evented for Box<Evented> {
register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>158 fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
159 self.as_ref().register(poll, token, interest, opts)
160 }
161
reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>162 fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
163 self.as_ref().reregister(poll, token, interest, opts)
164 }
165
deregister(&self, poll: &Poll) -> io::Result<()>166 fn deregister(&self, poll: &Poll) -> io::Result<()> {
167 self.as_ref().deregister(poll)
168 }
169 }
170
171 impl<T: Evented> Evented for Box<T> {
register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>172 fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
173 self.as_ref().register(poll, token, interest, opts)
174 }
175
reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>176 fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
177 self.as_ref().reregister(poll, token, interest, opts)
178 }
179
deregister(&self, poll: &Poll) -> io::Result<()>180 fn deregister(&self, poll: &Poll) -> io::Result<()> {
181 self.as_ref().deregister(poll)
182 }
183 }
184
185 impl<T: Evented> Evented for ::std::sync::Arc<T> {
register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>186 fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
187 self.as_ref().register(poll, token, interest, opts)
188 }
189
reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>190 fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
191 self.as_ref().reregister(poll, token, interest, opts)
192 }
193
deregister(&self, poll: &Poll) -> io::Result<()>194 fn deregister(&self, poll: &Poll) -> io::Result<()> {
195 self.as_ref().deregister(poll)
196 }
197 }
198
199 /// Options supplied when registering an `Evented` handle with `Poll`
200 ///
201 /// `PollOpt` values can be combined together using the various bitwise
202 /// operators.
203 ///
204 /// For high level documentation on polling and poll options, see [`Poll`].
205 ///
206 /// # Examples
207 ///
208 /// ```
209 /// use mio::PollOpt;
210 ///
211 /// let opts = PollOpt::edge() | PollOpt::oneshot();
212 ///
213 /// assert!(opts.is_edge());
214 /// assert!(opts.is_oneshot());
215 /// assert!(!opts.is_level());
216 /// ```
217 ///
218 /// [`Poll`]: struct.Poll.html
219 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord)]
220 pub struct PollOpt(usize);
221
222 impl PollOpt {
223 /// Return a `PollOpt` representing no set options.
224 ///
225 /// See [`Poll`] for more documentation on polling.
226 ///
227 /// # Examples
228 ///
229 /// ```
230 /// use mio::PollOpt;
231 ///
232 /// let opt = PollOpt::empty();
233 ///
234 /// assert!(!opt.is_level());
235 /// ```
236 ///
237 /// [`Poll`]: struct.Poll.html
238 #[inline]
empty() -> PollOpt239 pub fn empty() -> PollOpt {
240 PollOpt(0)
241 }
242
243 /// Return a `PollOpt` representing edge-triggered notifications.
244 ///
245 /// See [`Poll`] for more documentation on polling.
246 ///
247 /// # Examples
248 ///
249 /// ```
250 /// use mio::PollOpt;
251 ///
252 /// let opt = PollOpt::edge();
253 ///
254 /// assert!(opt.is_edge());
255 /// ```
256 ///
257 /// [`Poll`]: struct.Poll.html
258 #[inline]
edge() -> PollOpt259 pub fn edge() -> PollOpt {
260 PollOpt(0b0001)
261 }
262
263 /// Return a `PollOpt` representing level-triggered notifications.
264 ///
265 /// See [`Poll`] for more documentation on polling.
266 ///
267 /// # Examples
268 ///
269 /// ```
270 /// use mio::PollOpt;
271 ///
272 /// let opt = PollOpt::level();
273 ///
274 /// assert!(opt.is_level());
275 /// ```
276 ///
277 /// [`Poll`]: struct.Poll.html
278 #[inline]
level() -> PollOpt279 pub fn level() -> PollOpt {
280 PollOpt(0b0010)
281 }
282
283 /// Return a `PollOpt` representing oneshot notifications.
284 ///
285 /// See [`Poll`] for more documentation on polling.
286 ///
287 /// # Examples
288 ///
289 /// ```
290 /// use mio::PollOpt;
291 ///
292 /// let opt = PollOpt::oneshot();
293 ///
294 /// assert!(opt.is_oneshot());
295 /// ```
296 ///
297 /// [`Poll`]: struct.Poll.html
298 #[inline]
oneshot() -> PollOpt299 pub fn oneshot() -> PollOpt {
300 PollOpt(0b0100)
301 }
302
303 #[deprecated(since = "0.6.5", note = "removed")]
304 #[cfg(feature = "with-deprecated")]
305 #[doc(hidden)]
306 #[inline]
urgent() -> PollOpt307 pub fn urgent() -> PollOpt {
308 PollOpt(0b1000)
309 }
310
311 #[deprecated(since = "0.6.5", note = "removed")]
312 #[cfg(feature = "with-deprecated")]
313 #[doc(hidden)]
314 #[inline]
all() -> PollOpt315 pub fn all() -> PollOpt {
316 PollOpt::edge() | PollOpt::level() | PollOpt::oneshot()
317 }
318
319 /// Returns true if the options include edge-triggered notifications.
320 ///
321 /// See [`Poll`] for more documentation on polling.
322 ///
323 /// # Examples
324 ///
325 /// ```
326 /// use mio::PollOpt;
327 ///
328 /// let opt = PollOpt::edge();
329 ///
330 /// assert!(opt.is_edge());
331 /// ```
332 ///
333 /// [`Poll`]: struct.Poll.html
334 #[inline]
is_edge(&self) -> bool335 pub fn is_edge(&self) -> bool {
336 self.contains(PollOpt::edge())
337 }
338
339 /// Returns true if the options include level-triggered notifications.
340 ///
341 /// See [`Poll`] for more documentation on polling.
342 ///
343 /// # Examples
344 ///
345 /// ```
346 /// use mio::PollOpt;
347 ///
348 /// let opt = PollOpt::level();
349 ///
350 /// assert!(opt.is_level());
351 /// ```
352 ///
353 /// [`Poll`]: struct.Poll.html
354 #[inline]
is_level(&self) -> bool355 pub fn is_level(&self) -> bool {
356 self.contains(PollOpt::level())
357 }
358
359 /// Returns true if the options includes oneshot.
360 ///
361 /// See [`Poll`] for more documentation on polling.
362 ///
363 /// # Examples
364 ///
365 /// ```
366 /// use mio::PollOpt;
367 ///
368 /// let opt = PollOpt::oneshot();
369 ///
370 /// assert!(opt.is_oneshot());
371 /// ```
372 ///
373 /// [`Poll`]: struct.Poll.html
374 #[inline]
is_oneshot(&self) -> bool375 pub fn is_oneshot(&self) -> bool {
376 self.contains(PollOpt::oneshot())
377 }
378
379 #[deprecated(since = "0.6.5", note = "removed")]
380 #[cfg(feature = "with-deprecated")]
381 #[doc(hidden)]
382 #[allow(deprecated)]
383 #[inline]
is_urgent(&self) -> bool384 pub fn is_urgent(&self) -> bool {
385 self.contains(PollOpt::urgent())
386 }
387
388 #[deprecated(since = "0.6.5", note = "removed")]
389 #[cfg(feature = "with-deprecated")]
390 #[doc(hidden)]
391 #[inline]
bits(&self) -> usize392 pub fn bits(&self) -> usize {
393 self.0
394 }
395
396 /// Returns true if `self` is a superset of `other`.
397 ///
398 /// `other` may represent more than one option, in which case the function
399 /// only returns true if `self` contains all of the options specified in
400 /// `other`.
401 ///
402 /// See [`Poll`] for more documentation on polling.
403 ///
404 /// # Examples
405 ///
406 /// ```
407 /// use mio::PollOpt;
408 ///
409 /// let opt = PollOpt::oneshot();
410 ///
411 /// assert!(opt.contains(PollOpt::oneshot()));
412 /// assert!(!opt.contains(PollOpt::edge()));
413 /// ```
414 ///
415 /// ```
416 /// use mio::PollOpt;
417 ///
418 /// let opt = PollOpt::oneshot() | PollOpt::edge();
419 ///
420 /// assert!(opt.contains(PollOpt::oneshot()));
421 /// assert!(opt.contains(PollOpt::edge()));
422 /// ```
423 ///
424 /// ```
425 /// use mio::PollOpt;
426 ///
427 /// let opt = PollOpt::oneshot() | PollOpt::edge();
428 ///
429 /// assert!(!PollOpt::oneshot().contains(opt));
430 /// assert!(opt.contains(opt));
431 /// assert!((opt | PollOpt::level()).contains(opt));
432 /// ```
433 ///
434 /// [`Poll`]: struct.Poll.html
435 #[inline]
contains(&self, other: PollOpt) -> bool436 pub fn contains(&self, other: PollOpt) -> bool {
437 (*self & other) == other
438 }
439
440 /// Adds all options represented by `other` into `self`.
441 ///
442 /// This is equivalent to `*self = *self | other`.
443 ///
444 /// # Examples
445 ///
446 /// ```
447 /// use mio::PollOpt;
448 ///
449 /// let mut opt = PollOpt::empty();
450 /// opt.insert(PollOpt::oneshot());
451 ///
452 /// assert!(opt.is_oneshot());
453 /// ```
454 #[inline]
insert(&mut self, other: PollOpt)455 pub fn insert(&mut self, other: PollOpt) {
456 self.0 |= other.0;
457 }
458
459 /// Removes all options represented by `other` from `self`.
460 ///
461 /// This is equivalent to `*self = *self & !other`.
462 ///
463 /// # Examples
464 ///
465 /// ```
466 /// use mio::PollOpt;
467 ///
468 /// let mut opt = PollOpt::oneshot();
469 /// opt.remove(PollOpt::oneshot());
470 ///
471 /// assert!(!opt.is_oneshot());
472 /// ```
473 #[inline]
remove(&mut self, other: PollOpt)474 pub fn remove(&mut self, other: PollOpt) {
475 self.0 &= !other.0;
476 }
477 }
478
479 impl ops::BitOr for PollOpt {
480 type Output = PollOpt;
481
482 #[inline]
bitor(self, other: PollOpt) -> PollOpt483 fn bitor(self, other: PollOpt) -> PollOpt {
484 PollOpt(self.0 | other.0)
485 }
486 }
487
488 impl ops::BitXor for PollOpt {
489 type Output = PollOpt;
490
491 #[inline]
bitxor(self, other: PollOpt) -> PollOpt492 fn bitxor(self, other: PollOpt) -> PollOpt {
493 PollOpt(self.0 ^ other.0)
494 }
495 }
496
497 impl ops::BitAnd for PollOpt {
498 type Output = PollOpt;
499
500 #[inline]
bitand(self, other: PollOpt) -> PollOpt501 fn bitand(self, other: PollOpt) -> PollOpt {
502 PollOpt(self.0 & other.0)
503 }
504 }
505
506 impl ops::Sub for PollOpt {
507 type Output = PollOpt;
508
509 #[inline]
sub(self, other: PollOpt) -> PollOpt510 fn sub(self, other: PollOpt) -> PollOpt {
511 PollOpt(self.0 & !other.0)
512 }
513 }
514
515 #[deprecated(since = "0.6.10", note = "removed")]
516 #[cfg(feature = "with-deprecated")]
517 #[doc(hidden)]
518 impl ops::Not for PollOpt {
519 type Output = PollOpt;
520
521 #[inline]
not(self) -> PollOpt522 fn not(self) -> PollOpt {
523 PollOpt(!self.0)
524 }
525 }
526
527 impl fmt::Debug for PollOpt {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result528 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
529 let mut one = false;
530 let flags = [
531 (PollOpt::edge(), "Edge-Triggered"),
532 (PollOpt::level(), "Level-Triggered"),
533 (PollOpt::oneshot(), "OneShot")];
534
535 for &(flag, msg) in &flags {
536 if self.contains(flag) {
537 if one { write!(fmt, " | ")? }
538 write!(fmt, "{}", msg)?;
539
540 one = true
541 }
542 }
543
544 if !one {
545 fmt.write_str("(empty)")?;
546 }
547
548 Ok(())
549 }
550 }
551
552 #[test]
test_debug_pollopt()553 fn test_debug_pollopt() {
554 assert_eq!("(empty)", format!("{:?}", PollOpt::empty()));
555 assert_eq!("Edge-Triggered", format!("{:?}", PollOpt::edge()));
556 assert_eq!("Level-Triggered", format!("{:?}", PollOpt::level()));
557 assert_eq!("OneShot", format!("{:?}", PollOpt::oneshot()));
558 }
559
560 /// A set of readiness event kinds
561 ///
562 /// `Ready` is a set of operation descriptors indicating which kind of an
563 /// operation is ready to be performed. For example, `Ready::readable()`
564 /// indicates that the associated `Evented` handle is ready to perform a
565 /// `read` operation.
566 ///
567 /// This struct only represents portable event kinds. Since only readable and
568 /// writable events are guaranteed to be raised on all systems, those are the
569 /// only ones available via the `Ready` struct. There are also platform specific
570 /// extensions to `Ready`, i.e. `UnixReady`, which provide additional readiness
571 /// event kinds only available on unix platforms.
572 ///
573 /// `Ready` values can be combined together using the various bitwise operators.
574 ///
575 /// For high level documentation on polling and readiness, see [`Poll`].
576 ///
577 /// # Examples
578 ///
579 /// ```
580 /// use mio::Ready;
581 ///
582 /// let ready = Ready::readable() | Ready::writable();
583 ///
584 /// assert!(ready.is_readable());
585 /// assert!(ready.is_writable());
586 /// ```
587 ///
588 /// [`Poll`]: struct.Poll.html
589 /// [`readable`]: #method.readable
590 /// [`writable`]: #method.writable
591 /// [readiness]: struct.Poll.html#readiness-operations
592 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord)]
593 pub struct Ready(usize);
594
595 const READABLE: usize = 0b00001;
596 const WRITABLE: usize = 0b00010;
597
598 // These are deprecated and are moved into platform specific implementations.
599 const ERROR: usize = 0b00100;
600 const HUP: usize = 0b01000;
601
602 impl Ready {
603 /// Returns the empty `Ready` set.
604 ///
605 /// See [`Poll`] for more documentation on polling.
606 ///
607 /// # Examples
608 ///
609 /// ```
610 /// use mio::Ready;
611 ///
612 /// let ready = Ready::empty();
613 ///
614 /// assert!(!ready.is_readable());
615 /// ```
616 ///
617 /// [`Poll`]: struct.Poll.html
empty() -> Ready618 pub fn empty() -> Ready {
619 Ready(0)
620 }
621
622 #[deprecated(since = "0.6.5", note = "use Ready::empty instead")]
623 #[cfg(feature = "with-deprecated")]
624 #[doc(hidden)]
none() -> Ready625 pub fn none() -> Ready {
626 Ready::empty()
627 }
628
629 /// Returns a `Ready` representing readable readiness.
630 ///
631 /// See [`Poll`] for more documentation on polling.
632 ///
633 /// # Examples
634 ///
635 /// ```
636 /// use mio::Ready;
637 ///
638 /// let ready = Ready::readable();
639 ///
640 /// assert!(ready.is_readable());
641 /// ```
642 ///
643 /// [`Poll`]: struct.Poll.html
644 #[inline]
readable() -> Ready645 pub fn readable() -> Ready {
646 Ready(READABLE)
647 }
648
649 /// Returns a `Ready` representing writable readiness.
650 ///
651 /// See [`Poll`] for more documentation on polling.
652 ///
653 /// # Examples
654 ///
655 /// ```
656 /// use mio::Ready;
657 ///
658 /// let ready = Ready::writable();
659 ///
660 /// assert!(ready.is_writable());
661 /// ```
662 ///
663 /// [`Poll`]: struct.Poll.html
664 #[inline]
writable() -> Ready665 pub fn writable() -> Ready {
666 Ready(WRITABLE)
667 }
668
669 #[deprecated(since = "0.6.5", note = "use UnixReady instead")]
670 #[cfg(feature = "with-deprecated")]
671 #[doc(hidden)]
672 #[inline]
error() -> Ready673 pub fn error() -> Ready {
674 Ready(ERROR)
675 }
676
677 #[deprecated(since = "0.6.5", note = "use UnixReady instead")]
678 #[cfg(feature = "with-deprecated")]
679 #[doc(hidden)]
680 #[inline]
hup() -> Ready681 pub fn hup() -> Ready {
682 Ready(HUP)
683 }
684
685 /// Returns a `Ready` representing readiness for all operations.
686 ///
687 /// This includes platform specific operations as well (`hup`, `aio`,
688 /// `error`, `lio`, `pri`).
689 ///
690 /// See [`Poll`] for more documentation on polling.
691 ///
692 /// # Examples
693 ///
694 /// ```
695 /// use mio::Ready;
696 ///
697 /// let ready = Ready::all();
698 ///
699 /// assert!(ready.is_readable());
700 /// assert!(ready.is_writable());
701 /// ```
702 ///
703 /// [`Poll`]: struct.Poll.html
704 #[inline]
all() -> Ready705 pub fn all() -> Ready {
706 Ready(READABLE | WRITABLE | ::sys::READY_ALL)
707 }
708
709 /// Returns true if `Ready` is the empty set
710 ///
711 /// See [`Poll`] for more documentation on polling.
712 ///
713 /// # Examples
714 ///
715 /// ```
716 /// use mio::Ready;
717 ///
718 /// let ready = Ready::empty();
719 /// assert!(ready.is_empty());
720 /// ```
721 ///
722 /// [`Poll`]: struct.Poll.html
723 #[inline]
is_empty(&self) -> bool724 pub fn is_empty(&self) -> bool {
725 *self == Ready::empty()
726 }
727
728 #[deprecated(since = "0.6.5", note = "use Ready::is_empty instead")]
729 #[cfg(feature = "with-deprecated")]
730 #[doc(hidden)]
731 #[inline]
is_none(&self) -> bool732 pub fn is_none(&self) -> bool {
733 self.is_empty()
734 }
735
736 /// Returns true if the value includes readable readiness
737 ///
738 /// See [`Poll`] for more documentation on polling.
739 ///
740 /// # Examples
741 ///
742 /// ```
743 /// use mio::Ready;
744 ///
745 /// let ready = Ready::readable();
746 ///
747 /// assert!(ready.is_readable());
748 /// ```
749 ///
750 /// [`Poll`]: struct.Poll.html
751 #[inline]
is_readable(&self) -> bool752 pub fn is_readable(&self) -> bool {
753 self.contains(Ready::readable())
754 }
755
756 /// Returns true if the value includes writable readiness
757 ///
758 /// See [`Poll`] for more documentation on polling.
759 ///
760 /// # Examples
761 ///
762 /// ```
763 /// use mio::Ready;
764 ///
765 /// let ready = Ready::writable();
766 ///
767 /// assert!(ready.is_writable());
768 /// ```
769 ///
770 /// [`Poll`]: struct.Poll.html
771 #[inline]
is_writable(&self) -> bool772 pub fn is_writable(&self) -> bool {
773 self.contains(Ready::writable())
774 }
775
776 #[deprecated(since = "0.6.5", note = "use UnixReady instead")]
777 #[cfg(feature = "with-deprecated")]
778 #[doc(hidden)]
779 #[inline]
is_error(&self) -> bool780 pub fn is_error(&self) -> bool {
781 self.contains(Ready(ERROR))
782 }
783
784 #[deprecated(since = "0.6.5", note = "use UnixReady instead")]
785 #[cfg(feature = "with-deprecated")]
786 #[doc(hidden)]
787 #[inline]
is_hup(&self) -> bool788 pub fn is_hup(&self) -> bool {
789 self.contains(Ready(HUP))
790 }
791
792 /// Adds all readiness represented by `other` into `self`.
793 ///
794 /// This is equivalent to `*self = *self | other`.
795 ///
796 /// # Examples
797 ///
798 /// ```
799 /// use mio::Ready;
800 ///
801 /// let mut readiness = Ready::empty();
802 /// readiness.insert(Ready::readable());
803 ///
804 /// assert!(readiness.is_readable());
805 /// ```
806 #[inline]
insert<T: Into<Self>>(&mut self, other: T)807 pub fn insert<T: Into<Self>>(&mut self, other: T) {
808 let other = other.into();
809 self.0 |= other.0;
810 }
811
812 /// Removes all options represented by `other` from `self`.
813 ///
814 /// This is equivalent to `*self = *self & !other`.
815 ///
816 /// # Examples
817 ///
818 /// ```
819 /// use mio::Ready;
820 ///
821 /// let mut readiness = Ready::readable();
822 /// readiness.remove(Ready::readable());
823 ///
824 /// assert!(!readiness.is_readable());
825 /// ```
826 #[inline]
remove<T: Into<Self>>(&mut self, other: T)827 pub fn remove<T: Into<Self>>(&mut self, other: T) {
828 let other = other.into();
829 self.0 &= !other.0;
830 }
831
832 #[deprecated(since = "0.6.5", note = "removed")]
833 #[cfg(feature = "with-deprecated")]
834 #[doc(hidden)]
835 #[inline]
bits(&self) -> usize836 pub fn bits(&self) -> usize {
837 self.0
838 }
839
840 /// Returns true if `self` is a superset of `other`.
841 ///
842 /// `other` may represent more than one readiness operations, in which case
843 /// the function only returns true if `self` contains all readiness
844 /// specified in `other`.
845 ///
846 /// See [`Poll`] for more documentation on polling.
847 ///
848 /// # Examples
849 ///
850 /// ```
851 /// use mio::Ready;
852 ///
853 /// let readiness = Ready::readable();
854 ///
855 /// assert!(readiness.contains(Ready::readable()));
856 /// assert!(!readiness.contains(Ready::writable()));
857 /// ```
858 ///
859 /// ```
860 /// use mio::Ready;
861 ///
862 /// let readiness = Ready::readable() | Ready::writable();
863 ///
864 /// assert!(readiness.contains(Ready::readable()));
865 /// assert!(readiness.contains(Ready::writable()));
866 /// ```
867 ///
868 /// ```
869 /// use mio::Ready;
870 ///
871 /// let readiness = Ready::readable() | Ready::writable();
872 ///
873 /// assert!(!Ready::readable().contains(readiness));
874 /// assert!(readiness.contains(readiness));
875 /// ```
876 ///
877 /// [`Poll`]: struct.Poll.html
878 #[inline]
contains<T: Into<Self>>(&self, other: T) -> bool879 pub fn contains<T: Into<Self>>(&self, other: T) -> bool {
880 let other = other.into();
881 (*self & other) == other
882 }
883
884 /// Create a `Ready` instance using the given `usize` representation.
885 ///
886 /// The `usize` representation must have been obtained from a call to
887 /// `Ready::as_usize`.
888 ///
889 /// The `usize` representation must be treated as opaque. There is no
890 /// guaranteed correlation between the returned value and platform defined
891 /// constants. Also, there is no guarantee that the `usize` representation
892 /// will remain constant across patch releases of Mio.
893 ///
894 /// This function is mainly provided to allow the caller to loa a
895 /// readiness value from an `AtomicUsize`.
896 ///
897 /// # Examples
898 ///
899 /// ```
900 /// use mio::Ready;
901 ///
902 /// let ready = Ready::readable();
903 /// let ready_usize = ready.as_usize();
904 /// let ready2 = Ready::from_usize(ready_usize);
905 ///
906 /// assert_eq!(ready, ready2);
907 /// ```
from_usize(val: usize) -> Ready908 pub fn from_usize(val: usize) -> Ready {
909 Ready(val)
910 }
911
912 /// Returns a `usize` representation of the `Ready` value.
913 ///
914 /// This `usize` representation must be treated as opaque. There is no
915 /// guaranteed correlation between the returned value and platform defined
916 /// constants. Also, there is no guarantee that the `usize` representation
917 /// will remain constant across patch releases of Mio.
918 ///
919 /// This function is mainly provided to allow the caller to store a
920 /// readiness value in an `AtomicUsize`.
921 ///
922 /// # Examples
923 ///
924 /// ```
925 /// use mio::Ready;
926 ///
927 /// let ready = Ready::readable();
928 /// let ready_usize = ready.as_usize();
929 /// let ready2 = Ready::from_usize(ready_usize);
930 ///
931 /// assert_eq!(ready, ready2);
932 /// ```
as_usize(&self) -> usize933 pub fn as_usize(&self) -> usize {
934 self.0
935 }
936 }
937
938 impl<T: Into<Ready>> ops::BitOr<T> for Ready {
939 type Output = Ready;
940
941 #[inline]
bitor(self, other: T) -> Ready942 fn bitor(self, other: T) -> Ready {
943 Ready(self.0 | other.into().0)
944 }
945 }
946
947 impl<T: Into<Ready>> ops::BitOrAssign<T> for Ready {
948 #[inline]
bitor_assign(&mut self, other: T)949 fn bitor_assign(&mut self, other: T) {
950 self.0 |= other.into().0;
951 }
952 }
953
954 impl<T: Into<Ready>> ops::BitXor<T> for Ready {
955 type Output = Ready;
956
957 #[inline]
bitxor(self, other: T) -> Ready958 fn bitxor(self, other: T) -> Ready {
959 Ready(self.0 ^ other.into().0)
960 }
961 }
962
963 impl<T: Into<Ready>> ops::BitXorAssign<T> for Ready {
964 #[inline]
bitxor_assign(&mut self, other: T)965 fn bitxor_assign(&mut self, other: T) {
966 self.0 ^= other.into().0;
967 }
968 }
969
970 impl<T: Into<Ready>> ops::BitAnd<T> for Ready {
971 type Output = Ready;
972
973 #[inline]
bitand(self, other: T) -> Ready974 fn bitand(self, other: T) -> Ready {
975 Ready(self.0 & other.into().0)
976 }
977 }
978
979 impl<T: Into<Ready>> ops::BitAndAssign<T> for Ready {
980 #[inline]
bitand_assign(&mut self, other: T)981 fn bitand_assign(&mut self, other: T) {
982 self.0 &= other.into().0
983 }
984 }
985
986 impl<T: Into<Ready>> ops::Sub<T> for Ready {
987 type Output = Ready;
988
989 #[inline]
sub(self, other: T) -> Ready990 fn sub(self, other: T) -> Ready {
991 Ready(self.0 & !other.into().0)
992 }
993 }
994
995 impl<T: Into<Ready>> ops::SubAssign<T> for Ready {
996 #[inline]
sub_assign(&mut self, other: T)997 fn sub_assign(&mut self, other: T) {
998 self.0 &= !other.into().0;
999 }
1000 }
1001
1002 #[deprecated(since = "0.6.10", note = "removed")]
1003 #[cfg(feature = "with-deprecated")]
1004 #[doc(hidden)]
1005 impl ops::Not for Ready {
1006 type Output = Ready;
1007
1008 #[inline]
not(self) -> Ready1009 fn not(self) -> Ready {
1010 Ready(!self.0)
1011 }
1012 }
1013
1014 impl fmt::Debug for Ready {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result1015 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1016 let mut one = false;
1017 let flags = [
1018 (Ready::readable(), "Readable"),
1019 (Ready::writable(), "Writable"),
1020 (Ready(ERROR), "Error"),
1021 (Ready(HUP), "Hup")];
1022
1023 for &(flag, msg) in &flags {
1024 if self.contains(flag) {
1025 if one { write!(fmt, " | ")? }
1026 write!(fmt, "{}", msg)?;
1027
1028 one = true
1029 }
1030 }
1031
1032 if !one {
1033 fmt.write_str("(empty)")?;
1034 }
1035
1036 Ok(())
1037 }
1038 }
1039
1040 #[test]
test_debug_ready()1041 fn test_debug_ready() {
1042 assert_eq!("(empty)", format!("{:?}", Ready::empty()));
1043 assert_eq!("Readable", format!("{:?}", Ready::readable()));
1044 assert_eq!("Writable", format!("{:?}", Ready::writable()));
1045 }
1046
1047 /// An readiness event returned by [`Poll::poll`].
1048 ///
1049 /// `Event` is a [readiness state] paired with a [`Token`]. It is returned by
1050 /// [`Poll::poll`].
1051 ///
1052 /// For more documentation on polling and events, see [`Poll`].
1053 ///
1054 /// # Examples
1055 ///
1056 /// ```
1057 /// use mio::{Ready, Token};
1058 /// use mio::event::Event;
1059 ///
1060 /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0));
1061 ///
1062 /// assert_eq!(event.readiness(), Ready::readable() | Ready::writable());
1063 /// assert_eq!(event.token(), Token(0));
1064 /// ```
1065 ///
1066 /// [`Poll::poll`]: ../struct.Poll.html#method.poll
1067 /// [`Poll`]: ../struct.Poll.html
1068 /// [readiness state]: ../struct.Ready.html
1069 /// [`Token`]: ../struct.Token.html
1070 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
1071 pub struct Event {
1072 kind: Ready,
1073 token: Token
1074 }
1075
1076 impl Event {
1077 /// Creates a new `Event` containing `readiness` and `token`
1078 ///
1079 /// # Examples
1080 ///
1081 /// ```
1082 /// use mio::{Ready, Token};
1083 /// use mio::event::Event;
1084 ///
1085 /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0));
1086 ///
1087 /// assert_eq!(event.readiness(), Ready::readable() | Ready::writable());
1088 /// assert_eq!(event.token(), Token(0));
1089 /// ```
new(readiness: Ready, token: Token) -> Event1090 pub fn new(readiness: Ready, token: Token) -> Event {
1091 Event {
1092 kind: readiness,
1093 token,
1094 }
1095 }
1096
1097 /// Returns the event's readiness.
1098 ///
1099 /// # Examples
1100 ///
1101 /// ```
1102 /// use mio::{Ready, Token};
1103 /// use mio::event::Event;
1104 ///
1105 /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0));
1106 ///
1107 /// assert_eq!(event.readiness(), Ready::readable() | Ready::writable());
1108 /// ```
readiness(&self) -> Ready1109 pub fn readiness(&self) -> Ready {
1110 self.kind
1111 }
1112
1113 #[deprecated(since = "0.6.5", note = "use Event::readiness()")]
1114 #[cfg(feature = "with-deprecated")]
1115 #[doc(hidden)]
kind(&self) -> Ready1116 pub fn kind(&self) -> Ready {
1117 self.kind
1118 }
1119
1120 /// Returns the event's token.
1121 ///
1122 /// # Examples
1123 ///
1124 /// ```
1125 /// use mio::{Ready, Token};
1126 /// use mio::event::Event;
1127 ///
1128 /// let event = Event::new(Ready::readable() | Ready::writable(), Token(0));
1129 ///
1130 /// assert_eq!(event.token(), Token(0));
1131 /// ```
token(&self) -> Token1132 pub fn token(&self) -> Token {
1133 self.token
1134 }
1135 }
1136
1137 /*
1138 *
1139 * ===== Mio internal helpers =====
1140 *
1141 */
1142
ready_as_usize(events: Ready) -> usize1143 pub fn ready_as_usize(events: Ready) -> usize {
1144 events.0
1145 }
1146
opt_as_usize(opt: PollOpt) -> usize1147 pub fn opt_as_usize(opt: PollOpt) -> usize {
1148 opt.0
1149 }
1150
ready_from_usize(events: usize) -> Ready1151 pub fn ready_from_usize(events: usize) -> Ready {
1152 Ready(events)
1153 }
1154
opt_from_usize(opt: usize) -> PollOpt1155 pub fn opt_from_usize(opt: usize) -> PollOpt {
1156 PollOpt(opt)
1157 }
1158
1159 // Used internally to mutate an `Event` in place
1160 // Not used on all platforms
1161 #[allow(dead_code)]
kind_mut(event: &mut Event) -> &mut Ready1162 pub fn kind_mut(event: &mut Event) -> &mut Ready {
1163 &mut event.kind
1164 }
1165