| 1 |
#[cfg(unix)] |
| 2 |
mod unix; |
| 3 |
#[cfg(windows)] |
| 4 |
mod windows; |
| 5 |
|
| 6 |
use std::time::{Duration, Instant}; |
| 7 |
|
| 8 |
#[cfg(unix)] |
| 9 |
pub(crate) use unix::{UnixEventSource, UnixWaker}; |
| 10 |
#[cfg(windows)] |
| 11 |
pub(crate) use windows::{WindowsEventSource, WindowsWaker}; |
| 12 |
|
| 13 |
#[cfg(unix)] |
| 14 |
pub(crate) type PlatformEventSource = UnixEventSource; |
| 15 |
#[cfg(windows)] |
| 16 |
pub(crate) type PlatformEventSource = WindowsEventSource; |
| 17 |
|
| 18 |
#[cfg(unix)] |
| 19 |
pub(crate) type PlatformWaker = UnixWaker; |
| 20 |
#[cfg(windows)] |
| 21 |
pub(crate) type PlatformWaker = WindowsWaker; |
| 22 |
|
| 23 |
|
| 24 |
pub(crate) trait EventSource: Send + Sync { |
| 25 |
fn try_read(&mut self, timeout: Option<Duration>) -> std::io::Result<Option<crate::Event>>; |
| 26 |
|
| 27 |
fn waker(&self) -> PlatformWaker; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
#[derive(Debug, Clone)] |
| 32 |
pub(crate) struct PollTimeout { |
| 33 |
timeout: Option<Duration>, |
| 34 |
start: Instant, |
| 35 |
} |
| 36 |
|
| 37 |
impl PollTimeout { |
| 38 |
pub fn new(timeout: Option<Duration>) -> Self { |
| 39 |
Self { |
| 40 |
timeout, |
| 41 |
start: Instant::now(), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
pub fn elapsed(&self) -> bool { |
| 46 |
self.timeout |
| 47 |
.map(|timeout| self.start.elapsed() >= timeout) |
| 48 |
.unwrap_or(false) |
| 49 |
} |
| 50 |
|
| 51 |
pub fn leftover(&self) -> Option<Duration> { |
| 52 |
self.timeout.map(|timeout| { |
| 53 |
let elapsed = self.start.elapsed(); |
| 54 |
|
| 55 |
if elapsed >= timeout { |
| 56 |
Duration::ZERO |
| 57 |
} else { |
| 58 |
timeout - elapsed |
| 59 |
} |
| 60 |
}) |
| 61 |
} |
| 62 |
} |