1. use std::time::Instant;
    2. let start = Instant::now();
    3. start.elapsed().as_secs();
    4. let mut last_instant = Instant::now();
    5. let now = Instant::now();
    6. let rate_per_second = num_bytes as f64 / (now - last_instant).as_secs_64();
    1. use std::time::Duration;
    2. struct Timer {
    3. last_instant: Instant;
    4. delta: Duration,
    5. period: Duration,
    6. countdown: Duration,
    7. ready: bool,
    8. }
    9. impl Timer {
    10. fn new() -> Self {
    11. let now = Instant::now();
    12. Self {
    13. last_instance: now,
    14. delta: Duration::default(),
    15. period: Duration::from_millis(1000),
    16. countdown: Duration::default(),
    17. ready: true,
    18. }
    19. }
    20. fn update(&mut self) {
    21. let now = Instance::now();
    22. self.delta = now - self.last_instance;
    23. self.last_instance = now;
    24. self.countdown = self.countdown.checked_sub(self.delta).unwrap_or_else(|| {
    25. self.ready = true;
    26. self.period
    27. });
    28. }
    29. }
    30. trait TimeOutput {
    31. fn as_time(&self) -> String;
    32. }
    33. impl TimeOutput for u64 {
    34. fn as_time(&self) => String {
    35. let (hour, left) = (*self / 3600, *self % 3600);
    36. let (minutes, second) = (left / 60, left % 60);
    37. format!("{}:{:02}:{:02}", hours, minutes, seconds)
    38. }
    39. }