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
#![deny(missing_docs, unsafe_code)]

//! This crate provides timer types for measuring time in a program in different ways.
//! - `Timer` is a timer that counts up and knows how much time has passed since it was started.
//! - `EggTimer` is a timer that counts down from its set `Duration` and knows how much time it has left.
//! - `Stopwatch` is a timer that counts up and can be paused and resumed.
//!
//! In addition to the timer types, a collection type, `TimedList`, is provided,
//! which associates each element with a `Duration` and only retains elements whose `Duration` has not elapsed.

use std::time::{Duration, Instant};

/// A trait for types that can be turned into a `Duration`
pub trait ToDuration {
    /// Convert the value into the `Duration`
    fn to_duration(&self) -> Duration;
}

impl ToDuration for Duration {
    fn to_duration(&self) -> Duration {
        *self
    }
}

impl ToDuration for f32 {
    fn to_duration(&self) -> Duration {
        if self < &0.0 {
            panic!("Attempted to convert negative f32 number to Duration");
        }
        let whole = *self as u64;
        let fract = (self.fract() * 1e9) as u32;
        Duration::new(whole, fract)
    }
}

impl ToDuration for f64 {
    fn to_duration(&self) -> Duration {
        if self < &0.0 {
            panic!("Attempted to convert negative f64 number to Duration");
        }
        let whole = *self as u64;
        let fract = (self.fract() * 1e9) as u32;
        Duration::new(whole, fract)
    }
}

impl ToDuration for u8 {
    fn to_duration(&self) -> Duration {
        Duration::new(u64::from(*self), 0)
    }
}

impl ToDuration for u16 {
    fn to_duration(&self) -> Duration {
        Duration::new(u64::from(*self), 0)
    }
}

impl ToDuration for u32 {
    fn to_duration(&self) -> Duration {
        Duration::new(u64::from(*self), 0)
    }
}

impl ToDuration for u64 {
    fn to_duration(&self) -> Duration {
        Duration::new(*self, 0)
    }
}

impl ToDuration for u128 {
    fn to_duration(&self) -> Duration {
        Duration::new(*self as u64, 0)
    }
}

impl ToDuration for usize {
    fn to_duration(&self) -> Duration {
        Duration::new(*self as u64, 0)
    }
}

/// A trait for types that can be created from a `Duration`
pub trait FromDuration {
    /// Create the value from a `Duration`
    fn from_duration(duration: Duration) -> Self;
}

impl FromDuration for Duration {
    fn from_duration(duration: Duration) -> Self {
        duration
    }
}

impl FromDuration for u64 {
    fn from_duration(duration: Duration) -> Self {
        duration.as_secs()
    }
}

impl FromDuration for u128 {
    fn from_duration(duration: Duration) -> Self {
        u128::from(duration.as_secs())
    }
}

impl FromDuration for usize {
    fn from_duration(duration: Duration) -> Self {
        duration.as_secs() as usize
    }
}

impl FromDuration for f32 {
    fn from_duration(duration: Duration) -> Self {
        duration.as_secs() as f32 + duration.subsec_nanos() as f32 / 1e9
    }
}

impl FromDuration for f64 {
    fn from_duration(duration: Duration) -> Self {
        duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) / 1e9
    }
}

/// A simple timer that knows how long since it started
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timer {
    start: Instant,
}

impl Timer {
    /// Creates a new `Timer`
    pub fn start() -> Timer {
        Timer {
            start: Instant::now(),
        }
    }
    /// Restarts the `Timer`
    pub fn reset(&mut self) {
        self.start = Instant::now();
    }
    /// Gets the elapsed time as a floating-point number of seconds
    pub fn elapsed(self) -> f64 {
        f64::from_duration(self.duration())
    }
    /// Get the elapsed time as a `Duration`
    pub fn duration(self) -> Duration {
        Instant::now().duration_since(self.start)
    }
    /// Gets the `Instant` at which the `Timer` was started
    pub fn started_at(self) -> Instant {
        self.start
    }
}

impl Default for Timer {
    fn default() -> Self {
        Timer::start()
    }
}

/// A timer that counts down and knows when a `Duration` has elapsed
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EggTimer {
    timer: Timer,
    duration: Duration,
}

