| author | holly sparkles <sparkles@holly.sh> | 2024-01-14 18:30:43.0 +00:00:00 |
|---|---|---|
| committer | holly sparkles <sparkles@holly.sh> | 2024-01-14 18:30:43.0 +00:00:00 |
| commit | 061eea0f03733b7b04d02d0b2379ab23c4c35050 [patch] |
|
| tree | 67dcb348f6b2ce852c8089c3ee18f7f2aa1c2561 |
|
| parent | 65a84cf9e7688712836b17bf768c131339a28951 |
|
| download | 061eea0f03733b7b04d02d0b2379ab23c4c35050.tar.gz |
|
feat: add stopwatch functionality
Diff
Cargo.lock | 7 +++- Cargo.toml | 8 +++- src/lib.rs | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 168 insertions(+) diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3dab0cb # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "stopwatch" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ebfd5b0 [] = "stopwatch" = "0.1.0" = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..fd40ead use fmt; use ; /// Stopwatch is a simple utility for measuring elapsed time. /// The time when the stopwatch was started. start_time: , /// The total elapsed time. elapsed_time: Duration, } /// Creates a new `Stopwatch` with initial values. /// /// # Examples /// /// ``` /// use std::time::Duration; /// use stopwatch::Stopwatch; /// /// let stopwatch = Stopwatch::new(); /// /// assert_eq!(stopwatch.elapsed(), Duration::from_secs(0)); /// ``` Stopwatch start_time: None, elapsed_time: from_secs, } } /// Starts the stopwatch. If the stopwatch is already running, this has no effect. /// /// # Examples /// /// ``` /// use stopwatch::Stopwatch; /// /// let mut stopwatch = Stopwatch::new(); /// /// stopwatch.start(); /// /// assert_eq!(stopwatch.is_running(), true); /// ``` if self.start_time.is_none self.start_time = Some; } } /// Stops the stopwatch and adds the elapsed time since the start to the total elapsed time. /// If the stopwatch is not running, this has no effect. /// /// # Examples /// /// ``` /// use std::time::Duration; /// use stopwatch::Stopwatch; /// /// let mut stopwatch = Stopwatch::new(); /// /// stopwatch.start(); /// std::thread::sleep(std::time::Duration::from_secs(1)); /// stopwatch.stop(); /// /// assert!(!stopwatch.is_running()) /// ``` if let Some = self.start_time self.elapsed_time += now - start_time; self.start_time = None; } } /// Resets the stopwatch. If the stopwatch is currently running, stops it before resetting. /// /// # Examples /// /// ``` /// use std::time::Duration; /// use stopwatch::Stopwatch; /// /// let mut stopwatch = Stopwatch::new(); /// /// stopwatch.start(); /// std::thread::sleep(std::time::Duration::from_secs(1)); /// stopwatch.reset(); /// /// assert_eq!(stopwatch.elapsed(), Duration::from_secs(0)); /// ``` if self.is_running self.stop; } self.start_time = None; self.elapsed_time = from_secs; } /// Gets the total elapsed time. If the stopwatch is running, adds the time since the start. /// /// # Examples /// /// ``` /// use std::time::Duration; /// use stopwatch::Stopwatch; /// /// let mut stopwatch = Stopwatch::new(); /// /// stopwatch.start(); /// std::thread::sleep(Duration::from_secs(1)); /// /// assert_eq!(stopwatch.elapsed().as_secs(), 1); /// ``` if let Some = self.start_time if self.is_running return now.duration_since; } } self.elapsed_time } /// Checks if the stopwatch is currently running. /// /// # Examples /// /// ``` /// use std::time::Duration; /// use stopwatch::Stopwatch; /// /// let mut stopwatch = Stopwatch::new(); /// assert!(!stopwatch.is_running()); /// /// stopwatch.start(); /// assert!(stopwatch.is_running()); /// /// std::thread::sleep(Duration::from_secs(1)); /// /// stopwatch.stop(); /// assert!(!stopwatch.is_running()); /// ``` self.start_time.is_some } } write! } }