use std::task::{Context, Poll};
use std::time::Duration;
#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux;
#[cfg(any(target_os = "linux", target_os = "android"))]
use linux as imp;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
))]
mod bsd;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
))]
use bsd as imp;
#[cfg(not(any(
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
)))]
compile_error!(
"the `periodic` feature is only supported on Linux, Android, \
macOS, iOS, FreeBSD, NetBSD, OpenBSD, and DragonFly BSD"
);
pub type Expirations = u64;
pub struct PeriodicInterval {
inner: imp::PeriodicTimer,
period: Duration,
}
impl PeriodicInterval {
pub fn new(period: Duration) -> std::io::Result<Self> {
assert!(period > Duration::ZERO, "`period` must be non-zero");
let inner = imp::PeriodicTimer::new(period)?;
Ok(Self { inner, period })
}
#[must_use]
pub fn period(&self) -> Duration {
self.period
}
pub async fn tick(&mut self) -> std::io::Result<Expirations> {
std::future::poll_fn(|cx| self.poll_tick(cx)).await
}
pub fn poll_tick(&mut self, cx: &mut Context<'_>) -> Poll<std::io::Result<Expirations>> {
self.inner.poll_tick(cx)
}
}
impl std::fmt::Debug for PeriodicInterval {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PeriodicInterval")
.field("period", &self.period)
.finish_non_exhaustive()
}
}