async-rt 0.2.0

A small library designed to utilize async executors through an common API while extending features.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
//! Structured concurrency scopes for async tasks.
//!
//! A [`Scope`] lets you spawn futures that borrow from the enclosing stack
//! frame, analogous to [`std::thread::scope`]. Unlike thread scope, the
//! tasks are driven cooperatively by the scope's own future, they make
//! progress whenever the scope is polled so no runtime-level `spawn` is
//! involved and no `unsafe` is needed to extend lifetimes.
//!
//! The [`scope`] function is the entry point. Inside the async closure,
//! call [`Scope::spawn`] to enqueue a task; the returned [`ScopedJoinHandle`]
//! resolves with the task's output. Any tasks still running when the user
//! closure's future completes are drained before `scope` returns, so every
//! borrow is released before the stack frame goes away.

use crate::{
    AbortableJoinHandle, CommunicationTask, CompletionGuard, Executor, ExecutorBlocking,
    ExecutorTimeout, InnerJoinHandle, JoinHandle, TimeoutError, UnboundedCommunicationTask,
    abortable_result, error::JoinError,
};
use core::future::{Future, poll_fn};
use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll};
use futures::channel::mpsc::{Receiver, UnboundedReceiver};
use futures::channel::oneshot;
use futures::future::{AbortHandle, BoxFuture};
use futures::stream::FuturesUnordered;
use futures::task::AtomicWaker;
use futures::{FutureExt, StreamExt, TryFutureExt};
use futures_timeout::Timeout;
use parking_lot::Mutex;
use pollable_map::optional::Optional;
use std::panic::AssertUnwindSafe;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Weak};

struct ScopeState<'scope> {
    inbox: Mutex<Vec<BoxFuture<'scope, ()>>>,
    waker: AtomicWaker,
}

/// A scope within which tasks can be spawned that borrow from the enclosing
/// stack frame.
pub struct Scope<'scope, 'env: 'scope> {
    state: Weak<ScopeState<'scope>>,
    _scope: PhantomData<&'scope mut &'scope ()>,
    _env: PhantomData<&'env mut &'env ()>,
}

impl<'scope, 'env> Scope<'scope, 'env> {
    fn new() -> Self {
        Self {
            state: Weak::new(),
            _scope: PhantomData,
            _env: PhantomData,
        }
    }

    fn push(&self, task: BoxFuture<'scope, ()>) {
        if let Some(state) = self.state.upgrade() {
            state.inbox.lock().push(task);
            state.waker.wake();
        }
    }

    /// Spawn a task into this scope.
    ///
    /// The future may borrow any data that outlives its lifetime `'env`. The task will
    /// be polled cooperatively alongside the scope's user closure and any
    /// other spawned tasks.
    pub fn spawn<Fut>(&'scope self, fut: Fut) -> ScopedJoinHandle<Fut::Output>
    where
        Fut: Future + Send + 'scope,
        Fut::Output: Send + 'scope,
    {
        let (tx, rx) = oneshot::channel();
        let wrapped: BoxFuture<'scope, ()> = async move {
            let output = AssertUnwindSafe(fut)
                .catch_unwind()
                .await
                .map_err(|_| JoinError::Panicked);
            // If the receiver was dropped, the caller doesn't care about
            // the output so we will discard it.
            let _ = tx.send(output);
        }
        .boxed();

        self.push(wrapped);

        ScopedJoinHandle { rx }
    }

    /// Spawn a task into this scope and return an [`AbortableJoinHandle`].
    ///
    /// This mirrors [`Executor::spawn_abortable`] for cooperatively
    /// scheduled tasks. The returned handle aborts the task when all
    /// references to it have been dropped.
    pub fn spawn_abortable<Fut>(&'scope self, fut: Fut) -> AbortableJoinHandle<Fut::Output>
    where
        Fut: Future + Send + 'scope,
        Fut::Output: Send + 'scope,
    {
        let (abort_handle, abort_reg) = AbortHandle::new_pair();
        let abortable = abortable_result(fut, abort_reg);
        let (tx, rx) = oneshot::channel();
        let finished = Arc::new(AtomicBool::new(false));
        let completion = CompletionGuard::new(finished.clone());

        let wrapped: BoxFuture<'scope, ()> = async move {
            let _completion = completion;
            let val = abortable.await;
            let _ = tx.send(val);
        }
        .boxed();
        self.push(wrapped);

        let join = JoinHandle {
            inner: InnerJoinHandle::CustomHandle {
                inner: Optional::new(rx),
                handle: abort_handle,
                finished,
            },
        };
        AbortableJoinHandle::from(join)
    }

