delay_queue/
delayed.rs

1use std::time::{Duration, Instant};
2
3/// A value that is delayed until some `Instant`.
4///
5/// The `DelayQueue` only accepts values that implement this trait.
6/// In most situations you do not need to implement this trait yourself. You can use the helper
7/// struct `Delay`.
8pub trait Delayed {
9    /// Returns the `Instant` until which this value is delayed.
10    fn delayed_until(&self) -> Instant;
11}
12
13/// Wraps a value that should be delayed.
14///
15/// Implements `Delayed` and `Eq`. Two `Delay` objects are equal iff their wrapped `value`s are
16/// equal and they are delayed until the same `Instant`.
17///
18/// # Examples
19///
20/// Basic usage:
21///
22/// ```
23/// use delay_queue::{Delay, Delayed};
24/// use std::time::{Duration, Instant};
25///
26/// let delayed_one_hour = Delay::for_duration(123, Duration::from_secs(3600));
27/// let delayed_now = Delay::until_instant("abc", Instant::now());
28///
29/// assert!(delayed_one_hour.delayed_until() > delayed_now.delayed_until());
30/// assert_eq!(delayed_one_hour.value, 123);
31/// ```
32#[derive(Debug, PartialEq, Eq)]
33pub struct Delay<T> {
34    /// The value that is delayed.
35    pub value: T,
36
37    /// The `Instant` until which `value` is delayed.
38    until: Instant,
39}
40
41impl<T> Delay<T> {
42    /// Creates a new `Delay<T>` holding `value` and that is delayed until the given `Instant`.
43    ///
44    /// # Examples
45    ///
46    /// Basic usage:
47    ///
48    /// ```
49    /// use delay_queue::Delay;
50    /// use std::time::Instant;
51    ///
52    /// let delayed_now = Delay::until_instant("abc", Instant::now());
53    /// ```
54    pub fn until_instant(value: T, until: Instant) -> Delay<T> {
55        Delay { value, until }
56    }
57
58    /// Creates a new `Delay<T>` holding `value` and that is delayed until the given `Duration` has
59    /// elapsed.
60    ///
61    /// # Examples
62    ///
63    /// Basic usage:
64    ///
65    /// ```
66    /// use delay_queue::Delay;
67    /// use std::time::Duration;
68    ///
69    /// let delayed_one_hour = Delay::for_duration("abc", Duration::from_secs(3600));
70    /// ```
71    pub fn for_duration(value: T, duration: Duration) -> Delay<T> {
72        Delay::until_instant(value, Instant::now() + duration)
73    }
74}
75
76impl<T> Delayed for Delay<T> {
77    fn delayed_until(&self) -> Instant {
78        self.until
79    }
80}
81
82impl<T: Default> Default for Delay<T> {
83    /// Creates an expired `Delay<T>` with a default `value`.
84    fn default() -> Delay<T> {
85        Delay {
86            value: Default::default(),
87            until: Instant::now(),
88        }
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use std::time::{Duration, Instant};
95    use super::{Delay, Delayed};
96
97    #[test]
98    fn compare_until() {
99        let delayed_one_hour = Delay::for_duration(123, Duration::from_secs(3600));
100        let delayed_now = Delay::until_instant("abc", Instant::now());
101
102        assert!(delayed_one_hour.delayed_until() > delayed_now.delayed_until());
103    }
104
105    #[test]
106    fn correct_value() {
107        let delayed_one_hour = Delay::for_duration(123, Duration::from_secs(3600));
108        let delayed_now = Delay::until_instant("abc", Instant::now());
109
110        assert_eq!(delayed_one_hour.value, 123);
111        assert_eq!(delayed_now.value, "abc");
112    }
113}