ticky 1.0.2

A stopwatch library, written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
	This file is part of Ticky.
	Ticky is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	Ticky is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	You should have received a copy of the GNU Affero General Public License
	along with Ticky.  If not, see <https://www.gnu.org/licenses/>.
*/

//! # Ticky
//! A simple stopwatch implementation.
//!
//! ## Example
//! ```rust
//! use ticky::Stopwatch;
//! let mut sw = Stopwatch::start_new();
//! // Do something …
//! sw.stop();
//! println!("Elapsed time: {}ms", sw.elapsed_ms_whole());
//! ```
//!
//! ## Features
//! - `derive_more` - Enables using [`derive_more`](https://crates.io/crates/derive_more) for deriving `From`, `Into`, `Mul`, `MulAssign`, `Div`, `DivAssign`, `Rem`, `Shr`, and `Shl` for `Stopwatch`.
//! - `hifitime` - Enables using [`hifitime`](https://crates.io/crates/hifitime) for high-resolution timekeeping.
//! - `stdtime` - Enables using [`std::time`](https://doc.rust-lang.org/std/time/index.html) for timekeeping.
//!
//! Either `hifitime` or `stdtime` must be enabled. If neither is enabled, `stdtime` is used by default. If both are enabled, `hifitime` is used.
//!
//! ## Installation
//! Run `cargo add ticky` to add Ticky to your `Cargo.toml` file.
//!
//! ## License
//! Ticky is licensed under the [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.en.html).
//!
//! ## Contributing
//! Contributions are welcome! Please see [`CONTRIBUTING.md`](https://github.com/Dirout/ticky/blob/master/CONTRIBUTING.md) for more information.
//!
//! ## Authors
//! - [Emil Sayahi](https://github.com/emmyoh)
//!
//! ## Acknowledgements
//! - [`rust-stopwatch`](https://crates.io/crates/stopwatch) - The inspiration for this crate.

#![no_std]
#![warn(missing_docs)]

#[cfg(feature = "std")]
extern crate std;

