1 | use std::time::Duration; |
2 | |
3 | use crate::future; |
4 | use crate::io; |
5 | |
6 | /// Sleeps for the specified amount of time. |
7 | /// |
8 | /// This function might sleep for slightly longer than the specified duration but never less. |
9 | /// |
10 | /// This function is an async version of [`std::thread::sleep`]. |
11 | /// |
12 | /// [`std::thread::sleep`]: https://doc.rust-lang.org/std/thread/fn.sleep.html |
13 | /// |
14 | /// See also: [`stream::interval`]. |
15 | /// |
16 | /// [`stream::interval`]: ../stream/fn.interval.html |
17 | /// |
18 | /// # Examples |
19 | /// |
20 | /// ``` |
21 | /// # async_std::task::block_on(async { |
22 | /// # |
23 | /// use std::time::Duration; |
24 | /// |
25 | /// use async_std::task; |
26 | /// |
27 | /// task::sleep(Duration::from_secs(1)).await; |
28 | /// # |
29 | /// # }) |
30 | /// ``` |
31 | pub async fn sleep(dur: Duration) { |
32 | let _: io::Result<()> = io::timeout(dur, f:future::pending()).await; |
33 | } |
34 | |