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 core::time::Duration;

/// Manager for time.
#[derive(Default)]
pub struct StaticTimeManager
{
  get_uptime_fn: Option<fn() -> Duration>,
}

impl StaticTimeManager
{
  /// Set the function that returns the uptime
  pub fn set_uptime_fn(&mut self, cb: fn() -> Duration)
  {
    self.get_uptime_fn = Some(cb);
  }
  /// Function that return the uptime.
  pub fn get_uptime(&self) -> Option<Duration>
  {
    if let Some(cb) = self.get_uptime_fn
    {
      return Some(cb());
    }
    else
    {
      return None;
    }
  }
  /// Function that return the uptime. The value `default_uptime` (in nanoseconds) is used if not uptime callback function is defined.
  pub fn get_uptime_or(&self, default_uptime: u64) -> Duration
  {
    if let Some(upt) = self.get_uptime()
    {
      return upt;
    }
    else
    {
      return Duration::from_nanos(default_uptime);
    }
  }
}