aboutsummaryrefslogtreecommitdiff
path: root/src/counters/timer.rs
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2020-08-25 22:10:25 +0200
committerSébastien Crozet <developer@crozet.re>2020-08-25 22:10:25 +0200
commit754a48b7ff6d8c58b1ee08651e60112900b60455 (patch)
tree7d777a6c003f1f5d8f8d24f533f35a95a88957fe /src/counters/timer.rs
downloadrapier-754a48b7ff6d8c58b1ee08651e60112900b60455.tar.gz
rapier-754a48b7ff6d8c58b1ee08651e60112900b60455.tar.bz2
rapier-754a48b7ff6d8c58b1ee08651e60112900b60455.zip
First public release of Rapier.v0.1.0
Diffstat (limited to 'src/counters/timer.rs')
-rw-r--r--src/counters/timer.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/counters/timer.rs b/src/counters/timer.rs
new file mode 100644
index 0000000..fd25063
--- /dev/null
+++ b/src/counters/timer.rs
@@ -0,0 +1,53 @@
+use std::fmt::{Display, Error, Formatter};
+
+/// A timer.
+#[derive(Copy, Clone, Debug, Default)]
+pub struct Timer {
+ time: f64,
+ start: Option<f64>,
+}
+
+impl Timer {
+ /// Creates a new timer initialized to zero and not started.
+ pub fn new() -> Self {
+ Timer {
+ time: 0.0,
+ start: None,
+ }
+ }
+
+ /// Resets the timer to 0.
+ pub fn reset(&mut self) {
+ self.time = 0.0
+ }
+
+ /// Start the timer.
+ pub fn start(&mut self) {
+ self.time = 0.0;
+ self.start = Some(instant::now());
+ }
+
+ /// Pause the timer.
+ pub fn pause(&mut self) {
+ if let Some(start) = self.start {
+ self.time += instant::now() - start;
+ }
+ self.start = None;
+ }
+
+ /// Resume the timer.
+ pub fn resume(&mut self) {
+ self.start = Some(instant::now());
+ }
+
+ /// The measured time between the last `.start()` and `.pause()` calls.
+ pub fn time(&self) -> f64 {
+ self.time
+ }
+}
+
+impl Display for Timer {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+ write!(f, "{}s", self.time)
+ }
+}