File tree 3 files changed +48
-0
lines changed
3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,20 @@ impl Builder {
60
60
Ok ( JoinHandle :: new ( smol_task, task) )
61
61
}
62
62
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
+
63
77
/// Spawns a task with the configured settings, blocking on its execution.
64
78
pub fn blocking < F , T > ( self , future : F ) -> T
65
79
where
Original file line number Diff line number Diff line change @@ -139,6 +139,7 @@ cfg_default! {
139
139
pub use join_handle:: JoinHandle ;
140
140
pub use sleep:: sleep;
141
141
pub use spawn:: spawn;
142
+ pub use spawn_local:: spawn_local;
142
143
pub use task_local:: { AccessError , LocalKey } ;
143
144
144
145
pub ( crate ) use task_local:: LocalsMap ;
@@ -151,6 +152,7 @@ cfg_default! {
151
152
mod sleep;
152
153
mod spawn;
153
154
mod spawn_blocking;
155
+ mod spawn_local;
154
156
mod task;
155
157
mod task_id;
156
158
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