impl EggTimer {
    /// Creates a new `EggTimer`
    pub fn set<D: ToDuration>(time: D) -> EggTimer {
        EggTimer {
            timer: Timer::start(),
            duration: time.to_duration(),
        }
    }
    /// Resets the `EggTimer`
    pub fn reset(&mut self) {
        self.timer = Timer::start();
    }
    /// Gets the time left as a `Duration`
    pub fn duration_left(&self) -> Option<Duration> {
        self.duration.checked_sub(self.timer.duration())
    }
    /// Gets the time left as a floating-point number of seconds
    pub fn seconds_left(&self) -> f64 {
        f64::from_duration(self.duration) - self.timer.elapsed()
    }
    /// Checks if the set `Duration` has elapsed
    pub fn is_ready(self) -> bool {
        self.duration_left().is_none()
    }
    /// Gets the time the `EggTimer` was originally set with as a `Duration`
    pub fn max_duration(&self) -> Duration {
        self.duration
    }
    /// Gets the time the `EggTimer` was originally set with as a floating-point number of seconds
    pub fn max_seconds(&self) -> f64 {
        f64::from_duration(self.max_duration())
    }
    /// Gets the elapsed time as a floating-point number of seconds
    pub fn elapsed(&self) -> f64 {
        self.timer.elapsed()
    }
    /// Get the elapsed time as a `Duration`
    pub fn duration(&self) -> Duration {
        self.timer.duration()
    }
    /// Gets the `Instant` at which the `EggTimer` was started
    pub fn started_at(&self) -> Instant {
        self.timer.started_at()
    }
    /// Gets the `Instant` at which the `EggTimer` will or did end
    pub fn ends_at(&self) -> Instant {
        self.timer.started_at() + self.duration
    }
}

/// A timer that can be paused and resumed.
///
/// The reported elapsed times do not include periods when the timer was paused
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Stopwatch {
    last_start: Instant,
    prev_dur: Duration,
    paused: bool,
}

impl Stopwatch {
    /// Creates a new `Stopwatch` which immediately starts counting
    pub fn start() -> Stopwatch {
        Stopwatch {
            last_start: Instant::now(),
            prev_dur: 0u64.to_duration(),
            paused: false,
        }
    }
    /// Creates a new `Stopwatch` which starts paused
    pub fn start_paused() -> Stopwatch {
        Stopwatch {
            last_start: Instant::now(),
            prev_dur: 0u64.to_duration(),
            paused: true,
        }
    }
    /// Restarts the `Stopwatch` without pausing or resuming
    pub fn reset(&mut self) {
        self.last_start = Instant::now();
        self.prev_dur = 0u64.to_duration();
    }
    /// Gets the elapsed time as a floating-point number of seconds
    pub fn elapsed(&self) -> f64 {
        f64::from_duration(self.duration())
    }
    /// Gets the elapsed time as a `Duration`
    pub fn duration(&self) -> Duration {
        if self.paused {
            self.prev_dur
        } else {
            self.prev_dur + Instant::now().duration_since(self.last_start)
        }
    }
    /// Pauses the `Stopwatch`
    pub fn pause(&mut self) {
        if !self.paused {
            self.prev_dur += Instant::now().duration_since(self.last_start);
        }
    }
    /// Resumes the `Stopwatch`
    pub fn resume(&mut self) {
        if self.paused {
            self.last_start = Instant::now();
        }
    }
    /// Toggles whether the `Stopwatch` is paused or resumed
    pub fn toggle(&mut self) {
        if self.paused {
            self.pause();
        } else {
            self.resume();
        }
    }
    /// Gets the `Instant` at which the `Stopwatch` was last resumed
    pub fn started_at(&self) -> Instant {
        self.last_start
    }
}

impl Default for Stopwatch {
    fn default() -> Self {
        Stopwatch::start()
    }
}

/// An iterable list structure where each element has an associated `Duration`.
///
/// When an element's `Duration` has elapsed, the element is removed from the
/// list upon the next mutable function call. Timed-out elements will never be iterated over.
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct TimedList<T> {
    list: Vec<(EggTimer, T)>,
}

