extern crate time;
pub use self::time::{Duration, SteadyTime};
pub struct Stopwatch {
start_time: SteadyTime,
end_time: SteadyTime,
running: bool,
}
impl Stopwatch {
pub fn new() -> Stopwatch {
let initial_time = SteadyTime::now();
Stopwatch {
start_time: initial_time,
end_time: initial_time,
running: false,
}
}
pub fn elapsed(&self) -> Duration {
self.end_time - self.start_time
}
pub fn restart(&mut self) {
self.reset();
self.start();
}
pub fn start(&mut self) {
if !self.running {
if self.elapsed() == Duration::seconds(0) {
self.reset()
}
self.running = true;
}
}
pub fn stop(&mut self) {
if self.running {
self.end_time = SteadyTime::now();
self.running = false;
}
}
pub fn reset(&mut self) {
self.start_time = SteadyTime::now();
self.end_time = self.start_time;
}
}
#[cfg(test)]
mod tests {
use super::Stopwatch;
use std::thread;
use std::time;
#[test]
fn elapsed() {
let mut watch = Stopwatch::new();
watch.start();
thread::sleep(time::Duration::from_secs(2));
watch.stop();
assert_eq!(2, watch.elapsed().num_seconds());
}
#[test]
fn reset() {
let mut watch = Stopwatch::new();
watch.start();
thread::sleep(time::Duration::from_secs(2));
watch.stop();
watch.reset();
assert_eq!(0, watch.elapsed().num_nanoseconds().unwrap());
}
#[test]
fn restart() {
let mut watch = Stopwatch::new();
watch.start();
thread::sleep(time::Duration::from_secs(2));
watch.stop();
watch.restart();
thread::sleep(time::Duration::from_secs(1));
watch.stop();
assert_eq!(1, watch.elapsed().num_seconds());
}
}