1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use std::io; use {Poll, Token, Ready, PollOpt}; /// A value that may be registered with `Poll` /// /// Values that implement `Evented` can be registered with `Poll`. Users of soio /// should not use the `Evented` trait functions directly. Instead, the /// equivalent functions on `Poll` should be used. /// /// See [`Poll`] for more details. /// /// # Implementing `Evented` /// /// There are two types of `Evented` values. /// /// * **System** handles, which are backed by sockets or other system handles. /// These `Evented` handles will be monitored by the system selector. In this /// case, an implementation of `Evented` delegates to a lower level handle. /// /// * **User** handles, which are driven entirely in user space using /// [`Registration`] and [`SetReadiness`]. In this case, the implementer takes /// responsibility for driving the readiness state changes. /// /// [`Poll`]: struct.Poll.html /// [`Registration`]: struct.Registration.html /// [`SetReadiness`]: struct.SetReadiness.html /// /// # Examples /// /// Implementing `Evented` on a struct containing a socket: /// /// ``` /// use soio::{Ready, Poll, PollOpt, Token}; /// use soio::Evented; /// use soio::tcp::TcpStream; /// /// use std::io; /// /// pub struct MyEvented { /// socket: TcpStream, /// } /// /// impl Evented for MyEvented { /// fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) /// -> io::Result<()> /// { /// // Delegate the `register` call to `socket` /// self.socket.register(poll, token, interest, opts) /// } /// /// fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) /// -> io::Result<()> /// { /// // Delegate the `reregister` call to `socket` /// self.socket.reregister(poll, token, interest, opts) /// } /// /// fn deregister(&self, poll: &Poll) -> io::Result<()> { /// // Delegate the `deregister` call to `socket` /// self.socket.deregister(poll) /// } /// } /// ``` /// /// Implement `Evented` using [`Registration`] and [`SetReadiness`]. /// /// ``` /// use soio::{Ready, Registration, Poll, PollOpt, Token}; /// use soio::Evented; /// /// use std::io; /// use std::time::Instant; /// use std::thread; /// /// pub struct Deadline { /// when: Instant, /// registration: Registration, /// } /// /// impl Deadline { /// pub fn new(when: Instant) -> Deadline { /// let (registration, set_readiness) = Registration::new(); /// /// thread::spawn(move || { /// let now = Instant::now(); /// /// if now < when { /// thread::sleep(when - now); /// } /// /// set_readiness.set_readiness(Ready::readable()); /// }); /// /// Deadline { /// when: when, /// registration: registration, /// } /// } /// /// pub fn is_elapsed(&self) -> bool { /// Instant::now() >= self.when /// } /// } /// /// impl Evented for Deadline { /// fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) /// -> io::Result<()> /// { /// self.registration.register(poll, token, interest, opts) /// } /// /// fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) /// -> io::Result<()> /// { /// self.registration.reregister(poll, token, interest, opts) /// } /// /// fn deregister(&self, poll: &Poll) -> io::Result<()> { /// self.registration.deregister(poll) /// } /// } /// ``` pub trait Evented { /// Register `self` with the given `Poll` instance. /// /// This function should not be called directly. Use [`Poll::register`] /// instead. Implementors should handle registration by either delegating /// the call to another `Evented` type or calling [`Registration::register`]. /// /// See [struct] documentation for more details. /// /// [`Poll::register`]: struct.Poll.html#method.register /// [`Registration::register`]: struct.Registration.html#method.register /// [struct]: # fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>; /// Re-register `self` with the given `Poll` instance. /// /// This function should not be called directly. Use [`Poll::reregister`] /// instead. Implementors should handle re-registration by either delegating /// the call to another `Evented` type or calling [`Registration::reregister`]. /// /// See [struct] documentation for more details. /// /// [`Poll::reregister`]: struct.Poll.html#method.register /// [`Registration::reregister`]: struct.Registration.html#method.reregister /// [struct]: # fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>; /// Deregister `self` from the given `Poll` instance /// /// This function should not be called directly. Use [`Poll::deregister`] /// instead. Implementors shuld handle deregistration by either delegating /// the call to another `Evented` type or by dropping the [`Registration`] /// associated with `self`. /// /// See [struct] documentation for more details. /// /// [`Poll::deregister`]: struct.Poll.html#method.deregister /// [`Registration`]: struct.Registration.html /// [struct]: # fn deregister(&self, poll: &Poll) -> io::Result<()>; }