1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use core::{mem,time};
extern crate alloc;
use alloc::boxed::Box;
#[cfg(target_pointer_width = "16")]
type FatPtr = u32;
#[cfg(target_pointer_width = "32")]
type FatPtr = u64;
#[cfg(target_pointer_width = "64")]
type FatPtr = u128;
#[cfg(windows)]
mod win32;
#[cfg(windows)]
pub use win32::*;
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod apple;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub use apple::*;
#[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::*;
unsafe impl Send for Timer {}
unsafe impl Sync for Timer {}
impl Timer {
#[inline(always)]
pub const fn schedule(&self) -> Schedule<'_> {
Schedule {
timer: self,
timeout: time::Duration::from_secs(0),
interval: time::Duration::from_secs(0),
}
}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
#[inline(always)]
pub fn schedule_once(&self, timeout: time::Duration) -> bool {
self.schedule_interval(timeout, time::Duration::from_secs(0))
}
}
pub struct Schedule<'a> {
timer: &'a Timer,
timeout: time::Duration,
interval: time::Duration,
}
impl<'a> Schedule<'a> {
#[inline(always)]
pub const fn initial(mut self, timeout: time::Duration) -> Self {
self.timeout = timeout;
self
}
#[inline(always)]
pub const fn interval(mut self, timeout: time::Duration) -> Self {
self.interval = timeout;
self
}
#[inline(always)]
pub fn schedule(&self) -> bool {
self.timer.schedule_interval(self.timeout, self.interval)
}
}
struct BoxFnPtr(pub FatPtr);
impl BoxFnPtr {
#[inline(always)]
const fn new() -> Self {
Self(0)
}
#[inline(always)]
const fn is_null(&self) -> bool {
self.0 == 0
}
}
impl Drop for BoxFnPtr {
#[inline(always)]
fn drop(&mut self) {
if !self.is_null() {
unsafe {
let _ = Box::from_raw(mem::transmute::<_, *mut dyn FnMut()>(self.0));
}
}
}
}