@@ -10,6 +10,9 @@ pub mod rt_async_global_executor;
10
10
#[ cfg( feature = "_rt-async-std" ) ]
11
11
pub mod rt_async_std;
12
12
13
+ #[ cfg( feature = "_rt-smol" ) ]
14
+ pub mod rt_smol;
15
+
13
16
#[ cfg( feature = "_rt-tokio" ) ]
14
17
pub mod rt_tokio;
15
18
@@ -22,6 +25,8 @@ pub enum JoinHandle<T> {
22
25
AsyncGlobalExecutor ( rt_async_global_executor:: JoinHandle < T > ) ,
23
26
#[ cfg( feature = "_rt-async-std" ) ]
24
27
AsyncStd ( async_std:: task:: JoinHandle < T > ) ,
28
+ #[ cfg( feature = "_rt-smol" ) ]
29
+ Smol ( rt_smol:: JoinHandle < T > ) ,
25
30
#[ cfg( feature = "_rt-tokio" ) ]
26
31
Tokio ( tokio:: task:: JoinHandle < T > ) ,
27
32
// `PhantomData<T>` requires `T: Unpin`
@@ -41,14 +46,23 @@ pub async fn timeout<F: Future>(duration: Duration, f: F) -> Result<F::Output, T
41
46
return rt_async_global_executor:: timeout ( duration, f) . await ;
42
47
}
43
48
49
+ #[ cfg( feature = "_rt-smol" ) ]
50
+ {
51
+ return rt_smol:: timeout ( duration, f) . await ;
52
+ }
53
+
44
54
#[ cfg( feature = "_rt-async-std" ) ]
45
55
{
46
56
return async_std:: future:: timeout ( duration, f)
47
57
. await
48
58
. map_err ( |_| TimeoutError ) ;
49
59
}
50
60
51
- #[ cfg( not( all( feature = "_rt-async-global-executor" , feature = "_rt-async-std" , ) ) ) ]
61
+ #[ cfg( not( all(
62
+ feature = "_rt-async-global-executor" ,
63
+ feature = "_rt-async-std" ,
64
+ feature = "_rt-smol"
65
+ ) ) ) ]
52
66
#[ allow( unreachable_code) ]
53
67
missing_rt ( ( duration, f) )
54
68
}
@@ -64,12 +78,21 @@ pub async fn sleep(duration: Duration) {
64
78
return rt_async_global_executor:: sleep ( duration) . await ;
65
79
}
66
80
81
+ #[ cfg( feature = "_rt-smol" ) ]
82
+ {
83
+ return rt_smol:: sleep ( duration) . await ;
84
+ }
85
+
67
86
#[ cfg( feature = "_rt-async-std" ) ]
68
87
{
69
88
return async_std:: task:: sleep ( duration) . await ;
70
89
}
71
90
72
- #[ cfg( not( all( feature = "_rt-async-global-executor" , feature = "_rt-async-std" , ) ) ) ]
91
+ #[ cfg( not( all(
92
+ feature = "_rt-async-global-executor" ,
93
+ feature = "_rt-async-std" ,
94
+ feature = "_rt-smol"
95
+ ) ) ) ]
73
96
#[ allow( unreachable_code) ]
74
97
missing_rt ( duration)
75
98
}
@@ -97,7 +120,11 @@ where
97
120
return JoinHandle :: AsyncStd ( async_std:: task:: spawn ( fut) ) ;
98
121
}
99
122
100
- #[ cfg( not( all( feature = "_rt-async-global-executor" , feature = "_rt-async-std" , ) ) ) ]
123
+ #[ cfg( not( all(
124
+ feature = "_rt-async-global-executor" ,
125
+ feature = "_rt-async-std" ,
126
+ feature = "_rt-smol"
127
+ ) ) ) ]
101
128
#[ allow( unreachable_code) ]
102
129
missing_rt ( fut)
103
130
}
@@ -125,7 +152,18 @@ where
125
152
return JoinHandle :: AsyncStd ( async_std:: task:: spawn_blocking ( f) ) ;
126
153
}
127
154
128
- #[ cfg( not( all( feature = "_rt-async-global-executor" , feature = "_rt-async-std" , ) ) ) ]
155
+ #[ cfg( feature = "_rt-smol" ) ]
156
+ {
157
+ return JoinHandle :: Smol ( rt_smol:: JoinHandle {
158
+ task : Some ( smol:: unblock ( f) ) ,
159
+ } ) ;
160
+ }
161
+
162
+ #[ cfg( not( all(
163
+ feature = "_rt-async-global-executor" ,
164
+ feature = "_rt-async-std" ,
165
+ feature = "_rt-smol"
166
+ ) ) ) ]
129
167
#[ allow( unreachable_code) ]
130
168
missing_rt ( f)
131
169
}
@@ -146,7 +184,16 @@ pub async fn yield_now() {
146
184
return async_std:: task:: yield_now ( ) . await ;
147
185
}
148
186
149
- #[ cfg( not( all( feature = "_rt-async-global-executor" , feature = "_rt-async-std" , ) ) ) ]
187
+ #[ cfg( feature = "_rt-smol" ) ]
188
+ {
189
+ return smol:: future:: yield_now ( ) . await ;
190
+ }
191
+
192
+ #[ cfg( not( all(
193
+ feature = "_rt-async-global-executor" ,
194
+ feature = "_rt-async-std" ,
195
+ feature = "_rt-smol"
196
+ ) ) ) ]
150
197
#[ allow( unreachable_code) ]
151
198
missing_rt ( ( ) )
152
199
}
@@ -155,11 +202,13 @@ pub async fn yield_now() {
155
202
pub fn test_block_on < F : Future > ( f : F ) -> F :: Output {
156
203
#[ cfg( feature = "_rt-tokio" ) ]
157
204
{
158
- return tokio:: runtime:: Builder :: new_current_thread ( )
159
- . enable_all ( )
160
- . build ( )
161
- . expect ( "failed to start Tokio runtime" )
162
- . block_on ( f) ;
205
+ if rt_tokio:: available ( ) {
206
+ return tokio:: runtime:: Builder :: new_current_thread ( )
207
+ . enable_all ( )
208
+ . build ( )
209
+ . expect ( "failed to start Tokio runtime" )
210
+ . block_on ( f) ;
211
+ }
163
212
}
164
213
165
214
#[ cfg( feature = "_rt-async-global-executor" ) ]
@@ -172,7 +221,16 @@ pub fn test_block_on<F: Future>(f: F) -> F::Output {
172
221
return async_std:: task:: block_on ( f) ;
173
222
}
174
223
175
- #[ cfg( not( all( feature = "_rt-async-global-executor" , feature = "_rt-async-std" , ) ) ) ]
224
+ #[ cfg( feature = "_rt-smol" ) ]
225
+ {
226
+ return smol:: block_on ( f) ;
227
+ }
228
+
229
+ #[ cfg( not( all(
230
+ feature = "_rt-async-global-executor" ,
231
+ feature = "_rt-async-std" ,
232
+ feature = "_rt-smol"
233
+ ) ) ) ]
176
234
#[ allow( unreachable_code) ]
177
235
missing_rt ( f)
178
236
}
@@ -183,7 +241,7 @@ pub fn missing_rt<T>(_unused: T) -> ! {
183
241
panic ! ( "this functionality requires a Tokio context" )
184
242
}
185
243
186
- panic ! ( "one of the `runtime-async-global-executor`, `runtime-async-std`, or `runtime-tokio` feature must be enabled" )
244
+ panic ! ( "one of the `runtime-async-global-executor`, `runtime-async-std`, `runtime-smol`, or `runtime-tokio` feature must be enabled" )
187
245
}
188
246
189
247
impl < T : Send + ' static > Future for JoinHandle < T > {
@@ -196,6 +254,8 @@ impl<T: Send + 'static> Future for JoinHandle<T> {
196
254
Self :: AsyncGlobalExecutor ( handle) => Pin :: new ( handle) . poll ( cx) ,
197
255
#[ cfg( feature = "_rt-async-std" ) ]
198
256
Self :: AsyncStd ( handle) => Pin :: new ( handle) . poll ( cx) ,
257
+ #[ cfg( feature = "_rt-smol" ) ]
258
+ Self :: Smol ( handle) => Pin :: new ( handle) . poll ( cx) ,
199
259
#[ cfg( feature = "_rt-tokio" ) ]
200
260
Self :: Tokio ( handle) => Pin :: new ( handle)
201
261
. poll ( cx)
0 commit comments