async_std/task/block_on.rs
1use std::future::Future;
2
3// use kv_log_macro::trace;
4// use log::log_enabled;
5
6// use crate::task::Task;
7
8/// Spawns a task and blocks the current thread on its result.
9///
10/// Calling this function is similar to [spawning] a thread and immediately [joining] it, except an
11/// asynchronous task will be spawned.
12///
13/// See also: [`task::spawn_blocking`].
14///
15/// [`task::spawn_blocking`]: fn.spawn_blocking.html
16///
17/// [spawning]: https://doc.rust-lang.org/std/thread/fn.spawn.html
18/// [joining]: https://doc.rust-lang.org/std/thread/struct.JoinHandle.html#method.join
19///
20/// # Examples
21///
22/// ```no_run
23/// use async_std::task;
24///
25/// fn main() {
26/// task::block_on(async {
27/// println!("Hello, world!");
28/// })
29/// }
30/// ```
31pub fn block_on<F, T>(future: F) -> T
32where
33 F: Future<Output = T>,
34{
35 tokio::runtime::Builder::new().basic_scheduler().build().unwrap().block_on(future)
36 // // Create a new task handle.
37 // let task = Task::new(None);
38
39 // // Log this `block_on` operation.
40 // if log_enabled!(log::Level::Trace) {
41 // trace!("block_on", {
42 // task_id: task.id().0,
43 // parent_task_id: Task::get_current(|t| t.id().0).unwrap_or(0),
44 // });
45 // }
46
47 // let future = async move {
48 // // Drop task-locals on exit.
49 // defer! {
50 // Task::get_current(|t| unsafe { t.drop_locals() });
51 // }
52
53 // // Log completion on exit.
54 // defer! {
55 // if log_enabled!(log::Level::Trace) {
56 // Task::get_current(|t| {
57 // trace!("completed", {
58 // task_id: t.id().0,
59 // });
60 // });
61 // }
62 // }
63
64 // future.await
65 // };
66
67 // // Run the future as a task.
68 // unsafe { Task::set_current(&task, || run(future)) }
69}
70
71// /// Blocks the current thread on a future's result.
72// fn run<F, T>(future: F) -> T
73// where
74// F: Future<Output = T>,
75// {
76
77// }