@@ -34,7 +34,9 @@ const STATE_COMPLETE: usize = 4;
34
34
pub trait ScopeFutureExt < ' scope > {
35
35
fn spawn_future < F > ( & self , future : F ) -> RayonFuture < F :: Item , F :: Error >
36
36
where
37
- F : Future + Send + ' scope ;
37
+ F : Future + Send + ' scope ,
38
+ <F as Future >:: Item : Send ,
39
+ <F as Future >:: Error : Send ;
38
40
}
39
41
40
42
impl < ' scope , T > ScopeFutureExt < ' scope > for T
44
46
fn spawn_future < F > ( & self , future : F ) -> RayonFuture < F :: Item , F :: Error >
45
47
where
46
48
F : Future + Send + ' scope ,
49
+ <F as Future >:: Item : Send ,
50
+ <F as Future >:: Error : Send ,
47
51
{
48
52
let inner = ScopeFuture :: spawn ( future, self . to_scope_handle ( ) ) ;
49
53
@@ -136,6 +140,8 @@ impl<T, E> fmt::Debug for RayonFuture<T, E> {
136
140
struct ScopeFuture < ' scope , F , S >
137
141
where
138
142
F : Future + Send + ' scope ,
143
+ <F as Future >:: Item : Send ,
144
+ <F as Future >:: Error : Send ,
139
145
S : ScopeHandle < ' scope > ,
140
146
{
141
147
state : AtomicUsize ,
@@ -149,6 +155,8 @@ type CUError<F> = <CU<F> as Future>::Error;
149
155
struct ScopeFutureContents < ' scope , F , S >
150
156
where
151
157
F : Future + Send + ' scope ,
158
+ <F as Future >:: Item : Send ,
159
+ <F as Future >:: Error : Send ,
152
160
S : ScopeHandle < ' scope > ,
153
161
{
154
162
spawn : Option < Spawn < CU < F > > > ,
@@ -160,7 +168,7 @@ where
160
168
// the counter in the scope; since the scope doesn't terminate until
161
169
// counter reaches zero, and we hold a ref in this counter, we are
162
170
// assured that this pointer remains valid
163
- scope : Option < S > ,
171
+ scope : Option < ScopeHandleSend < ' scope , S > > ,
164
172
165
173
waiting_task : Option < Task > ,
166
174
result : Poll < CUItem < F > , CUError < F > > ,
@@ -171,6 +179,8 @@ where
171
179
impl < ' scope , F , S > fmt:: Debug for ScopeFutureContents < ' scope , F , S >
172
180
where
173
181
F : Future + Send + ' scope ,
182
+ <F as Future >:: Item : Send ,
183
+ <F as Future >:: Error : Send ,
174
184
S : ScopeHandle < ' scope > ,
175
185
{
176
186
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -183,11 +193,15 @@ where
183
193
struct ArcScopeFuture < ' scope , F , S > ( Arc < ScopeFuture < ' scope , F , S > > )
184
194
where
185
195
F : Future + Send + ' scope ,
196
+ <F as Future >:: Item : Send ,
197
+ <F as Future >:: Error : Send ,
186
198
S : ScopeHandle < ' scope > ;
187
199
188
200
impl < ' scope , F , S > Clone for ArcScopeFuture < ' scope , F , S >
189
201
where
190
202
F : Future + Send + ' scope ,
203
+ <F as Future >:: Item : Send ,
204
+ <F as Future >:: Error : Send ,
191
205
S : ScopeHandle < ' scope > ,
192
206
{
193
207
fn clone ( & self ) -> Self {
@@ -198,6 +212,8 @@ where
198
212
impl < ' scope , F , S > Notify for ArcScopeFuture < ' scope , F , S >
199
213
where
200
214
F : Future + Send + ' scope ,
215
+ <F as Future >:: Item : Send ,
216
+ <F as Future >:: Error : Send ,
201
217
S : ScopeHandle < ' scope > ,
202
218
{
203
219
fn notify ( & self , id : usize ) {
@@ -225,6 +241,8 @@ unsafe impl<'scope, F, S> Sync for ScopeFutureWrapped<'scope, F, S> {}
225
241
impl < ' scope , F , S > Notify for ScopeFutureWrapped < ' scope , F , S >
226
242
where
227
243
F : Future + Send + ' scope ,
244
+ <F as Future >:: Item : Send ,
245
+ <F as Future >:: Error : Send ,
228
246
S : ScopeHandle < ' scope > ,
229
247
{
230
248
fn notify ( & self , id : usize ) {
@@ -264,6 +282,8 @@ where
264
282
unsafe impl < ' scope , F , S > UnsafeNotify for ScopeFutureWrapped < ' scope , F , S >
265
283
where
266
284
F : Future + Send + ' scope ,
285
+ <F as Future >:: Item : Send ,
286
+ <F as Future >:: Error : Send ,
267
287
S : ScopeHandle < ' scope > ,
268
288
{
269
289
unsafe fn clone_raw ( & self ) -> NotifyHandle {
@@ -285,6 +305,8 @@ where
285
305
impl < ' scope , F , S > From < ArcScopeFuture < ' scope , F , S > > for NotifyHandle
286
306
where
287
307
F : Future + Send + ' scope ,
308
+ <F as Future >:: Item : Send ,
309
+ <F as Future >:: Error : Send ,
288
310
S : ScopeHandle < ' scope > ,
289
311
{
290
312
fn from ( rc : ArcScopeFuture < ' scope , F , S > ) -> NotifyHandle {
@@ -309,22 +331,31 @@ where
309
331
}
310
332
311
333
// Assert that the `*const` is safe to transmit between threads:
312
- unsafe impl < ' scope , F , S > Send for ScopeFuture < ' scope , F , S >
313
- where
314
- F : Future + Send + ' scope ,
315
- S : ScopeHandle < ' scope > ,
316
- {
317
- }
318
- unsafe impl < ' scope , F , S > Sync for ScopeFuture < ' scope , F , S >
334
+ struct ScopeHandleSend < ' s , S : ScopeHandle < ' s > > ( S , PhantomData < & ' s ( ) > ) ;
335
+ unsafe impl < ' scope , S > Send for ScopeHandleSend < ' scope , S > where S : ScopeHandle < ' scope > { }
336
+ impl < ' scope , S > ScopeHandleSend < ' scope , S >
319
337
where
320
- F : Future + Send + ' scope ,
321
338
S : ScopeHandle < ' scope > ,
322
339
{
340
+ unsafe fn assert_send ( s : S ) -> Self {
341
+ ScopeHandleSend ( s, PhantomData )
342
+ }
343
+ unsafe fn spawn_task < T : RayonTask + ' scope > ( & self , task : Arc < T > ) {
344
+ self . 0 . spawn_task ( task) ;
345
+ }
346
+ fn panicked ( self , err : Box < Any + Send > ) {
347
+ self . 0 . panicked ( err) ;
348
+ }
349
+ fn ok ( self ) {
350
+ self . 0 . ok ( ) ;
351
+ }
323
352
}
324
353
325
354
impl < ' scope , F , S > ScopeFuture < ' scope , F , S >
326
355
where
327
356
F : Future + Send + ' scope ,
357
+ <F as Future >:: Item : Send ,
358
+ <F as Future >:: Error : Send ,
328
359
S : ScopeHandle < ' scope > ,
329
360
{
330
361
fn spawn ( future : F , scope : S ) -> Arc < Self > {
@@ -338,7 +369,7 @@ where
338
369
contents : Mutex :: new ( ScopeFutureContents {
339
370
spawn : None ,
340
371
this : None ,
341
- scope : Some ( scope) ,
372
+ scope : unsafe { Some ( ScopeHandleSend :: assert_send ( scope) ) } ,
342
373
waiting_task : None ,
343
374
result : Ok ( Async :: NotReady ) ,
344
375
canceled : false ,
@@ -466,6 +497,8 @@ where
466
497
impl < ' scope , F , S > Notify for ScopeFuture < ' scope , F , S >
467
498
where
468
499
F : Future + Send + ' scope ,
500
+ <F as Future >:: Item : Send ,
501
+ <F as Future >:: Error : Send ,
469
502
S : ScopeHandle < ' scope > ,
470
503
{
471
504
fn notify ( & self , _: usize ) {
@@ -476,6 +509,8 @@ where
476
509
impl < ' scope , F , S > RayonTask for ScopeFuture < ' scope , F , S >
477
510
where
478
511
F : Future + Send + ' scope ,
512
+ <F as Future >:: Item : Send ,
513
+ <F as Future >:: Error : Send ,
479
514
S : ScopeHandle < ' scope > ,
480
515
{
481
516
fn execute ( this : Arc < Self > ) {
@@ -510,6 +545,8 @@ where
510
545
impl < ' scope , F , S > ScopeFutureContents < ' scope , F , S >
511
546
where
512
547
F : Future + Send + ' scope ,
548
+ <F as Future >:: Item : Send ,
549
+ <F as Future >:: Error : Send ,
513
550
S : ScopeHandle < ' scope > ,
514
551
{
515
552
fn poll ( & mut self ) -> Poll < CUItem < F > , CUError < F > > {
@@ -578,7 +615,9 @@ trait ScopeFutureTrait<T, E>: Send + Sync {
578
615
579
616
impl < ' scope , F , S > ScopeFutureTrait < CUItem < F > , CUError < F > > for ScopeFuture < ' scope , F , S >
580
617
where
581
- F : Future + Send ,
618
+ F : Future + Send + ' scope ,
619
+ <F as Future >:: Item : Send ,
620
+ <F as Future >:: Error : Send ,
582
621
S : ScopeHandle < ' scope > ,
583
622
{
584
623
fn probe ( & self ) -> bool {
0 commit comments