cfg_if::cfg_if! {
	if #[cfg(any(feature = "hifitime"))] {
		use hifitime::{Duration, Epoch, TimeUnits, Unit};

		#[cfg(feature = "derive_more")]
		use derive_more::{Div, DivAssign, From, Into, Mul, MulAssign, Rem, Shl, Shr};

		#[cfg_attr(
			feature = "derive_more",
			derive(From, Into, Mul, MulAssign, Div, DivAssign, Rem, Shr, Shl,)
		)]
		#[derive(Ord, Eq, PartialEq, PartialOrd, Clone, Copy, Debug, Hash)]
		/// A simple stopwatch implementation.
		/// ## Usage
		/// ```rust
		/// use ticky::Stopwatch;
		/// let mut sw = Stopwatch::start_new();
		/// // Do something …
		/// sw.stop();
		/// println!("Elapsed time: {}ms", sw.elapsed_ms_whole());
		/// ```
		pub struct Stopwatch {
			/// The total elapsed time.
			pub elapsed: Duration,
			/// The time at which the stopwatch was last started.
			pub timer: Duration,
			/// Whether the stopwatch is currently running.
			pub is_running: bool,
		}

		impl Display for Stopwatch {
			fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
				write!(f, "{}", self.elapsed())
			}
		}

		impl From<Duration> for Stopwatch {
			fn from(dur: Duration) -> Self {
				Self {
					elapsed: dur,
					timer: Epoch::now().unwrap().to_duration(),
					is_running: false,
				}
			}
		}

		impl Default for Stopwatch {
			fn default() -> Self {
				Self {
					elapsed: Duration::ZERO,
					timer: Epoch::now().unwrap().to_duration(),
					is_running: false,
				}
			}
		}

		impl Stopwatch {
			/// Starts (or resumes) the stopwatch.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch (pausing it)
			/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
			/// sw.start(); // Start the stopwatch again (resuming it)
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ms_whole().abs_diff(2_000) < 100); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn start(&mut self) {
				self.timer = Epoch::now().unwrap().to_duration();
				self.is_running = true;
			}

			/// Stops (or pauses) the stopwatch.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch (pausing it)
			/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
			/// sw.start(); // Start the stopwatch again (resuming it)
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ms_whole().abs_diff(2_000) < 100); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn stop(&mut self) {
				self.elapsed += (Epoch::now().unwrap().to_duration() - self.timer).abs();
				self.is_running = false;
			}

			/// Returns the total elapsed time.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!((sw.elapsed().to_unit(hifitime::prelude::Unit::Millisecond) - 1_000.0).abs() < 100.0); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn elapsed(&self) -> Duration {
				match self.is_running {
					true =>  self.elapsed + (Epoch::now().unwrap().to_duration() - self.timer).abs(),
					false => self.elapsed,
				}
			}

			/// Returns the total elapsed time in fractional milliseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!((sw.elapsed_ms() - 1_000.0).abs() < 100.0); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn elapsed_ms(&mut self) -> f64 {
				self.elapsed().to_unit(Unit::Millisecond)
			}

			/// Returns the total elapsed time in whole milliseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn elapsed_ms_whole(&mut self) -> u128 {
				self.elapsed().round(1.milliseconds()).to_unit(Unit::Millisecond).round() as u128
			}

			/// Returns the total elapsed time in fractional microseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_micros(1_000_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!((sw.elapsed_us() - 1_000_000.0).abs() < 10_000.0); // Allow for some error (± 10,000 microseconds)
			/// ```
			pub fn elapsed_us(&mut self) -> f64 {
				self.elapsed().to_unit(Unit::Microsecond)
			}

			/// Returns the total elapsed time in whole microseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_micros(1_000_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_us_whole().abs_diff(1_000_000) < 10_000); // Allow for some error (± 10,000 microseconds)
			/// ```
			pub fn elapsed_us_whole(&mut self) -> u128 {
				self.elapsed().round(1.microseconds()).to_unit(Unit::Microsecond).round() as u128
			}

			/// Returns the total elapsed time in fractional nanoseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_nanos(1_000_000_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!((sw.elapsed_ns() - 1_000_000_000.0).abs() < 100_000_000.0); // Allow for some error (± 100,000,000 nanoseconds)
			/// ```
			pub fn elapsed_ns(&mut self) -> f64 {
				self.elapsed().to_unit(Unit::Nanosecond)
			}

			/// Returns the total elapsed time in whole nanoseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_nanos(1_000_000_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ns_whole().abs_diff(1_000_000_000) < 100_000_000); // Allow for some error (± 100,000,000 nanoseconds)
			/// ```
			pub fn elapsed_ns_whole(&mut self) -> u128 {
				self.elapsed().total_nanoseconds().try_into().unwrap()
			}

			/// Returns the total elapsed time in fractional seconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_500)); // Wait for 1.5 seconds
			/// sw.stop(); // Stop the stopwatch
			/// assert!((sw.elapsed_s() - 1.5).abs() < 0.01); // Allow for some error (± 0.01 seconds)
			/// ```
			pub fn elapsed_s(&mut self) -> f64 {
				self.elapsed().to_unit(Unit::Second)
			}

			/// Returns the total elapsed time in whole seconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_500)); // Wait for 1.5 seconds
			/// sw.stop(); // Stop the stopwatch
			/// assert_eq!(sw.elapsed_s_whole(), 2);
			/// ```
			pub fn elapsed_s_whole(&self) -> u64 {
				self.elapsed().round(1.seconds()).to_seconds().round() as u64
			}
		}

	} else if #[cfg(feature = "stdtime")] {
		use core::time::Duration;

		#[cfg(feature = "std")]
		use std::time::Instant;

		#[cfg(feature = "derive_more")]
		use derive_more::{Div, DivAssign, From, Into, Mul, MulAssign, Rem, Shl, Shr};

		#[cfg_attr(
			feature = "derive_more",
			derive(From, Into, Mul, MulAssign, Div, DivAssign, Rem, Shr, Shl,)
		)]
		#[derive(Ord, Eq, PartialEq, PartialOrd, Clone, Copy, Debug, Hash)]
		/// A simple stopwatch implementation.
		/// ## Usage
		/// ```rust
		/// use ticky::Stopwatch;
		/// let mut sw = Stopwatch::start_new();
		/// // Do something …
		/// sw.stop();
		/// println!("Elapsed time: {}ms", sw.elapsed_ms_whole());
		/// ```
		pub struct Stopwatch {
			/// The total elapsed time.
			pub elapsed: Duration,
			/// The time at which the stopwatch was last started.
			pub timer: Instant,
			/// Whether the stopwatch is currently running.
			pub is_running: bool,
		}

		impl Display for Stopwatch {
			fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
				let elapsed_ms = self.clone().elapsed_ms_whole();
				write!(f, "{elapsed_ms}ms")
			}
		}

		impl From<Duration> for Stopwatch {
			fn from(dur: Duration) -> Self {
				Self {
					elapsed: dur,
					timer: Instant::now(),
					is_running: false,
				}
			}
		}

		impl Default for Stopwatch {
			fn default() -> Self {
				Self {
					elapsed: Duration::new(0, 0),
					timer: Instant::now(),
					is_running: false,
				}
			}
		}

		impl Stopwatch {
			/// Starts (or resumes) the stopwatch.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch (pausing it)
			/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
			/// sw.start(); // Start the stopwatch again (resuming it)
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ms_whole().abs_diff(2_000) < 100); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn start(&mut self) {
				self.timer = Instant::now();
				self.is_running = true;
			}

			/// Stops (or pauses) the stopwatch.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch (pausing it)
			/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
			/// sw.start(); // Start the stopwatch again (resuming it)
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ms_whole().abs_diff(2_000) < 100); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn stop(&mut self) {
				self.elapsed += self.timer.elapsed();
				self.is_running = false;
			}

			/// Returns the total elapsed time.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed().as_millis().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn elapsed(&self) -> Duration {
				match self.is_running {
					true => self.elapsed + self.timer.elapsed(),
					false => self.elapsed,
				}
			}

			/// Returns the total elapsed time in milliseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 100); // Allow for some error (± 100 milliseconds)
			/// ```
			pub fn elapsed_ms_whole(&mut self) -> u128 {
				self.elapsed().as_millis()
			}

			/// Returns the total elapsed time in microseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_micros(1_000_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_us_whole().abs_diff(1_000_000) < 10_000); // Allow for some error (± 10,000 microseconds)
			/// ```
			pub fn elapsed_us_whole(&mut self) -> u128 {
				self.elapsed().as_micros()
			}

			/// Returns the total elapsed time in nanoseconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_nanos(1_000_000_000)); // Wait for 1 second
			/// sw.stop(); // Stop the stopwatch
			/// assert!(sw.elapsed_ns_whole().abs_diff(1_000_000_000) < 100_000_000); // Allow for some error (± 100,000,000 nanoseconds)
			/// ```
			pub fn elapsed_ns_whole(&mut self) -> u128 {
				self.elapsed().as_nanos()
			}

			/// Returns the total elapsed time in fractional seconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_500)); // Wait for 1.5 seconds
			/// sw.stop(); // Stop the stopwatch
			/// assert!((sw.elapsed_s() - 1.5).abs() < 0.01); // Allow for some error (± 0.01 seconds)
			/// ```
			pub fn elapsed_s(&mut self) -> f64 {
				self.elapsed().as_secs_f64()
			}

			/// Returns the total elapsed time in whole seconds.
			///
			/// # Example
			/// ```rust
			/// use ticky::Stopwatch;
			///
			/// let mut sw = Stopwatch::new(); // Create a new stopwatch
			/// sw.start(); // Start the stopwatch
			/// std::thread::sleep(std::time::Duration::from_millis(1_500)); // Wait for 1.5 seconds
			/// sw.stop(); // Stop the stopwatch
			/// assert_eq!(sw.elapsed_s_whole(), 1);
			/// ```
			pub fn elapsed_s_whole(&self) -> u64 {
				self.elapsed().as_secs()
			}
		}
	} else {
		compile_error!("You must enable either the `stdtime` or `hifitime` feature.");
	}
}

