jag

typing_rust

public

A small typing test program in Rust

Star0
Watch0
ssh://git@git.jaglekon.de:30/jag/typing_rust.git
src/timer.rsRust35 lines668 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
        self.start
30
            .map(
31
                |time| time.elapsed().min(self.duration)
32
            )
33
            .unwrap_or(Duration::ZERO)
34
    }
35
}