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