icentral_timer/
timer.rs

1crate::ix!();
2
3pub struct Timer {
4    start: RefCell<Instant>,
5    stop:  RefCell<Option<Instant>>,
6}
7
8impl Default for Timer {
9
10    fn default() -> Self {
11
12        debug!("creating timer");
13
14        Self {
15            start: RefCell::new(Instant::now()),
16            stop:  RefCell::new(None),
17        }
18    }
19}
20
21impl Timer {
22
23    pub fn start(&mut self)  {
24        
25        debug!("starting timer");
26
27        *self.start.borrow_mut() = Instant::now();
28    }
29    
30    pub fn stop(&mut self)  {
31
32        debug!("stopping timer");
33        
34        *self.stop.borrow_mut() = Some(Instant::now());
35    }
36    
37    pub fn interval(&mut self) -> Duration {
38
39        if self.stop.borrow().is_none() {
40            self.stop();
41        }
42        
43        let interval = self.stop.borrow().unwrap() - *self.start.borrow();
44
45        debug!("computed timer interval: {:?}", interval);
46
47        interval
48    }
49}