@@ -61,14 +61,21 @@ pub use async_task::Task;
61
61
/// }));
62
62
/// ```
63
63
#[ derive( Debug ) ]
64
- pub struct Executor {
64
+ pub struct Executor < ' a > {
65
+ /// The executor state.
65
66
state : once_cell:: sync:: OnceCell < Arc < State > > ,
67
+
68
+ /// Makes the `'a` lifetime invariant.
69
+ _marker : PhantomData < std:: cell:: UnsafeCell < & ' a ( ) > > ,
66
70
}
67
71
68
- impl UnwindSafe for Executor { }
69
- impl RefUnwindSafe for Executor { }
72
+ unsafe impl Send for Executor < ' _ > { }
73
+ unsafe impl Sync for Executor < ' _ > { }
70
74
71
- impl Executor {
75
+ impl UnwindSafe for Executor < ' _ > { }
76
+ impl RefUnwindSafe for Executor < ' _ > { }
77
+
78
+ impl < ' a > Executor < ' a > {
72
79
/// Creates a new executor.
73
80
///
74
81
/// # Examples
@@ -78,9 +85,10 @@ impl Executor {
78
85
///
79
86
/// let ex = Executor::new();
80
87
/// ```
81
- pub const fn new ( ) -> Executor {
88
+ pub const fn new ( ) -> Executor < ' a > {
82
89
Executor {
83
90
state : once_cell:: sync:: OnceCell :: new ( ) ,
91
+ _marker : PhantomData ,
84
92
}
85
93
}
86
94
@@ -97,10 +105,7 @@ impl Executor {
97
105
/// println!("Hello world");
98
106
/// });
99
107
/// ```
100
- pub fn spawn < T : Send + ' static > (
101
- & self ,
102
- future : impl Future < Output = T > + Send + ' static ,
103
- ) -> Task < T > {
108
+ pub fn spawn < T : Send + ' a > ( & self , future : impl Future < Output = T > + Send + ' a ) -> Task < T > {
104
109
let mut active = self . state ( ) . active . lock ( ) . unwrap ( ) ;
105
110
106
111
// Remove the task from the set of active tasks when the future finishes.
@@ -112,7 +117,7 @@ impl Executor {
112
117
} ;
113
118
114
119
// Create the task and register it in the set of active tasks.
115
- let ( runnable, task) = async_task:: spawn ( future, self . schedule ( ) ) ;
120
+ let ( runnable, task) = unsafe { async_task:: spawn_unchecked ( future, self . schedule ( ) ) } ;
116
121
active. insert ( runnable. waker ( ) ) ;
117
122
118
123
runnable. schedule ( ) ;
@@ -226,7 +231,7 @@ impl Executor {
226
231
}
227
232
}
228
233
229
- impl Drop for Executor {
234
+ impl Drop for Executor < ' _ > {
230
235
fn drop ( & mut self ) {
231
236
if let Some ( state) = self . state . get ( ) {
232
237
let mut active = state. active . lock ( ) . unwrap ( ) ;
@@ -242,8 +247,8 @@ impl Drop for Executor {
242
247
}
243
248
}
244
249
245
- impl Default for Executor {
246
- fn default ( ) -> Executor {
250
+ impl < ' a > Default for Executor < ' a > {
251
+ fn default ( ) -> Executor < ' a > {
247
252
Executor :: new ( )
248
253
}
249
254
}
@@ -265,18 +270,18 @@ impl Default for Executor {
265
270
/// }));
266
271
/// ```
267
272
#[ derive( Debug ) ]
268
- pub struct LocalExecutor {
273
+ pub struct LocalExecutor < ' a > {
269
274
/// The inner executor.
270
- inner : once_cell:: unsync:: OnceCell < Executor > ,
275
+ inner : once_cell:: unsync:: OnceCell < Executor < ' a > > ,
271
276
272
- /// Make sure the type is `!Send` and `!Sync`.
277
+ /// Makes the type `!Send` and `!Sync`.
273
278
_marker : PhantomData < Rc < ( ) > > ,
274
279
}
275
280
276
- impl UnwindSafe for LocalExecutor { }
277
- impl RefUnwindSafe for LocalExecutor { }
281
+ impl UnwindSafe for LocalExecutor < ' _ > { }
282
+ impl RefUnwindSafe for LocalExecutor < ' _ > { }
278
283
279
- impl LocalExecutor {
284
+ impl < ' a > LocalExecutor < ' a > {
280
285
/// Creates a single-threaded executor.
281
286
///
282
287
/// # Examples
@@ -286,7 +291,7 @@ impl LocalExecutor {
286
291
///
287
292
/// let local_ex = LocalExecutor::new();
288
293
/// ```
289
- pub const fn new ( ) -> LocalExecutor {
294
+ pub const fn new ( ) -> LocalExecutor < ' a > {
290
295
LocalExecutor {
291
296
inner : once_cell:: unsync:: OnceCell :: new ( ) ,
292
297
_marker : PhantomData ,
@@ -306,7 +311,7 @@ impl LocalExecutor {
306
311
/// println!("Hello world");
307
312
/// });
308
313
/// ```
309
- pub fn spawn < T : ' static > ( & self , future : impl Future < Output = T > + ' static ) -> Task < T > {
314
+ pub fn spawn < T : ' a > ( & self , future : impl Future < Output = T > + ' a ) -> Task < T > {
310
315
let mut active = self . inner ( ) . state ( ) . active . lock ( ) . unwrap ( ) ;
311
316
312
317
// Remove the task from the set of active tasks when the future finishes.
@@ -318,7 +323,7 @@ impl LocalExecutor {
318
323
} ;
319
324
320
325
// Create the task and register it in the set of active tasks.
321
- let ( runnable, task) = async_task:: spawn_local ( future, self . schedule ( ) ) ;
326
+ let ( runnable, task) = unsafe { async_task:: spawn_unchecked ( future, self . schedule ( ) ) } ;
322
327
active. insert ( runnable. waker ( ) ) ;
323
328
324
329
runnable. schedule ( ) ;
@@ -399,13 +404,13 @@ impl LocalExecutor {
399
404
}
400
405
401
406
/// Returns a reference to the inner executor.
402
- fn inner ( & self ) -> & Executor {
407
+ fn inner ( & self ) -> & Executor < ' a > {
403
408
self . inner . get_or_init ( || Executor :: new ( ) )
404
409
}
405
410
}
406
411
407
- impl Default for LocalExecutor {
408
- fn default ( ) -> LocalExecutor {
412
+ impl < ' a > Default for LocalExecutor < ' a > {
413
+ fn default ( ) -> LocalExecutor < ' a > {
409
414
LocalExecutor :: new ( )
410
415
}
411
416
}
0 commit comments