| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#, convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryIntorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#, osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/io/::RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#, rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#, timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#}; |
| 2 | use vec_maprust-analyzer cargo vec_map 0.8.2 crate/::VecMaprust-analyzer cargo vec_map 0.8.2 VecMap#; |
| 3 | |
| 4 | use craterust-analyzer cargo calloop 0.10.5 crate/::{loop_logicrust-analyzer cargo calloop 0.10.5 loop_logic/::CalloopKeyrust-analyzer cargo calloop 0.10.5 loop_logic/CalloopKey#, sourcesrust-analyzer cargo calloop 0.10.5 sources/::timerrust-analyzer cargo calloop 0.10.5 sources/timer/::TimerWheelrust-analyzer cargo calloop 0.10.5 sources/timer/TimerWheel#}; |
| 5 | |
| 6 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 7 | mod epollrust-analyzer cargo calloop 0.10.5 sys/epoll/; |
| 8 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 9 | use epollrust-analyzer cargo calloop 0.10.5 sys/epoll/::Epollrust-analyzer cargo calloop 0.10.5 sys/epoll/Epoll# as Pollerrust-analyzer cargo calloop 0.10.5 sys/epoll/Epoll#; |
| 10 | |
| 11 | #[cfg(any( |
| 12 | target_os = "dragonfly", |
| 13 | target_os = "freebsd", |
| 14 | target_os = "netbsd", |
| 15 | target_os = "openbsd", |
| 16 | target_os = "macos" |
| 17 | ))] |
| 18 | mod kqueue; |
| 19 | #[cfg(any( |
| 20 | target_os = "dragonfly", |
| 21 | target_os = "freebsd", |
| 22 | target_os = "netbsd", |
| 23 | target_os = "openbsd", |
| 24 | target_os = "macos" |
| 25 | ))] |
| 26 | use kqueue::Kqueue as Poller; |
| 27 | |
| 28 | /// Possible modes for registering a file descriptor |
| 29 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 30 | pub enum Moderust-analyzer cargo calloop 0.10.5 sys/Mode# { |
| 31 | /// Single event generation |
| 32 | /// |
| 33 | /// This FD will be disabled as soon as it has generated one event. |
| 34 | /// |
| 35 | /// The user will need to use `LoopHandle::update()` to re-enable it if |
| 36 | /// desired. |
| 37 | OneShotrust-analyzer cargo calloop 0.10.5 sys/Mode#OneShot#, |
| 38 | /// Level-triggering |
| 39 | /// |
| 40 | /// This FD will report events on every poll as long as the requested interests |
| 41 | /// are available. If the same FD is inserted in multiple event loops, all of |
| 42 | /// them are notified of readiness. |
| 43 | Levelrust-analyzer cargo calloop 0.10.5 sys/Mode#Level#, |
| 44 | /// Edge-triggering |
| 45 | /// |
| 46 | /// This FD will report events only when it *gains* one of the requested interests. |
| 47 | /// it must thus be fully processed before it'll generate events again. If the same |
| 48 | /// FD is inserted on multiple event loops, it may be that not all of them are notified |
| 49 | /// of readiness, and not necessarily always the same(s) (at least one is notified). |
| 50 | Edgerust-analyzer cargo calloop 0.10.5 sys/Mode#Edge#, |
| 51 | } |
| 52 | |
| 53 | /// Interest to register regarding the file descriptor |
| 54 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 55 | pub struct Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# { |
| 56 | /// Wait for the FD to be readable |
| 57 | pub readablerust-analyzer cargo calloop 0.10.5 sys/Interest#readable.: bool, |
| 58 | /// Wait for the FD to be writable |
| 59 | pub writablerust-analyzer cargo calloop 0.10.5 sys/Interest#writable.: bool, |
| 60 | } |
| 61 | |
| 62 | impl Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# { |
| 63 | /// Shorthand for empty interest |
| 64 | pub const EMPTYrust-analyzer cargo calloop 0.10.5 sys/impl#[Interest]EMPTY.: Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# = Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# { |
| 65 | readablerust-analyzer cargo calloop 0.10.5 sys/Interest#readable.: false, |
| 66 | writablerust-analyzer cargo calloop 0.10.5 sys/Interest#writable.: false, |
| 67 | }; |
| 68 | /// Shorthand for read interest |
| 69 | pub const READrust-analyzer cargo calloop 0.10.5 sys/impl#[Interest]READ.: Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# = Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# { |
| 70 | readablerust-analyzer cargo calloop 0.10.5 sys/Interest#readable.: true, |
| 71 | writablerust-analyzer cargo calloop 0.10.5 sys/Interest#writable.: false, |
| 72 | }; |
| 73 | /// Shorthand for write interest |
| 74 | pub const WRITErust-analyzer cargo calloop 0.10.5 sys/impl#[Interest]WRITE.: Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# = Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# { |
| 75 | readablerust-analyzer cargo calloop 0.10.5 sys/Interest#readable.: false, |
| 76 | writablerust-analyzer cargo calloop 0.10.5 sys/Interest#writable.: true, |
| 77 | }; |
| 78 | /// Shorthand for read and write interest |
| 79 | pub const BOTHrust-analyzer cargo calloop 0.10.5 sys/impl#[Interest]BOTH.: Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# = Interestrust-analyzer cargo calloop 0.10.5 sys/Interest# { |
| 80 | readablerust-analyzer cargo calloop 0.10.5 sys/Interest#readable.: true, |
| 81 | writablerust-analyzer cargo calloop 0.10.5 sys/Interest#writable.: true, |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /// Readiness for a file descriptor notification |
| 86 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 87 | pub struct Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness# { |
| 88 | /// Is the FD readable |
| 89 | pub readablerust-analyzer cargo calloop 0.10.5 sys/Readiness#readable.: bool, |
| 90 | /// Is the FD writable |
| 91 | pub writablerust-analyzer cargo calloop 0.10.5 sys/Readiness#writable.: bool, |
| 92 | /// Is the FD in an error state |
| 93 | pub errorrust-analyzer cargo calloop 0.10.5 sys/Readiness#error.: bool, |
| 94 | } |
| 95 | |
| 96 | impl Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness# { |
| 97 | /// Shorthand for empty readiness |
| 98 | pub const EMPTYrust-analyzer cargo calloop 0.10.5 sys/impl#[Readiness]EMPTY.: Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness# = Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness# { |
| 99 | readablerust-analyzer cargo calloop 0.10.5 sys/Readiness#readable.: false, |
| 100 | writablerust-analyzer cargo calloop 0.10.5 sys/Readiness#writable.: false, |
| 101 | errorrust-analyzer cargo calloop 0.10.5 sys/Readiness#error.: false, |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 106 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) struct PollEventrust-analyzer cargo calloop 0.10.5 sys/PollEvent# { |
| 107 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) readinessrust-analyzer cargo calloop 0.10.5 sys/PollEvent#readiness.: Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness#, |
| 108 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) tokenrust-analyzer cargo calloop 0.10.5 sys/PollEvent#token.: Tokenrust-analyzer cargo calloop 0.10.5 sys/Token#, |
| 109 | } |
| 110 | |
| 111 | /// Factory for creating tokens in your registrations |
| 112 | /// |
| 113 | /// When composing event sources, each sub-source needs to |
| 114 | /// have its own token to identify itself. This factory is |
| 115 | /// provided to produce such unique tokens. |
| 116 | |
| 117 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 118 | pub struct TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory# { |
| 119 | keyrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#key.: CalloopKeyrust-analyzer cargo calloop 0.10.5 loop_logic/CalloopKey#, |
| 120 | sub_idrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#sub_id.: u32, |
| 121 | } |
| 122 | |
| 123 | impl TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory# { |
| 124 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) fn newrust-analyzer cargo calloop 0.10.5 sys/impl#[TokenFactory]new().(key: CalloopKeyrust-analyzer cargo calloop 0.10.5 loop_logic/CalloopKey#) -> TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory# { |
| 125 | TokenFactoryrust-analyzer cargo calloop 0.10.5 sys/TokenFactory# { keyrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#key., sub_idrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#sub_id.: 0 } |
| 126 | } |
| 127 | |
| 128 | /// Produce a new unique token |
| 129 | pub fn tokenrust-analyzer cargo calloop 0.10.5 sys/impl#[TokenFactory]token().(&mut self) -> Tokenrust-analyzer cargo calloop 0.10.5 sys/Token# { |
| 130 | let token = Tokenrust-analyzer cargo calloop 0.10.5 sys/Token# { |
| 131 | keyrust-analyzer cargo calloop 0.10.5 sys/Token#key.: self.keyrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#key., |
| 132 | sub_idrust-analyzer cargo calloop 0.10.5 sys/Token#sub_id.: self.sub_idrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#sub_id., |
| 133 | }; |
| 134 | self.sub_idrust-analyzer cargo calloop 0.10.5 sys/TokenFactory#sub_id. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().1; |
| 135 | token |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// A token (for implementation of the [`EventSource`](crate::EventSource) trait) |
| 140 | /// |
| 141 | /// This token is produced by the [`TokenFactory`] and is used when calling the |
| 142 | /// [`EventSource`](crate::EventSource) implementations to process event, in order |
| 143 | /// to identify which sub-source produced them. |
| 144 | /// |
| 145 | /// You should forward it to the [`Poll`] when registering your file descriptors. |
| 146 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 147 | pub struct Tokenrust-analyzer cargo calloop 0.10.5 sys/Token# { |
| 148 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) keyrust-analyzer cargo calloop 0.10.5 sys/Token#key.: CalloopKeyrust-analyzer cargo calloop 0.10.5 loop_logic/CalloopKey#, |
| 149 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) sub_idrust-analyzer cargo calloop 0.10.5 sys/Token#sub_id.: u32, |
| 150 | } |
| 151 | |
| 152 | /// The polling system |
| 153 | /// |
| 154 | /// This type represents the polling system of calloop, on which you |
| 155 | /// can register your file descriptors. This interface is only accessible in |
| 156 | /// implementations of the [`EventSource`](crate::EventSource) trait. |
| 157 | /// |
| 158 | /// You only need to interact with this type if you are implementing your |
| 159 | /// own event sources, while implementing the [`EventSource`](crate::EventSource) trait. |
| 160 | /// And even in this case, you can often just use the [`Generic`](crate::generic::Generic) event |
| 161 | /// source and delegate the implementations to it. |
| 162 | pub struct Pollrust-analyzer cargo calloop 0.10.5 sys/Poll# { |
| 163 | pollerrust-analyzer cargo calloop 0.10.5 sys/Poll#poller.: Pollerrust-analyzer cargo calloop 0.10.5 sys/epoll/Epoll#, |
| 164 | |
| 165 | // It is essential for safe use of this type that the pointers passed in to |
| 166 | // the underlying poller API are properly managed. Each time an event source |
| 167 | // is registered, the token it passes in is Boxed and converted to a raw |
| 168 | // pointer to be passed to the polling system by FFI. This pointer is what's |
| 169 | // stored in the map. When the event source is re- or unregistered, the same |
| 170 | // raw pointer can then be converted back into the Box and dropped, safely |
| 171 | // deallocating it. To put it another way, we effectively "own" the Token |
| 172 | // memory on behalf of the underlying polling mechanism. |
| 173 | // |
| 174 | // All the platforms we currently support follow the rule that file |
| 175 | // descriptors must be "small", positive integers. This means we can use a |
| 176 | // VecMap which has that exact constraint for its keys. If that ever |
| 177 | // changes, this will need to be changed to a different structure. |
| 178 | tokensrust-analyzer cargo calloop 0.10.5 sys/Poll#tokens.: VecMaprust-analyzer cargo vec_map 0.8.2 VecMap#<*mut Tokenrust-analyzer cargo calloop 0.10.5 sys/Token#>, |
| 179 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) timersrust-analyzer cargo calloop 0.10.5 sys/Poll#timers.: Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<TimerWheelrust-analyzer cargo calloop 0.10.5 sources/timer/TimerWheel#>>, |
| 180 | } |
| 181 | |
| 182 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Pollrust-analyzer cargo calloop 0.10.5 sys/Poll# { |
| 183 | #[cfg_attr(coverage, no_coverage)] |
| 184 | fn fmtrust-analyzer cargo calloop 0.10.5 sys/impl#[Poll][Debug]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 185 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("Poll { ... }") |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | impl Pollrust-analyzer cargo calloop 0.10.5 sys/Poll# { |
| 190 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) fn newrust-analyzer cargo calloop 0.10.5 sys/impl#[Poll]new().(high_precision: bool) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<Pollrust-analyzer cargo calloop 0.10.5 sys/Poll#> { |
| 191 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Pollrust-analyzer cargo calloop 0.10.5 sys/Poll# { |
| 192 | pollerrust-analyzer cargo calloop 0.10.5 sys/Poll#poller.: Pollerrust-analyzer cargo calloop 0.10.5 sys/epoll/Epoll#::newrust-analyzer cargo calloop 0.10.5 sys/epoll/impl#[Epoll]new().(high_precision)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 193 | tokensrust-analyzer cargo calloop 0.10.5 sys/Poll#tokens.: VecMaprust-analyzer cargo vec_map 0.8.2 VecMap#::newrust-analyzer cargo vec_map 0.8.2 impl#[`VecMap<V>`]new().(), |
| 194 | timersrust-analyzer cargo calloop 0.10.5 sys/Poll#timers.: Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T>`]new().(RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(TimerWheelrust-analyzer cargo calloop 0.10.5 sources/timer/TimerWheel#::newrust-analyzer cargo calloop 0.10.5 sources/timer/impl#[TimerWheel]new().())), |
| 195 | }) |
| 196 | } |
| 197 | |
| 198 | pub(craterust-analyzer cargo calloop 0.10.5 crate/) fn pollrust-analyzer cargo calloop 0.10.5 sys/impl#[Poll]poll().( |
| 199 | &mut self, |
| 200 | mut timeout: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#>, |
| 201 | ) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PollEventrust-analyzer cargo calloop 0.10.5 sys/PollEvent#>> { |
| 202 | let now = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(); |
| 203 | // adjust the timeout for the timers |
| 204 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next_timeout) = self.timersrust-analyzer cargo calloop 0.10.5 sys/Poll#timers..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().().next_deadlinerust-analyzer cargo calloop 0.10.5 sources/timer/impl#[TimerWheel]next_deadline().() { |
| 205 | if next_timeout rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().now { |
| 206 | timeout = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.); |
| 207 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(deadline) = timeout { |
| 208 | timeout = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(deadline, next_timeout rust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant][`Sub<Self>`]sub().-rust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant][`Sub<Self>`]sub(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant][`Sub<Self>`]sub().now)); |
| 209 | } else { |
| 210 | timeout = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next_timeout rust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant][`Sub<Self>`]sub().-rust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant][`Sub<Self>`]sub(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant][`Sub<Self>`]sub().now); |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | let mut events = self.pollerrust-analyzer cargo calloop 0.10.5 sys/Poll#poller..pollrust-analyzer cargo calloop 0.10.5 sys/epoll/impl#[Epoll]poll().(timeout)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 215 | |
| 216 | // Update 'now' as some time may have elapsed in poll() |
| 217 | let now = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(); |
| 218 | let mut timers = self.timersrust-analyzer cargo calloop 0.10.5 sys/Poll#timers..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 219 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_, token)) = timers.next_expiredrust-analyzer cargo calloop 0.10.5 sources/timer/impl#[TimerWheel]next_expired().(now) { |
| 220 | events.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(PollEventrust-analyzer cargo calloop 0.10.5 sys/PollEvent# { |
| 221 | readinessrust-analyzer cargo calloop 0.10.5 sys/PollEvent#readiness.: Readinessrust-analyzer cargo calloop 0.10.5 sys/Readiness# { |
| 222 | readablerust-analyzer cargo calloop 0.10.5 sys/Readiness#readable.: true, |
| 223 | writablerust-analyzer cargo calloop 0.10.5 sys/Readiness#writable.: false, |
| 224 | errorrust-analyzer cargo calloop 0.10.5 sys/Readiness#error.: false, |
| 225 | }, |
| 226 | tokenrust-analyzer cargo calloop 0.10.5 sys/PollEvent#token., |
| 227 | }); |
| 228 | } |
| 229 | |
| 230 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(events) |
| 231 | } |
| 232 | |
| 233 | /// Register a new file descriptor for polling |
| 234 | /// |
| 235 | /// The file descriptor will be registered with given interest, |
| 236 | /// mode and token. This function will fail if given a |
| 237 | /// bad file descriptor or if the provided file descriptor is already |
| 238 | /// registered. |
| 239 | /// |
| 240 | /// # Leaking tokens |
| 241 | /// |
| 242 | /// If your event source is dropped without being unregistered, the token |
| 243 | /// passed in here will remain on the heap and continue to be used by the |
| 244 | /// polling system even though no event source will match it. |
| 245 | pub fn registerrust-analyzer cargo calloop 0.10.5 sys/impl#[Poll]register().( |
| 246 | &mut self, |
| 247 | fd: RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#, |
| 248 | interest: Interestrust-analyzer cargo calloop 0.10.5 sys/Interest#, |
| 249 | mode: Moderust-analyzer cargo calloop 0.10.5 sys/Mode#, |
| 250 | token: Tokenrust-analyzer cargo calloop 0.10.5 sys/Token#, |
| 251 | ) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> { |
| 252 | let token_box = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(token); |
| 253 | let token_ptr = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::into_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]into_raw().(token_box); |
| 254 | |
| 255 | let registration_result = self.pollerrust-analyzer cargo calloop 0.10.5 sys/Poll#poller..registerrust-analyzer cargo calloop 0.10.5 sys/epoll/impl#[Epoll]register().(fd, interest, mode, token_ptr); |
| 256 | |
| 257 | if registration_result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 258 | // If registration did not work, do not add the file descriptor to |
| 259 | // the token map. Instead, reconstruct the Box and drop it. This is |
| 260 | // safe because it's from Box::into_raw() above. |
| 261 | let token_box = unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(token_ptr) }; |
| 262 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(token_box); |
| 263 | } else { |
| 264 | // Registration worked, keep the token pointer until it's replaced |
| 265 | // or removed. |
| 266 | let index = index_from_fdrust-analyzer cargo calloop 0.10.5 sys/index_from_fd().(fd); |
| 267 | if self.tokensrust-analyzer cargo calloop 0.10.5 sys/Poll#tokens..insertrust-analyzer cargo vec_map 0.8.2 impl#[`VecMap<V>`]insert().(index, token_ptr).is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 268 | // If there is already a file descriptor associated with a |
| 269 | // token, then replacing that entry will leak the token, but |
| 270 | // converting it back into a Box might leave a dangling pointer |
| 271 | // somewhere. We can theoretically continue safely by choosing |
| 272 | // to leak, but one of our assumptions is no longer valid, so |
| 273 | // panic. |
| 274 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("File descriptor ({}) already registered", fd); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | registration_result |
| 279 | } |
| 280 | |
| 281 | /// Update the registration for a file descriptor |
| 282 | /// |
| 283 | /// This allows you to change the interest, mode or token of a file |
| 284 | /// descriptor. Fails if the provided fd is not currently registered. |
| 285 | /// |
| 286 | /// See note on [`register()`](Self::register()) regarding leaking. |
| 287 | pub fn reregisterrust-analyzer cargo calloop 0.10.5 sys/impl#[Poll]reregister().( |
| 288 | &mut self, |
| 289 | fd: RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#, |
| 290 | interest: Interestrust-analyzer cargo calloop 0.10.5 sys/Interest#, |
| 291 | mode: Moderust-analyzer cargo calloop 0.10.5 sys/Mode#, |
| 292 | token: Tokenrust-analyzer cargo calloop 0.10.5 sys/Token#, |
| 293 | ) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> { |
| 294 | let token_box = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(token); |
| 295 | let token_ptr = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::into_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]into_raw().(token_box); |
| 296 | |
| 297 | let reregistration_result = self.pollerrust-analyzer cargo calloop 0.10.5 sys/Poll#poller..reregisterrust-analyzer cargo calloop 0.10.5 sys/epoll/impl#[Epoll]reregister().(fd, interest, mode, token_ptr); |
| 298 | |
| 299 | if reregistration_result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 300 | // If registration did not work, do not add the file descriptor to |
| 301 | // the token map. Instead, reconstruct the Box and drop it. This is |
| 302 | // safe because it's from Box::into_raw() above. |
| 303 | let token_box = unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(token_ptr) }; |
| 304 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(token_box); |
| 305 | } else { |
| 306 | // Registration worked, drop the old token memory and keep the new |
| 307 | // token pointer until it's replaced or removed. |
| 308 | let index = index_from_fdrust-analyzer cargo calloop 0.10.5 sys/index_from_fd().(fd); |
| 309 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(previous) = self.tokensrust-analyzer cargo calloop 0.10.5 sys/Poll#tokens..insertrust-analyzer cargo vec_map 0.8.2 impl#[`VecMap<V>`]insert().(index, token_ptr) { |
| 310 | // This is safe because it's from Box::into_raw() from a |
| 311 | // previous (re-)register() call. |
| 312 | let token_box = unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(previous) }; |
| 313 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(token_box); |
| 314 | } else { |
| 315 | // If there is no previous token registered for this file |
| 316 | // descriptor, either the event source has wrongly called |
| 317 | // reregister() without first being registered, or the |
| 318 | // underlying poller has a dangling pointer. In the first case, |
| 319 | // the reregistration should have failed; in the second case, we |
| 320 | // cannot safely proceed. |
| 321 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("File descriptor ({}) had no previous registration", fd); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | reregistration_result |
| 326 | } |
| 327 | |
| 328 | /// Unregister a file descriptor |
| 329 | /// |
| 330 | /// This file descriptor will no longer generate events. Fails if the |
| 331 | /// provided file descriptor is not currently registered. |
| 332 | pub fn unregisterrust-analyzer cargo calloop 0.10.5 sys/impl#[Poll]unregister().(&mut self, fd: RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#) -> craterust-analyzer cargo calloop 0.10.5 crate/::Resultrust-analyzer cargo calloop 0.10.5 error/Result#<()> { |
| 333 | let unregistration_result = self.pollerrust-analyzer cargo calloop 0.10.5 sys/Poll#poller..unregisterrust-analyzer cargo calloop 0.10.5 sys/epoll/impl#[Epoll]unregister().(fd); |
| 334 | |
| 335 | if unregistration_result.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() { |
| 336 | // The source was unregistered, we can remove the old token data. |
| 337 | let index = index_from_fdrust-analyzer cargo calloop 0.10.5 sys/index_from_fd().(fd); |
| 338 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(previous) = self.tokensrust-analyzer cargo calloop 0.10.5 sys/Poll#tokens..removerust-analyzer cargo vec_map 0.8.2 impl#[`VecMap<V>`]remove().(index) { |
| 339 | // This is safe because it's from Box::into_raw() from a |
| 340 | // previous (re-)register() call. |
| 341 | let token_box = unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(previous) }; |
| 342 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(token_box); |
| 343 | } else { |
| 344 | // If there is no previous token registered for this file |
| 345 | // descriptor, either the event source has wrongly called |
| 346 | // unregister() without first being registered, or the |
| 347 | // underlying poller has a dangling pointer. In the first case, |
| 348 | // the reregistration should have failed; in the second case, we |
| 349 | // cannot safely proceed. |
| 350 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("File descriptor ({}) had no previous registration", fd); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | unregistration_result |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /// Converts a file descriptor into an index for the token map. Panics if the |
| 359 | /// file descriptor is negative. |
| 360 | fn index_from_fdrust-analyzer cargo calloop 0.10.5 sys/index_from_fd().(fd: RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#) -> usize { |
| 361 | fd.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() |
| 362 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("File descriptor ({}) is invalid", fd)) |
| 363 | } |