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