-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_scheduler.rs
88 lines (78 loc) · 2.67 KB
/
function_scheduler.rs
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
use super::mutable_xinput_state::{MutableXInputState};
use std::time::{SystemTime, Duration};
pub struct ScheduledFunction {
duration: std::time::Duration,
func: Box<dyn Fn(&MutableXInputState) -> ()>,
}
impl ScheduledFunction {
pub fn new(duration: Duration, func: Box<dyn Fn(&MutableXInputState) -> ()>) -> Self {
Self {
duration,
func,
}
}
}
impl ::std::fmt::Debug for ScheduledFunction {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "ScheduledFunction: Box<dyn Fn(&MutableXInputState) -> ()>")
}
}
#[derive(PartialEq, Debug)]
pub enum ScheduledFunctionState {
Ongoing { next_end: SystemTime },
Completed,
}
#[derive(Debug)]
pub struct ScheduledFunctionStack {
state: ScheduledFunctionState,
functions: Vec<ScheduledFunction>,
}
impl ScheduledFunctionStack {
pub fn new(functions: Vec<ScheduledFunction>) -> Self {
Self {
state: ScheduledFunctionState::Ongoing { next_end: SystemTime::now() + functions[0].duration },
functions,
}
}
pub fn poll(&mut self, controller_state: &MutableXInputState) -> ScheduledFunctionState {
match self.state {
ScheduledFunctionState::Ongoing { next_end } => {
if SystemTime::now() < next_end {
(self.functions[0].func)(controller_state);
ScheduledFunctionState::Ongoing { next_end }
} else {
self.functions.remove(0);
if self.functions.is_empty() {
self.state = ScheduledFunctionState::Completed;
ScheduledFunctionState::Completed
} else {
let next_end = next_end + self.functions[0].duration;
self.state = ScheduledFunctionState::Ongoing { next_end };
ScheduledFunctionState::Ongoing { next_end }
}
}
},
ScheduledFunctionState::Completed => ScheduledFunctionState::Completed,
}
}
}
unsafe impl Send for ScheduledFunctionStack {}
unsafe impl Sync for ScheduledFunctionStack {}
// a macro to make it easier to create a ScheduledFunctionStack
// format:
// [time to rerun function for in ms] => [function to run],
//
// example usage:
// scheduled_function_stack! {
// 2000 => |controller_state| { controller_state.set_west_button(ButtonState::UP) },
// 2000 => |controller_state| { controller_state.set_west_button(ButtonState::DOWN) },
// 2000 => |controller_state| { controller_state.set_west_button(ButtonState::UP) },
// }
#[macro_export]
macro_rules! scheduled_function_stack {
($($time_ms:literal => $func:expr),+ $(,)+) => {
ScheduledFunctionStack::new(vec![
$(ScheduledFunction::new(::std::time::Duration::from_millis($time_ms), Box::new($func))),+
])
};
}