jag

typing_rust

public

A small typing test program in Rust

Star0
Watch0
d29b02a0114e7073424e954f22be89a5b68f41f0
1
ssh://git@git.jaglekon.de:30/jag/typing_rust.git
src/timer.rsRust38 lines753 B
1
use std::time::{
2
        Duration,
3
        Instant,
4
    };
5
6
#[derive(Debug)]
7
pub struct  Timer {
8
    start: Option<Instant>,
9
    duration: Duration,
10
}
11
12
impl Timer {
13
    pub fn new(duration: Duration) -> Self {
14
        Self {
15
            start: None,
16
            duration,
17
        }
18
    }
19
20
    pub fn start(&mut self) {
21
        self.start = Some(Instant::now());
22
    }
23
24
    pub fn remaining (&self) -> Duration {
25
        self.duration.saturating_sub(self.elapsed())
26
    }
27
28
    pub fn elapsed (&self) -> Duration {
29
        match self.start {
30
            Some(time) => if time.elapsed() < self.duration {
31
                time.elapsed()
32
            } else {
33
                self.duration
34
            },
35
            None => Duration::ZERO,
36
        }
37
    }
38
}