async_std/task/
block_on.rs

1use std::future::Future;
2
3use 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/// fn main() {
23///     task::block_on(async {
24///         println!("Hello, world!");
25///     })
26/// }
27/// ```
28#[cfg(not(target_os = "unknown"))]
29pub fn block_on<F, T>(future: F) -> T
30where
31    F: Future<Output = T>,
32{
33    Builder::new().blocking(future)
34}
35
36/// Spawns a task and waits for it to finish.
37#[cfg(target_os = "unknown")]
38pub fn block_on<F, T>(future: F)
39where
40    F: Future<Output = T> + 'static,
41    T: 'static,
42{
43    Builder::new().local(future).unwrap();
44}