    /// Spawn a task into this scope without keeping a handle to it.
    ///
    /// Equivalent to [`Executor::dispatch`] for scoped tasks.
    pub fn dispatch<Fut>(&'scope self, fut: Fut)
    where
        Fut: Future + Send + 'scope,
        Fut::Output: Send + 'scope,
    {
        drop(self.spawn(fut));
    }

    /// Spawn a message-driven coroutine into this scope.
    ///
    /// Equivalent to [`Executor::spawn_coroutine`] for scoped tasks.
    pub fn spawn_coroutine<T, F, Fut>(&'scope self, f: F) -> CommunicationTask<T>
    where
        F: FnMut(T) -> Fut + Send + 'scope,
        Fut: Future<Output = ()> + Send + 'scope,
        T: Send + 'scope,
    {
        self.spawn_coroutine_with_buffer(1, f)
    }

    /// Like [`Scope::spawn_coroutine`] but with a configurable channel
    /// buffer.
    pub fn spawn_coroutine_with_buffer<T, F, Fut>(
        &'scope self,
        buffer: usize,
        mut f: F,
    ) -> CommunicationTask<T>
    where
        F: FnMut(T) -> Fut + Send + 'scope,
        Fut: Future<Output = ()> + Send + 'scope,
        T: Send + 'scope,
    {
        let (tx, mut rx) = futures::channel::mpsc::channel(buffer);
        let task_handle = self.spawn_abortable(async move {
            while let Some(message) = rx.next().await {
                f(message).await;
            }
        });
        CommunicationTask::new(task_handle, tx)
    }

    /// Spawn an unbounded message-driven coroutine into this scope.
    ///
    /// Equivalent to [`Executor::spawn_unbounded_coroutine`] for scoped
    /// tasks.
    pub fn spawn_unbounded_coroutine<T, F, Fut>(
        &'scope self,
        mut f: F,
    ) -> UnboundedCommunicationTask<T>
    where
        F: FnMut(T) -> Fut + Send + 'scope,
        Fut: Future<Output = ()> + Send + 'scope,
        T: Send + 'scope,
    {
        let (tx, mut rx) = futures::channel::mpsc::unbounded();
        let task_handle = self.spawn_abortable(async move {
            while let Some(message) = rx.next().await {
                f(message).await;
            }
        });
        UnboundedCommunicationTask::new(task_handle, tx)
    }

    /// Spawn a message-driven coroutine with caller-provided context.
    ///
    /// If the context must be borrowed across awaits, use
    /// [`Scope::spawn_coroutine_with_receiver_and_context`].
    pub fn spawn_coroutine_with_context<T, C, F, Fut>(
        &'scope self,
        context: C,
        f: F,
    ) -> CommunicationTask<T>
    where
        F: FnMut(&mut C, T) -> Fut + Send + 'scope,
        Fut: Future<Output = ()> + Send + 'scope,
        C: Send + 'scope,
        T: Send + 'scope,
    {
        self.spawn_coroutine_with_buffer_and_context(context, 1, f)
    }

    /// Like [`Scope::spawn_coroutine_with_context`] but with a configurable
    /// channel buffer.
    pub fn spawn_coroutine_with_buffer_and_context<T, C, F, Fut>(
        &'scope self,
        context: C,
        buffer: usize,
        mut f: F,
    ) -> CommunicationTask<T>
    where
        F: FnMut(&mut C, T) -> Fut + Send + 'scope,
        Fut: Future<Output = ()> + Send + 'scope,
        C: Send + 'scope,
        T: Send + 'scope,
    {
        let (tx, mut rx) = futures::channel::mpsc::channel(buffer);
        let task_handle = self.spawn_abortable(async move {
            let mut context = context;
            while let Some(message) = rx.next().await {
                f(&mut context, message).await;
            }
        });
        CommunicationTask::new(task_handle, tx)
    }

    /// Spawn an unbounded message-driven coroutine with caller-provided
    /// context.
    pub fn spawn_unbounded_coroutine_with_context<T, C, F, Fut>(
        &'scope self,
        context: C,
        mut f: F,
    ) -> UnboundedCommunicationTask<T>
    where
        F: FnMut(&mut C, T) -> Fut + Send + 'scope,
        Fut: Future<Output = ()> + Send + 'scope,
        C: Send + 'scope,
        T: Send + 'scope,
    {
        let (tx, mut rx) = futures::channel::mpsc::unbounded();
        let task_handle = self.spawn_abortable(async move {
            let mut context = context;
            while let Some(message) = rx.next().await {
                f(&mut context, message).await;
            }
        });
        UnboundedCommunicationTask::new(task_handle, tx)
    }

    /// Spawn a coroutine that receives the bounded channel directly.
    pub fn spawn_coroutine_with_receiver<T, F, Fut>(&'scope self, f: F) -> CommunicationTask<T>
    where
        F: FnMut(Receiver<T>) -> Fut,
        Fut: Future<Output = ()> + Send + 'scope,
    {
        self.spawn_coroutine_with_receiver_and_buffer(1, f)
    }

    /// Like [`Scope::spawn_coroutine_with_receiver`] but with a configurable
    /// channel buffer.
    pub fn spawn_coroutine_with_receiver_and_buffer<T, F, Fut>(
        &'scope self,
        buffer: usize,
        mut f: F,
    ) -> CommunicationTask<T>
    where
        F: FnMut(Receiver<T>) -> Fut,
        Fut: Future<Output = ()> + Send + 'scope,
    {
        let (tx, rx) = futures::channel::mpsc::channel(buffer);
        let task_handle = self.spawn_abortable(f(rx));
        CommunicationTask::new(task_handle, tx)
    }

    /// Spawn a coroutine that receives caller-provided context and the
    /// bounded channel directly.
    pub fn spawn_coroutine_with_receiver_and_context<T, F, C, Fut>(
        &'scope self,
        context: C,
        f: F,
    ) -> CommunicationTask<T>
    where
        F: FnMut(C, Receiver<T>) -> Fut,
        Fut: Future<Output = ()> + Send + 'scope,
    {
        self.spawn_coroutine_with_receiver_buffer_and_context(context, 1, f)
    }

    /// Like [`Scope::spawn_coroutine_with_receiver_and_context`] but with a
    /// configurable channel buffer.
    pub fn spawn_coroutine_with_receiver_buffer_and_context<T, F, C, Fut>(
        &'scope self,
        context: C,
        buffer: usize,
        mut f: F,
    ) -> CommunicationTask<T>
    where
        F: FnMut(C, Receiver<T>) -> Fut,
        Fut: Future<Output = ()> + Send + 'scope,
    {
        let (tx, rx) = futures::channel::mpsc::channel(buffer);
        let task_handle = self.spawn_abortable(f(context, rx));
        CommunicationTask::new(task_handle, tx)
    }

    /// Spawn a coroutine that receives the unbounded channel directly.
    pub fn spawn_unbounded_coroutine_with_receiver<T, F, Fut>(
        &'scope self,
        mut f: F,
    ) -> UnboundedCommunicationTask<T>
    where
        F: FnMut(UnboundedReceiver<T>) -> Fut,
        Fut: Future<Output = ()> + Send + 'scope,
    {
        let (tx, rx) = futures::channel::mpsc::unbounded();
        let task_handle = self.spawn_abortable(f(rx));
        UnboundedCommunicationTask::new(task_handle, tx)
    }

    /// Spawn a coroutine that receives caller-provided context and the
    /// unbounded channel directly.
    pub fn spawn_unbounded_coroutine_with_receiver_and_context<T, F, C, Fut>(
        &'scope self,
        context: C,
        mut f: F,
    ) -> UnboundedCommunicationTask<T>
    where
        F: FnMut(C, UnboundedReceiver<T>) -> Fut,
        Fut: Future<Output = ()> + Send + 'scope,
    {
        let (tx, rx) = futures::channel::mpsc::unbounded();
        let task_handle = self.spawn_abortable(f(context, rx));
        UnboundedCommunicationTask::new(task_handle, tx)
    }

    /// Spawn a scoped task that must complete within `duration`.
    ///
    /// If the future does not finish before `duration` elapses, it is dropped and the task
    /// completes with [`TimeoutError`]. This is the scoped analogue of
    /// [`ExecutorTimeout::spawn_timeout`].
    pub fn spawn_timeout<F>(
        &'scope self,
        duration: std::time::Duration,
        f: F,
    ) -> ScopedJoinHandle<Result<F::Output, TimeoutError>>
    where
        F: Future + Send + 'scope,
        F::Output: Send + 'scope,
    {
        self.spawn(Timeout::from_future(f, duration).map_err(|_| TimeoutError))
    }

    /// Spawns a task after waiting for a duration before the task is polled.
    pub fn spawn_delay<F>(
        &'scope self,
        duration: std::time::Duration,
        f: F,
    ) -> ScopedJoinHandle<F::Output>
    where
        F: Future + Send + 'scope,
        F::Output: Send + 'scope,
    {
        self.spawn(async move {
            let _ = Timeout::from_future(futures::future::pending::<()>(), duration).await;
            f.await
        })
    }

    /// Spawn a scoped task that must complete within `duration`, returning an
    /// [`AbortableJoinHandle`] that cancels the task once all references to it are dropped.
    ///
    /// If the future does not finish before `duration` elapses, it is dropped and the task
    /// completes with [`TimeoutError`].
    pub fn spawn_abortable_timeout<F>(
        &'scope self,
        duration: std::time::Duration,
        f: F,
    ) -> AbortableJoinHandle<Result<F::Output, TimeoutError>>
    where
        F: Future + Send + 'scope,
        F::Output: Send + 'scope,
    {
        self.spawn_abortable(Timeout::from_future(f, duration).map_err(|_| TimeoutError))
    }

    /// Spawns a task after waiting for a duration before the task is polled.
    pub fn spawn_abortable_delay<F>(
        &'scope self,
        duration: std::time::Duration,
        f: F,
    ) -> AbortableJoinHandle<F::Output>
    where
        F: Future + Send + 'scope,
        F::Output: Send + 'scope,
    {
        self.spawn_abortable(async move {
            let _ = Timeout::from_future(futures::future::pending::<()>(), duration).await;
            f.await
        })
    }
}

/// Drive a scope once: absorb any inbox entries, then poll the active
/// set until no more immediate progress can be made.
fn drive_scope<'scope>(
    active: &mut FuturesUnordered<BoxFuture<'scope, ()>>,
    state: &ScopeState<'scope>,
    cx: &mut Context<'_>,
) -> (bool, bool) {
    let mut made_progress = false;
    loop {
        state.waker.register(cx.waker());

        // Take the queued tasks while holding the lock only long enough to
        // swap the inbox, never while polling user code.
        let incoming = std::mem::take(&mut *state.inbox.lock());
        active.extend(incoming);
        match active.poll_next_unpin(cx) {
            Poll::Ready(Some(())) => made_progress = true,
            Poll::Ready(None) => {
                state.waker.register(cx.waker());
                if state.inbox.lock().is_empty() {
                    return (made_progress, true);
                }
            }
            Poll::Pending => {
                state.waker.register(cx.waker());
                if state.inbox.lock().is_empty() {
                    return (made_progress, false);
                }
            }
        }
    }
}

/// A handle to a task spawned on a [`Scope`].
///
/// Awaiting the handle yields the task's output. A panicking task yields
/// [`JoinError::Panicked`]. If the scope is dropped before the task finishes
/// (for example, because the scope future was cancelled), awaiting yields
/// [`JoinError::Cancelled`].
///
/// The handle does not borrow the spawned future itself. It can therefore
/// outlive the scope when its output type is also `'static`, which borrowed output
/// types retain their normal lifetime restrictions.
pub struct ScopedJoinHandle<T> {
    rx: oneshot::Receiver<Result<T, JoinError>>,
}

impl<T> Future for ScopedJoinHandle<T> {
    type Output = Result<T, JoinError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match Pin::new(&mut self.rx).poll(cx) {
            Poll::Ready(Ok(result)) => Poll::Ready(result),
            Poll::Ready(Err(_)) => Poll::Ready(Err(JoinError::Cancelled)),
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Create a scope for spawning tasks that borrow from the enclosing stack
/// frame.
///
/// The provided async closure receives a reference to the [`Scope`] and
/// returns a future. That future is driven alongside any tasks spawned on
/// the scope; when it completes, any still-running spawned tasks are
/// drained before `scope` returns its value.
///
/// # Example
///
/// ```no_run
/// # async fn run() {
/// use async_rt::scoped::scope;
///
/// let data = vec![1, 2, 3, 4];
/// let data = &data;
/// let sum = scope(async |s| {
///     let a = s.spawn(async { data[0] + data[1] });
///     let b = s.spawn(async { data[2] + data[3] });
///     a.await.unwrap() + b.await.unwrap()
/// })
/// .await;
/// assert_eq!(sum, 10);
/// # }
/// ```
pub async fn scope<'env, F, T>(f: F) -> T
where
    F: for<'scope> AsyncFnOnce(&'scope Scope<'scope, 'env>) -> T,
{
    // Declaration order is part of the safety invariant: `active` is dropped
    // first, then `state` (and its queued tasks), and finally `scope`.
    let mut scope = Scope::new();
    let state = Arc::new(ScopeState {
        inbox: Mutex::new(Vec::new()),
        waker: AtomicWaker::new(),
    });
    scope.state = Arc::downgrade(&state);
    // The active task set lives on the driver, not inside `Scope`. Only
    // `drive_scope` touches it, so no lock guards it, and the inbox mutex
    // is enough for the spawn side.
    let mut active = FuturesUnordered::new();

    let result = {
        let user_fut = f(&scope);
        let mut user_fut = std::pin::pin!(user_fut);

        poll_fn(|cx| {
            loop {
                if let Poll::Ready(r) = user_fut.as_mut().poll(cx) {
                    return Poll::Ready(r);
                }
                let (made_progress, _empty) = drive_scope(&mut active, &state, cx);
                if !made_progress {
                    return Poll::Pending;
                }
            }
        })
        .await
    };

    // Drain any remaining spawned tasks before the scope goes away, so
    // every borrow of the data is released before this frame unwinds.
    poll_fn(|cx| {
        let (_made_progress, empty) = drive_scope(&mut active, &state, cx);
        if empty {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    })
    .await;

    result
}

/// An [`Executor`] wrapper that tracks spawned tasks so they can be
/// canceled when a scope ends.
///
/// `ScopeExecutor` itself implements [`Executor`], so it can be passed to
/// any code that takes `Executor` every spawn performed through
/// that code will be tracked by the scope.
///
/// Because submitted futures must still be `Send + 'static` (enforced by
/// [`Executor::spawn`]), this flavour of scope does *not* allow borrowing
/// from the enclosing stack frame. Use [`scope`] for that.
pub struct ScopeExecutor<'scope, E> {
    inner: &'scope E,
    task_handles: Mutex<Vec<AbortableJoinHandle<()>>>,
    _scope: PhantomData<&'scope mut &'scope ()>,
}

impl<'scope, E> ScopeExecutor<'scope, E> {
    fn new(inner: &'scope E) -> Self {
        Self {
            inner,
            task_handles: Mutex::new(Vec::new()),
            _scope: PhantomData,
        }
    }

    /// Abort every task currently tracked by this scope.
    fn abort_all(&self) {
        for handle in self.task_handles.lock().iter() {
            handle.abort();
        }
    }
}

impl<E> ScopeExecutor<'_, E>
where
    E: Executor,
{
    fn spawn_tracked<F, T>(&self, future: F) -> JoinHandle<T>
    where
        F: Future<Output = Result<T, JoinError>> + Send + 'static,
        T: Send + 'static,
    {
        let (abort_handle, abort_registration) = AbortHandle::new_pair();
        let (tx, rx) = oneshot::channel();
        let finished = Arc::new(AtomicBool::new(false));
        let completion = CompletionGuard::new(finished.clone());
        let wrapped = async move {
            let _completion = completion;
            let result = abortable_result(future, abort_registration)
                .await
                .and_then(|result| result);
            let _ = tx.send(result);
        };

        // Track an abort-on-drop handle so cancellation remains effective even
        // after the handles are drained from `ScopeExecutor` for joining.
        let task_handle = self.inner.spawn_abortable(wrapped);
        self.task_handles.lock().push(task_handle);

        JoinHandle {
            inner: InnerJoinHandle::CustomHandle {
                inner: Optional::new(rx),
                handle: abort_handle,
                finished,
            },
        }
    }
}

impl<E> Drop for ScopeExecutor<'_, E> {
    fn drop(&mut self) {
        self.abort_all();
    }
}

impl<'scope, E> Executor for ScopeExecutor<'scope, E>
where
    E: Executor,
{
    fn runtime_type(&self) -> Option<&'static str> {
        self.inner.runtime_type()
    }

    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        self.spawn_tracked(async move { Ok(future.await) })
    }
}

