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/// task::block_on(async {
23/// println!("Hello, world!");
24/// })
25/// ```
26#[cfg(not(target_os = "unknown"))]
27pub fn block_on<F, T>(future: F) -> T
28where
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")]
36pub fn block_on<F, T>(future: F)
37where
38 F: Future<Output = T> + 'static,
39 T: 'static,
40{
41 Builder::new().local(future).unwrap();
42}