use core::fmt::{Display, Formatter};

impl From<Stopwatch> for Duration {
	fn from(sw: Stopwatch) -> Self {
		sw.elapsed()
	}
}

impl Stopwatch {
	/// Creates a new stopwatch.
	///
	/// # Example
	/// ```rust
	/// use ticky::Stopwatch;
	///
	/// let mut sw = Stopwatch::new(); // Create a new stopwatch
	/// sw.start(); // Start the stopwatch
	/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
	/// sw.stop(); // Stop the stopwatch
	/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
	/// ```
	pub fn new() -> Stopwatch {
		Stopwatch::default()
	}

	/// Creates a new stopwatch and starts it.
	///
	/// # Example
	/// ```rust
	/// use ticky::Stopwatch;
	///
	/// let mut sw = Stopwatch::start_new(); // Create a new stopwatch, and start it
	/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
	/// sw.stop(); // Stop the stopwatch
	/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
	/// ```
	pub fn start_new() -> Stopwatch {
		let mut sw = Stopwatch::new();
		sw.start();
		sw
	}

	/// Resets the stopwatch.
	///
	/// # Example
	/// ```rust
	/// use ticky::Stopwatch;
	///
	/// let mut sw = Stopwatch::new(); // Create a new stopwatch
	/// sw.start(); // Start the stopwatch
	/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
	/// sw.stop(); // Stop the stopwatch
	/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
	/// sw.reset(); // Reset the stopwatch
	/// assert!(sw.elapsed_ms_whole().abs_diff(0) < 10); // Allow for some error (± 10 milliseconds)
	/// sw.start(); // Start the stopwatch
	/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
	/// sw.stop(); // Stop the stopwatch
	/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
	pub fn reset(&mut self) {
		*self = Stopwatch::new();
	}

