Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_datime/src/time/stopwatch.rs

1.3 KiB, 18 runs

created by r1870400018:6393, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
2//! Anthropic Claude
3
4use oxedyne_fe2o3_core::prelude::*;
5
6#[derive(Clone, Debug)]
7pub struct StopWatch {
8 start_time: Option<std::time::Instant>,
9}
10
11impl StopWatch {
12 pub fn new() -> Self {
13 Self { start_time: None }
14 }
15
16 pub fn start(&mut self) {
17 self.start_time = Some(std::time::Instant::now());
18 }
19
20 pub fn stop(&mut self) -> Outcome<u64> {
21 if let Some(start) = self.start_time.take() {
22 let elapsed = start.elapsed();
23 Ok(elapsed.as_nanos() as u64)
24 } else {
25 Err(err!("Stopwatch not started"; Invalid))
26 }
27 }
28}
29
30#[derive(Clone, Debug)]
31pub struct StopWatchMillis {
32 start_time: Option<std::time::Instant>,
33}
34
35impl StopWatchMillis {
36 pub fn new() -> Self {
37 Self { start_time: None }
38 }
39
40 pub fn start(&mut self) {
41 self.start_time = Some(std::time::Instant::now());
42 }
43
44 pub fn stop(&mut self) -> Outcome<u64> {
45 if let Some(start) = self.start_time.take() {
46 let elapsed = start.elapsed();
47 Ok(elapsed.as_millis() as u64)
48 } else {
49 Err(err!("Stopwatch not started"; Invalid))
50 }
51 }
52
53 pub fn tic(&mut self) {
54 self.start();
55 }
56
57 pub fn toc(&self) -> u64 {
58 if let Some(start) = self.start_time {
59 let elapsed = start.elapsed();
60 elapsed.as_millis() as u64
61 } else {
62 0
63 }
64 }
65}