Skip to main content

osom_lib_wait_timer/platforms/
mod.rs

1use core::time::Duration;
2
3use crate::traits::{MAX_WAIT_DURATION, WaitTimer};
4
5osom_lib_cfg_ext::cfg_match!(
6    (target_os="windows") => {
7        mod windows_timer;
8        use windows_timer::WindowsWaitTimer as PlaformWaitTimer;
9    },
10    (any(target_os="macos", target_os="linux")) => {
11        mod libc_timer;
12        use libc_timer::LibcWaitTimer as PlaformWaitTimer;
13    },
14    _ => {
15        compile_error!("Current target is not supported.");
16    }
17);
18
19/// The platform dependent implementation of [`WaitTimer`]. This struct is guaranteed
20/// to have size at most 8.
21#[derive(Default)]
22#[repr(transparent)]
23#[must_use]
24pub struct TheWaitTimer {
25    inner: PlaformWaitTimer,
26}
27
28impl TheWaitTimer {
29    /// Creates a new [`TheWaitTimer`].
30    #[inline(always)]
31    pub fn new() -> Self {
32        Self {
33            inner: PlaformWaitTimer::new(),
34        }
35    }
36
37    /// Sleeps for the given duration, blocking the thread. This function aims
38    /// to have 1ms resolution, assuming the underlying platform allows it.
39    ///
40    /// # Panics
41    ///
42    /// When `dur` is above [`MAX_WAIT_DURATION`].
43    #[inline(always)]
44    pub fn wait(&mut self, dur: Duration) {
45        assert!(
46            dur <= MAX_WAIT_DURATION,
47            ".wait() cannot be called with duration above MAX_WAIT_DURATION."
48        );
49        self.inner.wait(dur);
50    }
51}
52
53impl WaitTimer for TheWaitTimer {
54    #[inline(always)]
55    fn wait(&mut self, dur: Duration) {
56        self.wait(dur);
57    }
58}
59
60const _CHECK: () = const {
61    assert!(size_of::<TheWaitTimer>() <= 8, "Size of TheWaitTimer cannot exceed 8");
62};