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
use std::time::{Duration, Instant}; /// A value that is delayed until some `Instant`. /// /// The `DelayQueue` only accepts values that implement this trait. /// In most situations you do not need to implement this trait yourself. You can use the helper /// struct `Delay`. pub trait Delayed { /// Returns the `Instant` until which this value is delayed. fn delayed_until(&self) -> Instant; } /// Wraps a value that should be delayed. /// /// Implements `Delayed` and `Eq`. Two `Delay` objects are equal iff their wrapped `value`s are /// equal and they are delayed until the same `Instant`. /// /// # Examples /// /// Basic usage: /// /// ``` /// use delay_queue::{Delay, Delayed}; /// use std::time::{Duration, Instant}; /// /// let delayed_one_hour = Delay::for_duration(123, Duration::from_secs(3600)); /// let delayed_now = Delay::until_instant("abc", Instant::now()); /// /// assert!(delayed_one_hour.delayed_until() > delayed_now.delayed_until()); /// assert_eq!(delayed_one_hour.value, 123); /// ``` #[derive(Debug, PartialEq, Eq)] pub struct Delay<T> { /// The value that is delayed. pub value: T, /// The `Instant` until which `value` is delayed. until: Instant, } impl<T> Delay<T> { /// Creates a new `Delay` holding `value` and that is delayed until the given `Instant`. /// /// # Examples /// /// Basic usage: /// /// ``` /// use delay_queue::Delay; /// use std::time::Instant; /// /// let delayed_now = Delay::until_instant("abc", Instant::now()); /// ``` pub fn until_instant(value: T, until: Instant) -> Delay<T> { Delay { value, until } } /// Creates a new `Delay` holding `value` and that is delayed until the given `Duration` has /// elapsed. /// /// # Examples /// /// Basic usage: /// /// ``` /// use delay_queue::Delay; /// use std::time::Duration; /// /// let delayed_one_hour = Delay::for_duration("abc", Duration::from_secs(3600)); /// ``` pub fn for_duration(value: T, duration: Duration) -> Delay<T> { Delay::until_instant(value, Instant::now() + duration) } } impl<T> Delayed for Delay<T> { fn delayed_until(&self) -> Instant { self.until } } #[cfg(test)] mod tests { use std::time::{Duration, Instant}; use super::{Delay, Delayed}; #[test] fn compare_until() { let delayed_one_hour = Delay::for_duration(123, Duration::from_secs(3600)); let delayed_now = Delay::until_instant("abc", Instant::now()); assert!(delayed_one_hour.delayed_until() > delayed_now.delayed_until()); } #[test] fn correct_value() { let delayed_one_hour = Delay::for_duration(123, Duration::from_secs(3600)); let delayed_now = Delay::until_instant("abc", Instant::now()); assert_eq!(delayed_one_hour.value, 123); assert_eq!(delayed_now.value, "abc"); } }