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
use ;
use crateHandle;
/// The future returned by [`sleep`]. Does nothing until awaited.
///
/// It stores the moment to wake up rather than the duration, so being polled
/// early or late doesn't stretch the wait.
/// Pause the current task for `duration`, letting other tasks run meanwhile.
///
/// This is the async cousin of `std::thread::sleep`. That one puts a whole
/// thread to sleep; this one only parks the task, so the worker thread moves
/// straight on to something else.
///
/// ```
/// use nezuko::{Runtime, sleep};
/// use std::time::{Duration, Instant};
///
/// let rt = Runtime::new().unwrap();
///
/// rt.block_on(async {
/// let start = Instant::now();
/// sleep(Duration::from_millis(50)).await;
/// assert!(start.elapsed() >= Duration::from_millis(50));
/// });
/// ```
///
/// Nothing happens until you `.await` it - `sleep(d);` on its own is a no-op.
/// Timing is best-effort: you get *at least* `duration`, maybe a little more.
///
/// # Panics
///
/// Panics when awaited outside a running runtime, since there is no reactor to
/// register the timer with.
/// Either the moment has arrived, or we leave our waker in the runtime's timer
/// list and let the reactor call it when the clock catches up.