timer_kit/
sleep.rs

1use std::{pin::Pin, time::{Duration}, future::Future};
2
3use crate::{Delay, Instant};
4
5/// Creates a new `Sleep` that completes after the specified duration.
6/// 
7/// # Example
8/// 
9/// Creates a sleep with smol's timer
10/// 
11/// ```rust,no_run
12/// use std::time::Duration;
13/// use timer_kit::sleep;
14/// 
15/// sleep::<smol::Timer>(Duration::from_millis(100)).await;
16/// ```
17/// 
18/// Creates a sleep with `fluvio_wasm_timer::Delay`
19/// 
20/// ```rust,no_run
21/// use std::time::Duration;
22/// use timer_kit::sleep;
23/// 
24/// sleep::<fluvio_wasm_timer::Delay>(Duration::from_millis(100)).await;
25/// ```
26pub fn sleep<D>(duration: Duration) -> Sleep<D> 
27where
28    D: Delay,
29{
30    Sleep::new(duration)
31}
32
33/// Creates a new `Sleep` that completes at the specified deadline
34/// 
35/// # Example
36/// 
37/// Creates a sleep with smol's timer
38/// 
39/// ```rust,no_run
40/// use std::time::{Duration, Instant};
41/// use timer_kit::sleep_until;
42/// 
43/// sleep_until::<smol::Timer>(Instant::now() + Duration::from_millis(100)).await;
44/// ```
45/// 
46/// Creates a sleep with `fluvio_wasm_timer::Delay`
47/// 
48/// ```rust,no_run
49/// use std::time::{Duration};
50/// use fluent_wasm_timer::Instant;
51/// use timer_kit::sleep_until;
52/// 
53/// sleep_until::<fluvio_wasm_timer::Delay>(Instant::now() + Duration::from_millis(100)).await;
54/// ```
55pub fn sleep_until<D>(deadline: D::Instant) -> Sleep<D> 
56where
57    D: Delay,
58{
59    Sleep::new_until(deadline)
60}
61
62/// A future that completes after a specified duration.
63/// 
64/// This future calls `Delay::poll_elapsed` internally.
65/// 
66/// # Type Parameter
67/// 
68/// - `D`: The underlying timer type that implements the [`Delay`] trait
69#[derive(Debug)]
70pub struct Sleep<D: Delay> {
71    delay: Pin<Box<D>>,
72    deadline: D::Instant,
73}
74
75impl<D> Sleep<D>
76where
77    D: Delay,
78{
79    /// Creates a new `Sleep` that completes after the specified duration.
80    /// 
81    /// # Example
82    /// 
83    /// Creates a sleep with smol's timer
84    /// 
85    /// ```rust,no_run
86    /// use std::time::Duration;
87    /// use timer_kit::Sleep;
88    /// 
89    /// let sleep = Sleep::<smol::Timer>::new(Duration::from_millis(100));
90    /// sleep.await;
91    /// ```
92    /// 
93    /// Creates a sleep with `fluvio_wasm_timer::Delay`
94    /// 
95    /// ```rust,no_run
96    /// use std::time::Duration;
97    /// use timer_kit::Sleep;
98    /// 
99    /// let sleep = Sleep::<fluvio_wasm_timer::Delay>::new(Duration::from_millis(100));
100    /// sleep.await;
101    /// ```
102    pub fn new(duration: Duration) -> Self 
103    {
104        let delay = Box::pin(D::delay(duration));
105        let deadline = delay.deadline().unwrap_or(D::Instant::now() + duration);
106        Self {
107            delay,
108            deadline,
109        }
110    }
111
112    /// Creates a new `Sleep` that completes at the specified deadline
113    /// 
114    /// # Example
115    /// 
116    /// Creates a sleep with smol's timer
117    /// 
118    /// ```rust,no_run
119    /// use std::time::{Duration, Instant};
120    /// use timer_kit::Sleep;
121    /// 
122    /// let sleep = Sleep::<smol::Timer>::new_until(Instant::now() + Duration::from_millis(100));
123    /// sleep.await;
124    /// ```
125    /// 
126    /// Creates a sleep with `fluvio_wasm_timer::Delay`
127    /// 
128    /// ```rust,no_run
129    /// use std::time::{Duration};
130    /// use fluent_wasm_timer::Instant;
131    /// use timer_kit::Sleep;
132    /// 
133    /// let sleep = Sleep::<fluvio_wasm_timer::Delay>::new_until(Instant::now() + Duration::from_millis(100));
134    /// sleep.await;
135    /// ```
136    pub fn new_until(deadline: D::Instant) -> Self {
137        Self {
138            delay: Box::pin(D::delay_until(deadline)),
139            deadline,
140        }
141    }
142
143    /// Reset the `Sleep` to a new deadline
144    pub fn reset(&mut self, deadline: D::Instant) {
145        self.deadline = deadline;
146        self.delay.as_mut().reset(deadline);
147    }
148
149    /// Gets the deadline
150    pub fn deadline(&self) -> D::Instant {
151        self.deadline
152    }
153}
154
155impl<D> Future for Sleep<D>
156where
157    D: Delay,
158    D::Instant: Unpin,
159{
160    type Output = D::Value;
161
162    fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
163        self.get_mut().delay.as_mut().poll_elapsed(cx)
164    }
165}