use core::{time, task};
use core::future::Future;
use crate::state::TimerState;
pub trait Timer: Send + Sync + Unpin + Future<Output=()> {
fn new(timeout: time::Duration) -> Self;
fn is_ticking(&self) -> bool;
fn is_expired(&self) -> bool;
fn restart(&mut self, timeout: time::Duration);
fn restart_ctx(&mut self, timeout: time::Duration, waker: &task::Waker);
fn cancel(&mut self);
}
pub trait SyncTimer: Timer {
fn init<R, F: Fn(&TimerState) -> R>(&mut self, init: F) -> R;
#[inline(always)]
fn tick(&mut self) -> bool {
self.init(|state| state.is_done())
}
}
#[inline(always)]
fn poll_sync<T: SyncTimer>(timer: &mut T, ctx: &mut task::Context) -> task::Poll<()> {
timer.init(|state| {
state.register(ctx.waker());
match state.is_done() {
true => task::Poll::Ready(()),
false => task::Poll::Pending
}
})
}
#[cfg(windows)]
mod win;
#[cfg(windows)]
pub use win::WinTimer;
#[cfg(windows)]
pub type Platform = win::WinTimer;
#[cfg(windows)]
pub type SyncPlatform = win::WinTimer;
#[cfg(all(feature = "tokio1", unix))]
mod async_tokio1;
#[cfg(all(feature = "tokio1", unix))]
pub use async_tokio1::AsyncTimer;
#[cfg(all(feature = "tokio1", unix))]
pub type Platform = AsyncTimer;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
mod posix;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
pub use posix::PosixTimer;
#[cfg(all(not(feature = "tokio1"), not(any(target_os = "macos", target_os = "ios")), unix))]
pub type Platform = posix::PosixTimer;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
pub type SyncPlatform = posix::PosixTimer;
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod apple;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub use apple::AppleTimer;
#[cfg(all(not(feature = "tokio1"), any(target_os = "macos", target_os = "ios")))]
pub type Platform = apple::AppleTimer;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub type SyncPlatform = apple::AppleTimer;
#[cfg(target_arch = "wasm32")]
mod web;
#[cfg(target_arch = "wasm32")]
pub use web::WebTimer;
#[cfg(target_arch = "wasm32")]
pub type Platform = web::WebTimer;
#[cfg(target_arch = "wasm32")]
pub type SyncPlatform = web::WebTimer;
mod dummy;
pub use dummy::DummyTimer;
#[cfg(not(any(windows, target_arch = "wasm32", unix)))]
pub type Platform = dummy::DummyTimer;
#[cfg(not(any(windows, target_arch = "wasm32", unix)))]
pub type SyncPlatform = dummy::DummyTimer;
#[inline]
pub const fn new_timer(timeout: time::Duration) -> Platform {
Platform::new(timeout)
}
#[inline]
pub const fn new_sync_timer(timeout: time::Duration) -> SyncPlatform {
SyncPlatform::new(timeout)
}