mango_core/
time.rs

1use core::time::Duration;
2
3/// Manager for time.
4#[derive(Default)]
5pub struct StaticTimeManager
6{
7  get_uptime_fn: Option<fn() -> Duration>,
8}
9
10impl StaticTimeManager
11{
12  /// Set the function that returns the uptime
13  pub fn set_uptime_fn(&mut self, cb: fn() -> Duration)
14  {
15    self.get_uptime_fn = Some(cb);
16  }
17  /// Function that return the uptime.
18  pub fn get_uptime(&self) -> Option<Duration>
19  {
20    if let Some(cb) = self.get_uptime_fn
21    {
22      return Some(cb());
23    }
24    else
25    {
26      return None;
27    }
28  }
29  /// Function that return the uptime. The value `default_uptime` (in nanoseconds) is used if not uptime callback function is defined.
30  pub fn get_uptime_or(&self, default_uptime: u64) -> Duration
31  {
32    if let Some(upt) = self.get_uptime()
33    {
34      return upt;
35    }
36    else
37    {
38      return Duration::from_nanos(default_uptime);
39    }
40  }
41}