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
use std::{
fmt::{Display, Formatter},
time::Duration
};
use crate::{ TimerQueue, Timer };
pub enum CallbackHint {
QuickFunction,
SlowFunction
}
#[derive(Debug)]
pub enum TimerError {
OsError(isize, String)
}
pub type Result<T> = std::result::Result<T, TimerError>;
pub struct TimerHandle<'h>(Timer<'static, 'h>);
pub fn schedule_interval<'h, F>(interval: Duration, hint: Option<CallbackHint>, handler: F) -> Result<TimerHandle<'h>> where F: FnMut() + Send + 'h {
DEFAULT_QUEUE.schedule_timer(interval, interval, hint, handler).map(|t| TimerHandle(t))
}
pub fn schedule_oneshot<'h, F>(due: Duration, hint: Option<CallbackHint>, handler: F) -> Result<TimerHandle<'h>> where F: FnMut() + Send + 'h {
DEFAULT_QUEUE.schedule_timer(due, Duration::ZERO, hint, handler).map(|t| TimerHandle(t))
}
const DEFAULT_QUEUE: &TimerQueue = &TimerQueue::default();
impl Display for TimerError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
TimerError::OsError(code, msg) => write!(f, "OS error {code}: {msg}")
}
}
}
impl std::error::Error for TimerError {}