1 | use std::future::Future; |
2 | |
3 | use crate::task::Builder; |
4 | |
5 | /// Spawns a task and blocks the current thread on its result. |
6 | /// |
7 | /// Calling this function is similar to [spawning] a thread and immediately [joining] it, except an |
8 | /// asynchronous task will be spawned. |
9 | /// |
10 | /// See also: [`task::spawn_blocking`]. |
11 | /// |
12 | /// [`task::spawn_blocking`]: fn.spawn_blocking.html |
13 | /// |
14 | /// [spawning]: https://doc.rust-lang.org/std/thread/fn.spawn.html |
15 | /// [joining]: https://doc.rust-lang.org/std/thread/struct.JoinHandle.html#method.join |
16 | /// |
17 | /// # Examples |
18 | /// |
19 | /// ```no_run |
20 | /// use async_std::task; |
21 | /// |
22 | /// task::block_on(async { |
23 | /// println!("Hello, world!" ); |
24 | /// }) |
25 | /// ``` |
26 | #[cfg (not(target_os = "unknown" ))] |
27 | pub fn block_on<F, T>(future: F) -> T |
28 | where |
29 | F: Future<Output = T>, |
30 | { |
31 | Builder::new().blocking(future) |
32 | } |
33 | |
34 | /// Spawns a task and waits for it to finish. |
35 | #[cfg (target_os = "unknown" )] |
36 | pub fn block_on<F, T>(future: F) |
37 | where |
38 | F: Future<Output = T> + 'static, |
39 | T: 'static, |
40 | { |
41 | Builder::new().local(future).unwrap(); |
42 | } |
43 | |