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
use std::{pin::Pin, time::{Duration}, future::Future};
use crate::{Delay, Instant};
/// Creates a new `Sleep` that completes after the specified duration.
///
/// # Example
///
/// Creates a sleep with smol's timer
///
/// ```rust,no_run
/// use std::time::Duration;
/// use timer_kit::sleep;
///
/// sleep::<smol::Timer>(Duration::from_millis(100)).await;
/// ```
///
/// Creates a sleep with `fluvio_wasm_timer::Delay`
///
/// ```rust,no_run
/// use std::time::Duration;
/// use timer_kit::sleep;
///
/// sleep::<fluvio_wasm_timer::Delay>(Duration::from_millis(100)).await;
/// ```
pub fn sleep<D>(duration: Duration) -> Sleep<D>
where
D: Delay,
{
Sleep::new(duration)
}
/// Creates a new `Sleep` that completes at the specified deadline
///
/// # Example
///
/// Creates a sleep with smol's timer
///
/// ```rust,no_run
/// use std::time::{Duration, Instant};
/// use timer_kit::sleep_until;
///
/// sleep_until::<smol::Timer>(Instant::now() + Duration::from_millis(100)).await;
/// ```
///
/// Creates a sleep with `fluvio_wasm_timer::Delay`
///
/// ```rust,no_run
/// use std::time::{Duration};
/// use fluent_wasm_timer::Instant;
/// use timer_kit::sleep_until;
///
/// sleep_until::<fluvio_wasm_timer::Delay>(Instant::now() + Duration::from_millis(100)).await;
/// ```
pub fn sleep_until<D>(deadline: D::Instant) -> Sleep<D>
where
D: Delay,
{
Sleep::new_until(deadline)
}
/// A future that completes after a specified duration.
///
/// This future calls `Delay::poll_elapsed` internally.
///
/// # Type Parameter
///
/// - `D`: The underlying timer type that implements the [`Delay`] trait
#[derive(Debug)]
pub struct Sleep<D: Delay> {
delay: Pin<Box<D>>,
deadline: D::Instant,
}
impl<D> Sleep<D>
where
D: Delay,
{
/// Creates a new `Sleep` that completes after the specified duration.
///
/// # Example
///
/// Creates a sleep with smol's timer
///
/// ```rust,no_run
/// use std::time::Duration;
/// use timer_kit::Sleep;
///
/// let sleep = Sleep::<smol::Timer>::new(Duration::from_millis(100));
/// sleep.await;
/// ```
///
/// Creates a sleep with `fluvio_wasm_timer::Delay`
///
/// ```rust,no_run
/// use std::time::Duration;
/// use timer_kit::Sleep;
///
/// let sleep = Sleep::<fluvio_wasm_timer::Delay>::new(Duration::from_millis(100));
/// sleep.await;
/// ```
pub fn new(duration: Duration) -> Self
{
let delay = Box::pin(D::delay(duration));
let deadline = delay.deadline().unwrap_or(D::Instant::now() + duration);
Self {
delay,
deadline,
}
}
/// Creates a new `Sleep` that completes at the specified deadline
///
/// # Example
///
/// Creates a sleep with smol's timer
///
/// ```rust,no_run
/// use std::time::{Duration, Instant};
/// use timer_kit::Sleep;
///
/// let sleep = Sleep::<smol::Timer>::new_until(Instant::now() + Duration::from_millis(100));
/// sleep.await;
/// ```
///
/// Creates a sleep with `fluvio_wasm_timer::Delay`
///
/// ```rust,no_run
/// use std::time::{Duration};
/// use fluent_wasm_timer::Instant;
/// use timer_kit::Sleep;
///
/// let sleep = Sleep::<fluvio_wasm_timer::Delay>::new_until(Instant::now() + Duration::from_millis(100));
/// sleep.await;
/// ```
pub fn new_until(deadline: D::Instant) -> Self {
Self {
delay: Box::pin(D::delay_until(deadline)),
deadline,
}
}
/// Reset the `Sleep` to a new deadline
pub fn reset(&mut self, deadline: D::Instant) {
self.deadline = deadline;
self.delay.as_mut().reset(deadline);
}
/// Gets the deadline
pub fn deadline(&self) -> D::Instant {
self.deadline
}
}
impl<D> Future for Sleep<D>
where
D: Delay,
D::Instant: Unpin,
{
type Output = D::Value;
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
self.get_mut().delay.as_mut().poll_elapsed(cx)
}
}