1 | use std::future::Future; |
2 | |
3 | use crate::task::{Builder, JoinHandle}; |
4 | |
5 | /// Spawns a task. |
6 | /// |
7 | /// This function is similar to [`std::thread::spawn`], except it spawns an asynchronous task. |
8 | /// |
9 | /// [`std::thread`]: https://doc.rust-lang.org/std/thread/fn.spawn.html |
10 | /// |
11 | /// # Examples |
12 | /// |
13 | /// ``` |
14 | /// # async_std::task::block_on(async { |
15 | /// # |
16 | /// use async_std::task; |
17 | /// |
18 | /// let handle = task::spawn(async { |
19 | /// 1 + 2 |
20 | /// }); |
21 | /// |
22 | /// assert_eq!(handle.await, 3); |
23 | /// # |
24 | /// # }) |
25 | /// ``` |
26 | pub fn spawn<F, T>(future: F) -> JoinHandle<T> |
27 | where |
28 | F: Future<Output = T> + Send + 'static, |
29 | T: Send + 'static, |
30 | { |
31 | Builder::new().spawn(future).expect(msg:"cannot spawn task" ) |
32 | } |
33 | |