@@ -61,18 +61,67 @@ pub use interval::Interval;
61
61
use std:: time:: { Duration , Instant } ;
62
62
63
63
/// Sleep the current future for the given duration.
64
+ ///
65
+ /// ## Examples
66
+ /// ```
67
+ /// # #![feature(async_await)]
68
+ /// use runtime::time::delay_for;
69
+ /// use std::time::{Duration, Instant};
70
+ ///
71
+ /// # #[runtime::main]
72
+ /// # async fn main () -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
73
+ /// let start = Instant::now();
74
+ /// let now = delay_for(Duration::from_millis(20)).await;
75
+ ///
76
+ /// assert!(now - start >= Duration::from_millis(20));
77
+ /// # Ok(())}
78
+ /// ```
64
79
#[ inline]
65
80
pub fn delay_for ( dur : Duration ) -> Delay {
66
81
Delay :: new ( dur)
67
82
}
68
83
69
84
/// Sleep the current future until the given time.
85
+ ///
86
+ /// ## Examples
87
+ /// ```
88
+ /// # #![feature(async_await)]
89
+ /// use runtime::time::delay_until;
90
+ /// use std::time::{Duration, Instant};
91
+ ///
92
+ /// # #[runtime::main]
93
+ /// # async fn main () -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
94
+ /// let start = Instant::now();
95
+ /// let now = delay_until(start + Duration::from_millis(40)).await;
96
+ ///
97
+ /// assert!(now - start >= Duration::from_millis(40));
98
+ /// # Ok(())}
99
+ /// ```
70
100
#[ inline]
71
101
pub fn delay_until ( at : Instant ) -> Delay {
72
102
Delay :: new_at ( at)
73
103
}
74
104
75
105
/// Create a stream that fires events at a set interval.
106
+ ///
107
+ /// ## Examples
108
+ /// ```
109
+ /// # #![feature(async_await)]
110
+ /// # use futures::prelude::*;
111
+ /// use runtime::time::interval;
112
+ /// use std::time::{Duration, Instant};
113
+ ///
114
+ /// # #[runtime::main]
115
+ /// # async fn main () -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
116
+ /// let start = Instant::now();
117
+ /// let mut interval = interval(Duration::from_millis(10)).take(3);
118
+ /// while let Some(now) = interval.next().await {
119
+ /// println!("{}ms have elapsed", (now - start).as_millis());
120
+ /// }
121
+ ///
122
+ /// assert!(Instant::now() - start >= Duration::from_millis(30));
123
+ /// # Ok(())}
124
+ /// ```
76
125
#[ inline]
77
126
pub fn interval ( dur : Duration ) -> Interval {
78
127
Interval :: new ( dur)
0 commit comments