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
use thunk::Thunk;
use traits::{Receiver};
use {MaridError, Runner, Signal};
pub type FnRunner = Thunk<'static, Receiver<Signal>, Result<(), MaridError>>;
impl FnRunner {
pub fn new<F>(func: F) -> FnRunner
where F: FnOnce(Receiver<Signal>) -> Result<(), MaridError>, F: Send + 'static {
Thunk::with_arg(func)
}
}
impl Runner for FnRunner {
fn run(self: Box<Self>, signals: Receiver<Signal>) -> Result<(), MaridError> {
(*self).invoke(signals)
}
fn setup(&mut self) -> Result<(), MaridError> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use {Runner, FnRunner};
use chan;
#[test]
fn test_thunk_runner() {
let (_sn, rc) = chan::sync(1);
let mut runner = Box::new(FnRunner::new(move |_sig| {
Ok(())
}));
assert!(runner.setup().is_ok());
assert!(runner.run(rc).is_ok());
}
}