	/// Resets and starts the stopwatch.
	///
	/// # Example
	/// ```rust
	/// use ticky::Stopwatch;
	///
	/// let mut sw = Stopwatch::new(); // Create a new stopwatch
	/// sw.start(); // Start the stopwatch
	/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
	/// sw.stop(); // Stop the stopwatch
	/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
	/// sw.restart(); // Reset and start the stopwatch
	/// std::thread::sleep(std::time::Duration::from_millis(1_000)); // Wait for 1 second
	/// sw.stop(); // Stop the stopwatch
	/// assert!(sw.elapsed_ms_whole().abs_diff(1_000) < 10); // Allow for some error (± 10 milliseconds)
	pub fn restart(&mut self) {
		self.reset();
		self.start();
	}

	/// Returns true if the stopwatch is running, and false if not.
	///
	/// # Example
	/// ```rust
	/// use ticky::Stopwatch;
	///
	/// let mut sw = Stopwatch::new(); // Create a new stopwatch
	/// assert_eq!(sw.is_running(), false); // The stopwatch is not running
	/// sw.start(); // Start the stopwatch
	/// assert_eq!(sw.is_running(), true); // The stopwatch is running
	/// sw.stop(); // Stop the stopwatch
	/// assert_eq!(sw.is_running(), false); // The stopwatch is not running
	/// ```
	pub fn is_running(&mut self) -> bool {
		self.is_running
	}
}