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
// libsw: stopwatch library
// copyright (C) 2022-2023 Ula Shipman <ula.hello@mailbox.org>
// licensed under MIT OR Apache-2.0
/* TODO: this is very basic and that limits how useful it is.
# it'd be nice if:
## guards could overlap and mask/invert eachother
```text
sw: ....!!!...........!!..!!!......
guard1: ^ created ^ dropped
guard2: ^ created ^ dropped
guard3: ^ created
^ dropped
```
## you could give it a closure to run on the stopwatch when dropped
struct GuardFn { inner: &mut Stopwatch, callback: FnOnce(&mut Stopwatch) }
impl Drop for GuardFn {
fn drop(&mut self) {
(self.callback)(self.inner);
}
}
*/
use crate::;
/// A running, guarded, [stopwatch](StopwatchImpl). When [dropped](Guard::drop),
/// the stopwatch will automatically stop.
///
/// `Guard`s are returned by the `StopwatchImpl` methods
/// [`guard`](StopwatchImpl::guard) and [`guard_at`](StopwatchImpl::guard_at).
///
/// # Examples
///
/// ```
/// # use libsw::Sw;
/// # use core::time::Duration;
/// # use std::thread;
/// # fn main() -> libsw::Result<()> {
/// let mut sw = Sw::new();
/// {
/// let _guard = sw.guard()?;
/// // stopwatch is now running and guarded!
/// thread::sleep(Duration::from_millis(100));
/// // guard dropped, stopwatch stopped
/// }
/// assert!(sw.is_stopped());
/// assert!(sw.elapsed() >= Duration::from_millis(100));
/// # Ok(())
/// # }
/// ```