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
//! Timing module for frame-based applications
//!
//! Contains methods for slowing down to a fixed framerate, as well as
//! measuring actual frames per second.
//!
//! An example game loop:
//!
//! ```ignore
//! use std::time;
//! use ticktock::{Clock, Timer};
//!
//! let now = time::Instant::now();
//!
//! // initialize game
//! // ...
//!
//! // show some fps measurements every 5 seconds
//! let mut fps_counter = Timer::apply(|delta_t, prev_tick| (delta_t, *prev_tick), 0)
//!     .every(time::Duration::from_secs(5))
//!     .start(now);
//!
//! // run with a constant framerate of 30 fps
//! for (tick, now) in Clock::framerate(30.0).iter() {
//!     // this loop will run approx. every 33.3333 ms
//!
//!     // update, render, etc
//!     // ...
//!
//!     // update or display fps count
//!     if let Some((delta_t, prev_tick)) = fps_counter.update(now) {
//!         fps_counter.set_value(tick);
//!
//!         let fps = (tick - prev_tick) as f64 / delta_t.as_secs_f64();
//!         println!("FPS: {}", fps);
//!     }
//!     break; // ignore, for doctests
//! }
//! ```

pub mod clock;
pub mod delay;
pub mod throttled_io;
pub mod timer;

pub use crate::clock::Clock;
pub use crate::timer::Timer;

/// Iterator attempt
///
/// Given an iterator of outcomes, iterates returning either
///
/// * the first successful outcome
/// * the last unsuccessful outcome
/// * `None` if the iterator was empty
///
/// `Result` is often used as an outcome, e.g. when trying to reconnect multiple times:
///
/// ```rust
/// use std::net::TcpStream;
/// use std::time::Duration;
/// use ticktock::delay::Delay;
/// use ticktock::Attempt;
///
/// const RETRY_DELAY: Duration = Duration::from_millis(250);
///
/// // attempt to connect to localhost:12348 three times, before giving up.
/// // in total, 500 ms of delay will be inserted
/// let conn = Delay::new(RETRY_DELAY)
///     .map(|_| TcpStream::connect("localhost:12348"))
///     .take(3)
///     .attempt()
///     .unwrap();
///
/// # // our test will fail, because there is noting listening at 12348
/// # assert!(conn.is_err());
/// ```
///
/// `Option` is also a valid outcome:
///
/// ```ignore
/// let credentials = vec![("Bob", "secret"), ("Jeff", "hunter2"), ("John", "swordfish")];
///
/// fn try_login(username: &str, password: &str) -> Option<(String, String)> { ... }
///
/// // brute-force our way in
/// let valid_credentials: Option<(String, String)> = credentials
///                                                       .map(|(u, p)| try_login(u, p))
///                                                       .attempt()
///                                                       .unwrap();
/// ```

// note: this could probably be expressed more cleanly by using associated types
// (i.e. `type Outcome = ...`), but a bug in the rust compiler at the time of this writing
// did not allow for it https://github.com/rust-lang/rust/issues/20400

pub trait Attempt<O> {
    /// Consumes until the successful outcome is encountered. In case of failure, returns the last
    /// unsuccessful outcome.
    fn attempt(self) -> Option<O>;
}

impl<T, E, I> Attempt<Result<T, E>> for I
where
    I: Iterator<Item = Result<T, E>>,
{
    fn attempt(self) -> Option<Result<T, E>> {
        let mut rv = None;

        for res in self {
            rv = Some(res);

            // do not keep going if we got an Ok
            if let Some(Ok(_)) = rv {
                break;
            }
        }

        rv
    }
}

impl<T, I> Attempt<Option<T>> for I
where
    I: Iterator<Item = Option<T>>,
{
    fn attempt(self) -> Option<Option<T>> {
        let mut rv = None;

        for res in self {
            rv = Some(res);

            // do not keep going if we got an Ok
            if let Some(Some(_)) = rv {
                break;
            }
        }

        rv
    }
}

#[cfg(test)]
mod test {
    use super::Attempt;

    #[test]
    fn attempt_works_on_ok_results() {
        let rs = vec![Err(1), Err(2), Err(3), Ok(4), Ok(5)];

        assert_eq!(Some(Ok(4)), rs.into_iter().attempt());
    }

    #[test]
    fn attempt_works_on_err_results() {
        let rs: Vec<Result<(), _>> = vec![Err(1), Err(2), Err(3)];

        assert_eq!(Some(Err(3)), rs.into_iter().attempt());
    }

    #[test]
    fn attempt_works_on_empty_result_vecs() {
        let rs: Vec<Result<(), ()>> = Vec::new();

        assert_eq!(None, rs.into_iter().attempt());
    }

    #[test]
    fn attempt_works_on_some_options() {
        let rs = vec![None, None, None, Some(4), Some(5)];

        assert_eq!(Some(Some(4)), rs.into_iter().attempt());
    }

    #[test]
    fn attempt_works_on_none_options() {
        let rs: Vec<Option<()>> = vec![None, None, None];

        assert_eq!(Some(None), rs.into_iter().attempt());
    }

    #[test]
    fn attempt_works_on_empty_option_vecs() {
        let rs: Vec<Option<()>> = Vec::new();

        assert_eq!(None, rs.into_iter().attempt());
    }
}