impl<T> TimedList<T> {
    /// Creates a new `TimedList`
    pub fn new() -> TimedList<T> {
        TimedList { list: Vec::new() }
    }
    /// Inserts an element into the list with the given number of floating-point seconds
    pub fn insert<D: ToDuration>(&mut self, element: T, time: D) {
        self.list.push((EggTimer::set(time), element));
    }
    /// Forces the removal of all elements whose `Duration` has elpased.
    /// This method does not need to be called manually unless you
    /// want to explicitely free the memory of timed-out elements immediately.
    pub fn clean(&mut self) {
        self.list.retain(|(timer, _)| !timer.is_ready());
    }
    /// Removes all elements from the list
    pub fn clear(&mut self) {
        self.list.clear();
    }
    /// Gets the number of elements in the list that have not timed out.
    pub fn len(&self) -> usize {
        self.iter().count()
    }
    /// Check if the list is empty or if all existing elements have timed out.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Retains elements in the list that match the predicate
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&T) -> bool,
    {
        self.list.retain(|(_, elem)| f(elem));
    }
    /// Iterates immutably through all elements.
    ///
    /// While this method does not remove timed-out elements,
    /// it does filter them out.
    /// If iteration takes sufficiently long, elements that
    /// may have been valid when iteration began may be skipped
    /// when they are actually iterated over.
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> {
        self.list.iter().filter_map(
            |(timer, elem)| {
                if timer.is_ready() {
                    None
                } else {
                    Some(elem)
                }
            },
        )
    }
    /// Iterates mutably through all elements.
    ///
    /// If iteration takes sufficiently long, elements that
    /// may have been valid when iteration began may be skipped
    /// when they are actually iterated over.
    pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
        self.clean();
        self.list.iter_mut().filter_map(
            |(timer, elem)| {
                if timer.is_ready() {
                    None
                } else {
                    Some(elem)
                }
            },
        )
    }
    /// Iterates immutably through all elements and their timers.
    ///
    /// While this method does not remove timed-out elements,
    /// it does filter them out.
    /// If iteration takes sufficiently long, elements that
    /// may have been valid when iteration began may be skipped
    /// when they are actually iterated over.
    pub fn timer_iter(&self) -> impl DoubleEndedIterator<Item = (&T, EggTimer)> {
        self.list.iter().filter_map(|(timer, elem)| {
            if timer.is_ready() {
                None
            } else {
                Some((elem, *timer))
            }
        })
    }
    /// Iterates mutably through all elements.
    ///
    /// If iteration takes sufficiently long, elements that
    /// may have been valid when iteration began may be skipped
    /// when they are actually iterated over.
    pub fn timer_iter_mut(&mut self) -> impl DoubleEndedIterator<Item = (&mut T, EggTimer)> {
        self.clean();
        self.list.iter_mut().filter_map(|(timer, elem)| {
            if timer.is_ready() {
                None
            } else {
                Some((elem, *timer))
            }
        })
    }
}

impl<T, D> std::iter::FromIterator<(T, D)> for TimedList<T>
where
    D: ToDuration,
{
    fn from_iter<I: IntoIterator<Item = (T, D)>>(iter: I) -> Self {
        TimedList {
            list: iter
                .into_iter()
                .map(|(x, d)| (EggTimer::set(d), x))
                .collect(),
        }
    }
}

impl<T> IntoIterator for TimedList<T>
where
    T: 'static,
{
    type Item = T;
    type IntoIter = Box<DoubleEndedIterator<Item = T>>;
    fn into_iter(mut self) -> Self::IntoIter {
        self.clean();
        Box::new(self.list.into_iter().filter_map(
            |(timer, elem)| {
                if timer.is_ready() {
                    None
                } else {
                    Some(elem)
                }
            },
        ))
    }
}

/**
Measure the amount of time the given function takes to execute

Time is measured in floating-point number of seconds

# Example
```
use eggtimer::measure;

let elapsed = measure(|| {
    for i in 0..1000 {
        println!("{}", i);
    }
});

println!("Printing all those numbers took {} seconds", elapsed);
```
*/
pub fn measure<F>(f: F) -> f64
where
    F: FnOnce(),
{
    let timer = Timer::start();
    f();
    timer.elapsed()
}