File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,20 @@ impl Builder {
6060 Ok ( JoinHandle :: new ( smol_task, task) )
6161 }
6262
63+ /// Spawns a task locally with the configured settings.
64+ pub fn local < F , T > ( self , future : F ) -> io:: Result < JoinHandle < T > >
65+ where
66+ F : Future < Output = T > + ' static ,
67+ T : ' static ,
68+ {
69+ let wrapped = self . build ( future) ;
70+
71+ let task = wrapped. tag . task ( ) . clone ( ) ;
72+ let smol_task = smol:: Task :: local ( wrapped) . into ( ) ;
73+
74+ Ok ( JoinHandle :: new ( smol_task, task) )
75+ }
76+
6377 /// Spawns a task with the configured settings, blocking on its execution.
6478 pub fn blocking < F , T > ( self , future : F ) -> T
6579 where
Original file line number Diff line number Diff line change @@ -139,6 +139,7 @@ cfg_default! {
139139 pub use join_handle:: JoinHandle ;
140140 pub use sleep:: sleep;
141141 pub use spawn:: spawn;
142+ pub use spawn_local:: spawn_local;
142143 pub use task_local:: { AccessError , LocalKey } ;
143144
144145 pub ( crate ) use task_local:: LocalsMap ;
@@ -151,6 +152,7 @@ cfg_default! {
151152 mod sleep;
152153 mod spawn;
153154 mod spawn_blocking;
155+ mod spawn_local;
154156 mod task;
155157 mod task_id;
156158 mod task_local;
Original file line number Diff line number Diff line change 1+ use std:: future:: Future ;
2+
3+ use crate :: task:: { Builder , JoinHandle } ;
4+
5+ /// Spawns a task onto the thread-local executor.
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_local(async {
19+ /// 1 + 2
20+ /// });
21+ ///
22+ /// assert_eq!(handle.await, 3);
23+ /// #
24+ /// # })
25+ /// ```
26+ pub fn spawn_local < F , T > ( future : F ) -> JoinHandle < T >
27+ where
28+ F : Future < Output = T > + ' static ,
29+ T : ' static ,
30+ {
31+ Builder :: new ( ) . local ( future) . expect ( "cannot spawn task" )
32+ }
You can’t perform that action at this time.
0 commit comments