impl<E> ExecutorBlocking for ScopeExecutor<'_, E>
where
    E: ExecutorBlocking,
{
    fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
    where
        F: FnOnce() -> R + Send + 'static,
        R: Send + 'static,
    {
        let blocking_handle = self.inner.spawn_blocking_abortable(f);
        // Note that the monitor owns the blocking handle, so aborting the monitor drops its
        // abort-on-drop handle, which prevents queued work from starting when
        // the backend supports that. Work already running may continue.
        self.spawn_tracked(blocking_handle)
    }
}

impl<E> ExecutorTimeout for ScopeExecutor<'_, E> where E: Executor {}

/// Run an async closure with a scoped [`Executor`] wrapper.
///
/// If the future containing `executor_scope` itself is dropped
/// (canceled externally), it will abort every outstanding task.
///
/// Unlike [`scope`], futures spawned here must be `Send + 'static`
/// they're going onto the real executor, so they can't borrow from the
/// enclosing stack frame.
///
/// # Example
///
/// ```no_run
/// # async fn run() {
/// use async_rt::Executor;
/// use async_rt::global::ConfiguredExecutor;
///
/// let executor: ConfiguredExecutor = ConfiguredExecutor::default();
/// let total = executor
///     .executor_scope(async |s| {
///         let a = s.spawn(async { 1 + 2 });
///         let b = s.spawn(async { 3 + 4 });
///         a.await.unwrap() + b.await.unwrap()
///     })
///     .await;
///     assert_eq!(total, 10);
/// # }
/// ```
pub async fn executor_scope<'scope, E, F, T>(executor: &'scope E, f: F) -> T
where
    E: Executor,
    F: AsyncFnOnce(&ScopeExecutor<'scope, E>) -> T,
{
    let scope_exec = ScopeExecutor::new(executor);
    let result = f(&scope_exec).await;

    // Wait for every spawned task to complete
    let handles: Vec<_> = scope_exec.task_handles.lock().drain(..).collect();
    for handle in handles {
        let _ = handle.await;
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(feature = "tokio")]
    use futures_timer::Delay;
    #[cfg(feature = "tokio")]
    use std::time::Duration;

    #[tokio::test]
    async fn borrows_stack_data() {
        let data = vec![1, 2, 3, 4];
        let data = &data;
        let sum = scope(async |s: &Scope<'_, '_>| {
            let a = s.spawn(async move { data[0] + data[1] });
            let b = s.spawn(async move { data[2] + data[3] });
            a.await.unwrap() + b.await.unwrap()
        })
        .await;
        assert_eq!(sum, 10);
    }

    #[tokio::test]
    async fn drains_unawaited_tasks() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        let counter = AtomicUsize::new(0);
        let counter_ref = &counter;
        scope(async |s: &Scope<'_, '_>| {
            for _ in 0..8 {
                s.spawn(async move {
                    counter_ref.fetch_add(1, Ordering::SeqCst);
                });
            }
        })
        .await;
        assert_eq!(counter.load(Ordering::SeqCst), 8);
    }

    #[tokio::test]
    async fn returns_closure_value() {
        let v: i32 = scope(async |_s: &Scope<'_, '_>| 42).await;
        assert_eq!(v, 42);
    }

    #[tokio::test]
    async fn join_handle_yields_output() {
        let out = scope(async |s: &Scope<'_, '_>| {
            let h = s.spawn(async { "hello" });
            h.await.unwrap()
        })
        .await;
        assert_eq!(out, "hello");
    }

    #[cfg(panic = "unwind")]
    #[tokio::test]
    async fn join_handle_reports_task_panic() {
        let result = scope(async |s: &Scope<'_, '_>| {
            s.spawn(async { panic!("expected scoped task panic") })
                .await
        })
        .await;

        assert!(matches!(result, Err(JoinError::Panicked)));
    }

    #[cfg(panic = "unwind")]
    #[tokio::test]
    async fn unawaited_task_panic_does_not_stop_other_tasks() {
        let completed = AtomicBool::new(false);

        scope(async |s: &Scope<'_, '_>| {
            s.spawn(async { panic!("expected unawaited scoped task panic") });
            s.spawn(async {
                completed.store(true, std::sync::atomic::Ordering::SeqCst);
            });
        })
        .await;

        assert!(completed.load(std::sync::atomic::Ordering::SeqCst));
    }

    #[tokio::test]
    async fn abortable_handle_reports_completion_without_being_polled() {
        scope(async |s: &Scope<'_, '_>| {
            let handle = s.spawn_abortable(async {});

            // Yield the user future so the cooperative driver can complete
            // the child without polling its join handle.
            crate::task::yield_now().await;

            assert!(handle.is_finished());
        })
        .await;
    }

    #[test]
    fn pushing_task_wakes_scope_driver() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::task::{Wake, Waker};

        struct WakeCounter(AtomicUsize);

        impl Wake for WakeCounter {
            fn wake(self: Arc<Self>) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }

            fn wake_by_ref(self: &Arc<Self>) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
        }

        let mut scope = Scope::new();
        let state = Arc::new(ScopeState {
            inbox: Mutex::new(Vec::new()),
            waker: AtomicWaker::new(),
        });
        scope.state = Arc::downgrade(&state);

        let wake_counter = Arc::new(WakeCounter(AtomicUsize::new(0)));
        let waker = Waker::from(wake_counter.clone());
        state.waker.register(&waker);

        let scope_ref = &scope;
        scope.push(
            async move {
                // Consume the driver's registered waker while tasks are
                // being polled, forcing drive_scope to register it again.
                scope_ref.push(async {}.boxed());
            }
            .boxed(),
        );

        assert_eq!(state.inbox.lock().len(), 1);
        assert_eq!(wake_counter.0.load(Ordering::SeqCst), 1);

        let mut active = FuturesUnordered::new();
        let mut context = Context::from_waker(&waker);
        let (_, empty) = drive_scope(&mut active, &state, &mut context);
        assert!(empty);
        assert_eq!(wake_counter.0.load(Ordering::SeqCst), 2);

        // The nested push consumed the previous registration. This push
        // only wakes the driver if drive_scope registered again afterward.
        scope.push(async {}.boxed());
        assert_eq!(wake_counter.0.load(Ordering::SeqCst), 3);

        let (_, empty) = drive_scope(&mut active, &state, &mut context);
        assert!(empty);

        let state_ref = Arc::downgrade(&state);
        scope.push(
            poll_fn(move |_cx| {
                // Model a push that occurs after registration but before
                // the inbox is drained: its wake is consumed, then the
                // newly active task returns Pending.
                state_ref.upgrade().unwrap().waker.wake();
                Poll::<()>::Pending
            })
            .boxed(),
        );
        assert_eq!(wake_counter.0.load(Ordering::SeqCst), 4);

        let (_, empty) = drive_scope(&mut active, &state, &mut context);
        assert!(!empty);
        let wakes_after_pending = wake_counter.0.load(Ordering::SeqCst);
        assert!(wakes_after_pending > 4);

        // The Pending path must have registered once more before returning.
        scope.push(async {}.boxed());
        assert_eq!(
            wake_counter.0.load(Ordering::SeqCst),
            wakes_after_pending + 1
        );
    }

    #[tokio::test]
    async fn many_concurrent_tasks_complete() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        let counter = AtomicUsize::new(0);
        let counter_ref = &counter;
        let total: usize = scope(async |s: &Scope<'_, '_>| {
            let handles: Vec<_> = (0..32)
                .map(|i| {
                    s.spawn(async move {
                        counter_ref.fetch_add(1, Ordering::SeqCst);
                        i
                    })
                })
                .collect();
            let mut sum = 0usize;
            for h in handles {
                sum += h.await.unwrap();
            }
            sum
        })
        .await;
        assert_eq!(total, (0..32).sum());
        assert_eq!(counter.load(Ordering::SeqCst), 32);
    }

    #[tokio::test]
    async fn child_task_can_spawn_nested_task() {
        let result = scope(async |s: &Scope<'_, '_>| {
            let outer = s.spawn(async move {
                let inner = s.spawn(async { 41usize });
                inner.await.unwrap() + 1
            });
            outer.await.unwrap()
        })
        .await;

        assert_eq!(result, 42);
    }

    #[tokio::test]
    async fn drains_unawaited_nested_task() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let nested_ran = AtomicBool::new(false);
        let nested_ran_ref = &nested_ran;

        scope(async |s: &Scope<'_, '_>| {
            s.dispatch(async move {
                s.dispatch(async move {
                    nested_ran_ref.store(true, Ordering::SeqCst);
                });
            });
        })
        .await;

        assert!(nested_ran.load(Ordering::SeqCst));
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_runs_tasks() {
        use crate::rt::tokio::TokioExecutor;
        let executor = TokioExecutor;
        let total = executor
            .executor_scope(async |s| {
                let a = s.spawn(async { 1 + 2 });
                let b = s.spawn(async { 3 + 4 });
                a.await.unwrap() + b.await.unwrap()
            })
            .await;
        assert_eq!(total, 10);
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_supports_timeouts() {
        use crate::rt::tokio::TokioExecutor;
        use futures::future::pending;

        let executor = TokioExecutor;
        executor
            .executor_scope(async |s| {
                let timeout = s.spawn_timeout(Duration::from_millis(10), pending::<()>());
                assert!(matches!(timeout.await, Ok(Err(TimeoutError))));

                let abortable_timeout =
                    s.spawn_abortable_timeout(Duration::from_millis(10), pending::<()>());
                assert!(matches!(abortable_timeout.await, Ok(Err(TimeoutError))));
            })
            .await;
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_supports_blocking_tasks() {
        use crate::rt::tokio::TokioExecutor;

        let executor = TokioExecutor;
        executor
            .executor_scope(async |s| {
                assert_eq!(s.spawn_blocking(|| 42).await.unwrap(), 42);

                let panicked = s
                    .spawn_blocking(|| -> () { panic!("deliberate blocking task panic") })
                    .await;
                assert!(matches!(panicked, Err(JoinError::Panicked)));
            })
            .await;
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_drains_unawaited_blocking_tasks() {
        use crate::rt::tokio::TokioExecutor;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, Ordering};

        let executor = TokioExecutor;
        let completed = Arc::new(AtomicBool::new(false));
        let task_completed = completed.clone();

        executor
            .executor_scope(async move |s| {
                let _handle = s.spawn_blocking(move || {
                    std::thread::sleep(Duration::from_millis(25));
                    task_completed.store(true, Ordering::SeqCst);
                });
            })
            .await;

        assert!(completed.load(Ordering::SeqCst));
    }

    #[cfg(feature = "tokio")]
    #[tokio::test(flavor = "current_thread")]
    async fn executor_scope_blocking_abort_is_reported() {
        use crate::rt::tokio::TokioExecutor;
        use futures::future::{Either, select};

        let executor = TokioExecutor;
        executor
            .executor_scope(async |s| {
                let (started_tx, started_rx) = oneshot::channel();
                let (release_tx, release_rx) = std::sync::mpsc::channel();
                let handle = s.spawn_blocking(move || {
                    let _ = started_tx.send(());
                    let _ = release_rx.recv();
                });

                started_rx.await.unwrap();
                handle.abort();

                let handle = Box::pin(handle);
                let result = match select(handle, Delay::new(Duration::from_secs(1))).await {
                    Either::Left((result, _)) => result,
                    Either::Right((_, handle)) => {
                        release_tx.send(()).unwrap();
                        let _ = handle.await;
                        panic!("aborting the blocking handle did not cancel its monitor");
                    }
                };

                release_tx.send(()).unwrap();
                assert!(matches!(result, Err(JoinError::Aborted)));
            })
            .await;
    }

    #[cfg(feature = "tokio")]
    #[tokio::test(flavor = "current_thread")]
    async fn executor_scope_handle_reports_completion_without_being_polled() {
        use crate::rt::tokio::TokioExecutor;

        let executor = TokioExecutor;
        executor
            .executor_scope(async |s| {
                let (completed_tx, completed_rx) = oneshot::channel();
                let handle = s.spawn(async move {
                    let _ = completed_tx.send(());
                });

                completed_rx.await.unwrap();

                // On the current-thread runtime, the spawned wrapper finishes
                // before the task it woke can be polled again.
                assert!(handle.is_finished());
            })
            .await;
    }

    #[tokio::test]
    async fn scope_spawn_coroutine_receives_messages() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        let total = AtomicUsize::new(0);
        let total_ref = &total;

        scope(async |s: &Scope<'_, '_>| {
            let mut task = s.spawn_coroutine(|value| async move {
                total_ref.fetch_add(value, Ordering::SeqCst);
            });
            for v in [1usize, 2, 3, 4] {
                task.send(v).await.unwrap();
            }
            drop(task); // closes channel → coroutine exits cleanly
        })
        .await;

        assert_eq!(total.load(Ordering::SeqCst), 10);
    }

    #[tokio::test]
    async fn scope_receiver_coroutine_receives_messages() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        let total = AtomicUsize::new(0);
        let total_ref = &total;

        scope(async |s: &Scope<'_, '_>| {
            let mut task = s.spawn_coroutine_with_receiver(|mut rx| async move {
                while let Some(value) = rx.next().await {
                    total_ref.fetch_add(value, Ordering::SeqCst);
                }
            });
            for value in [1usize, 2, 3, 4] {
                task.send(value).await.unwrap();
            }
            drop(task);
        })
        .await;

        assert_eq!(total.load(Ordering::SeqCst), 10);
    }

    #[tokio::test]
    async fn scope_coroutine_api_matches_executor() {
        use futures::future::ready;

        scope(async |s: &Scope<'_, '_>| {
            let task = s.spawn_coroutine_with_buffer(2, |_value: usize| ready(()));
            drop(task);

            let task = s.spawn_unbounded_coroutine(|_value: usize| ready(()));
            drop(task);

            let task =
                s.spawn_coroutine_with_context(0usize, |context: &mut usize, value: usize| {
                    *context += value;
                    ready(())
                });
            drop(task);

            let task = s.spawn_coroutine_with_buffer_and_context(
                0usize,
                2,
                |context: &mut usize, value: usize| {
                    *context += value;
                    ready(())
                },
            );
            drop(task);

            let task = s.spawn_unbounded_coroutine_with_context(
                0usize,
                |context: &mut usize, value: usize| {
                    *context += value;
                    ready(())
                },
            );
            drop(task);

            let task =
                s.spawn_coroutine_with_receiver_and_buffer(2, |_rx: Receiver<usize>| async {});
            drop(task);

            let task = s.spawn_coroutine_with_receiver_and_context(
                0usize,
                |_context, _rx: Receiver<usize>| async {},
            );
            drop(task);

            let task = s.spawn_coroutine_with_receiver_buffer_and_context(
                0usize,
                2,
                |_context, _rx: Receiver<usize>| async {},
            );
            drop(task);

            let task =
                s.spawn_unbounded_coroutine_with_receiver(|_rx: UnboundedReceiver<usize>| async {});
            drop(task);

            let task = s.spawn_unbounded_coroutine_with_receiver_and_context(
                0usize,
                |_context, _rx: UnboundedReceiver<usize>| async {},
            );
            drop(task);
        })
        .await;
    }

    #[tokio::test]
    async fn scope_dispatch_runs_fire_and_forget() {
        use std::sync::atomic::{AtomicBool, Ordering};
        let flag = AtomicBool::new(false);
        let flag_ref = &flag;

        scope(async |s: &Scope<'_, '_>| {
            s.dispatch(async move {
                flag_ref.store(true, Ordering::SeqCst);
            });
        })
        .await;

        assert!(flag.load(Ordering::SeqCst));
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_drains_unawaited_tasks() {
        use crate::rt::tokio::TokioExecutor;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, Ordering};

        let executor = TokioExecutor;
        let flag = Arc::new(AtomicBool::new(false));
        let flag_clone = flag.clone();

        executor
            .executor_scope(async move |s| {
                // Spawn a task but do NOT await its handle.
                // executor_scope should wait for it to complete before
                // returning.
                let _h = s.spawn(async move {
                    Delay::new(Duration::from_millis(50)).await;
                    flag_clone.store(true, Ordering::SeqCst);
                });
            })
            .await;

        assert!(
            flag.load(Ordering::SeqCst),
            "unawaited task should have completed before executor_scope returned"
        );
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_swallows_task_panic() {
        use crate::rt::tokio::TokioExecutor;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let executor = TokioExecutor;
        let sibling_done = Arc::new(AtomicUsize::new(0));
        let sibling_done_clone = sibling_done.clone();

        let result = executor
            .executor_scope(async move |s| {
                // Task A: panics. Its JoinHandle is dropped without
                // being awaited. the panic goes to the runtime.
                let _panicker = s.spawn(async {
                    panic!("deliberate test panic");
                });
                // Task B: completes normally. The scope should still
                // drain it before returning.
                let _sibling = s.spawn(async move {
                    sibling_done_clone.fetch_add(1, Ordering::SeqCst);
                });
                42usize
            })
            .await;

        assert_eq!(result, 42);
        assert_eq!(sibling_done.load(Ordering::SeqCst), 1);
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_aborts_on_external_cancel() {
        use crate::rt::tokio::TokioExecutor;
        use futures::future::Either;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, Ordering};

        let executor = TokioExecutor;
        let flag = Arc::new(AtomicBool::new(false));
        let flag_clone = flag.clone();

        {
            let scope_fut = executor.executor_scope(async move |s| {
                let _h = s.spawn(async move {
                    Delay::new(Duration::from_millis(200)).await;
                    flag_clone.store(true, Ordering::SeqCst);
                });
                futures::future::pending::<()>().await;
            });

            let scope_fut = std::pin::pin!(scope_fut);
            let timer = std::pin::pin!(Delay::new(Duration::from_millis(30)));
            let result = futures::future::select(scope_fut, timer).await;
            assert!(
                matches!(result, Either::Right(_)),
                "timer should have won the race"
            );
        }

        // Give the (supposedly aborted) task plenty of time.
        Delay::new(Duration::from_millis(300)).await;
        assert!(
            !flag.load(Ordering::SeqCst),
            "task should have been aborted by scope drop"
        );
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn executor_scope_aborts_when_cancelled_during_drain() {
        use crate::rt::tokio::TokioExecutor;
        use futures::future::{Either, pending, select};

        let executor = TokioExecutor;
        let (started_tx, started_rx) = oneshot::channel();
        let (held_tx, held_rx) = oneshot::channel::<()>();

        let scope_fut = Box::pin(executor.executor_scope(async move |s| {
            let _handle = s.spawn(async move {
                let _held_until_task_drop = held_tx;
                let _ = started_tx.send(());
                pending::<()>().await;
            });
        }));

        let scope_fut = match select(scope_fut, started_rx).await {
            Either::Right((Ok(()), scope_fut)) => scope_fut,
            Either::Left(_) => panic!("scope unexpectedly completed"),
            Either::Right((Err(_), _)) => panic!("child task never started"),
        };

        drop(scope_fut);

        match select(held_rx, Delay::new(Duration::from_secs(1))).await {
            Either::Left((Err(_), _)) => {}
            Either::Left((Ok(_), _)) => unreachable!("child never sends a value"),
            Either::Right(_) => panic!("child remained detached after scope cancellation"),
        }
    }
}