runite 0.1.0

An event-loop-per-thread async runtime built on io_uring (Linux), kqueue (macOS), and IOCP (Windows)
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
use core::fmt;
use core::future::Future;
use core::pin::Pin;
use core::task::Poll;

use crate::{JoinHandle, spawn};

/// Error returned by awaiting a join handle or [`JoinSet`] task.
///
/// Produced both by [`crate::task::BlockingJoinHandle`] (when a blocking-pool
/// worker exits without delivering a value) and by [`crate::JoinHandle`] or
/// [`JoinSet`] (when a queued future is aborted before it completes). A queued
/// task's join output is `Result<T, JoinError>`, so callers should handle these
/// errors when awaiting any join handle.
///
/// A queued future that **panics** while being polled resolves its join handle
/// to [`JoinError::Panicked`] rather than unwinding the event loop: runite
/// isolates task panics so one misbehaving task cannot take down the runtime
/// thread (the panic is still reported through the process panic hook).
///
/// Use [`JoinError::is_cancelled`], [`JoinError::is_aborted`], and
/// [`JoinError::is_panicked`] when the caller only needs to distinguish the
/// category.
///
/// # Examples
///
/// ```
/// use std::rc::Rc;
/// use std::cell::Cell;
///
/// let saw_abort = Rc::new(Cell::new(false));
/// let saw_abort_task = Rc::clone(&saw_abort);
///
/// runite::spawn(async move {
///     let mut set = runite::task::JoinSet::<()>::new();
///     set.spawn(async { std::future::pending::<()>().await });
///     set.abort_all();
///     let result = set.join_next().await.expect("aborted task should remain joinable");
///     saw_abort_task.set(result.expect_err("task should be aborted").is_aborted());
/// });
///
/// runite::run();
/// assert!(saw_abort.get());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum JoinError {
    /// The worker exited without producing a value.
    ///
    /// This is used for blocking tasks whose result channel closes before the
    /// worker delivers a value, such as during runtime shutdown or panic
    /// unwinding.
    Cancelled,
    /// The task was aborted before it completed.
    ///
    /// This is returned by [`crate::JoinHandle`] when
    /// [`JoinHandle::abort`](crate::JoinHandle::abort), an
    /// [`AbortHandle`](crate::AbortHandle), or [`JoinSet::abort_all`] cancels
    /// the queued future.
    Aborted,
    /// The task panicked while being polled.
    ///
    /// runite catches panics that unwind out of a spawned future so the panic
    /// does not tear down the whole runtime thread; the joiner observes this
    /// variant instead. The panic itself is still reported through the process
    /// panic hook (message and, if enabled, backtrace on stderr). The panic
    /// payload is not carried on the error, so that [`JoinError`] can stay
    /// `Copy`.
    Panicked,
}

impl JoinError {
    /// Returns `true` if the task was aborted before completion.
    ///
    /// This is true only for [`JoinError::Aborted`].
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let observed = Rc::new(Cell::new(false));
    /// let observed_task = Rc::clone(&observed);
    ///
    /// runite::spawn(async move {
    ///     let mut set = runite::task::JoinSet::<()>::new();
    ///     set.spawn(async { std::future::pending::<()>().await });
    ///     set.abort_all();
    ///     let err = set.join_next().await.unwrap().unwrap_err();
    ///     observed_task.set(err.is_aborted());
    /// });
    ///
    /// runite::run();
    /// assert!(observed.get());
    /// ```
    pub fn is_aborted(&self) -> bool {
        matches!(self, JoinError::Aborted)
    }

    /// Returns `true` if a blocking-pool worker was cancelled without producing
    /// a value.
    ///
    /// This is true only for [`JoinError::Cancelled`].
    ///
    /// # Examples
    ///
    /// ```
    /// assert!(runite::task::JoinError::Cancelled.is_cancelled());
    /// assert!(!runite::task::JoinError::Aborted.is_cancelled());
    /// ```
    pub fn is_cancelled(&self) -> bool {
        matches!(self, JoinError::Cancelled)
    }

    /// Returns `true` if the task panicked while being polled.
    ///
    /// This is true only for [`JoinError::Panicked`].
    ///
    /// # Examples
    ///
    /// ```
    /// assert!(runite::task::JoinError::Panicked.is_panicked());
    /// assert!(!runite::task::JoinError::Aborted.is_panicked());
    /// ```
    pub fn is_panicked(&self) -> bool {
        matches!(self, JoinError::Panicked)
    }
}

impl fmt::Display for JoinError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            JoinError::Cancelled => f.write_str("blocking task was cancelled"),
            JoinError::Aborted => f.write_str("task was aborted"),
            JoinError::Panicked => f.write_str("task panicked"),
        }
    }
}

impl std::error::Error for JoinError {}

/// A collection of spawned local tasks that yields results as tasks complete.
///
/// `JoinSet` owns each task's [`JoinHandle`]. Dropping the set aborts any tasks
/// that are still running, making it a structured-concurrency primitive: child
/// tasks cannot silently outlive the owner unless [`detach_all`](Self::detach_all)
/// is called first.
///
/// Like all futures in this runtime, tasks in a `JoinSet` are `!Send` and stay on
/// the runtime thread where they were spawned. This differs from Tokio's
/// multithreaded `JoinSet`: runite owns local tasks on one event-loop thread,
/// dropping the set aborts remaining tasks, and dropping an individual
/// [`JoinHandle`] detaches that task instead of cancelling it.
///
/// # Examples
///
/// ```
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// let results = Rc::new(RefCell::new(Vec::new()));
/// let results_task = Rc::clone(&results);
///
/// runite::spawn(async move {
///     let mut set = runite::task::JoinSet::new();
///     set.spawn(async { 1usize });
///     set.spawn(async { 2usize });
///
///     while let Some(result) = set.join_next().await {
///         results_task.borrow_mut().push(result.expect("task should finish"));
///     }
/// });
///
/// runite::run();
/// results.borrow_mut().sort_unstable();
/// assert_eq!(&*results.borrow(), &[1, 2]);
/// ```
pub struct JoinSet<T> {
    handles: Vec<JoinHandle<T>>,
}

impl<T> JoinSet<T> {
    /// Creates an empty `JoinSet`.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let was_empty = Rc::new(Cell::new(false));
    /// let was_empty_task = Rc::clone(&was_empty);
    ///
    /// runite::spawn(async move {
    ///     let set = runite::task::JoinSet::<usize>::new();
    ///     was_empty_task.set(set.is_empty());
    /// });
    ///
    /// runite::run();
    /// assert!(was_empty.get());
    /// ```
    pub fn new() -> Self {
        Self {
            handles: Vec::new(),
        }
    }

    /// Spawns `future` on the current runtime thread and adds it to the set.
    ///
    /// The task is scheduled through [`crate::spawn`] as a microtask. Its output
    /// can later be retrieved by awaiting [`join_next`](Self::join_next).
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let result = Rc::new(Cell::new(0));
    /// let result_task = Rc::clone(&result);
    ///
    /// runite::spawn(async move {
    ///     let mut set = runite::task::JoinSet::new();
    ///     set.spawn(async { 42 });
    ///     result_task.set(set.join_next().await.unwrap().unwrap());
    /// });
    ///
    /// runite::run();
    /// assert_eq!(result.get(), 42);
    /// ```
    pub fn spawn<F>(&mut self, future: F)
    where
        F: Future<Output = T> + 'static,
        T: 'static,
    {
        self.handles.push(spawn(future));
    }

    /// Waits for the next task in the set to complete.
    ///
    /// Resolves to `Some(Ok(value))` for a completed task,
    /// `Some(Err(JoinError::Aborted))` for an aborted task, or `None` when the
    /// set is empty. `abort_all` keeps handles in the set, so aborted tasks are
    /// reported by subsequent calls to `join_next`. `detach_all` removes handles,
    /// so detached tasks will not be reported.
    ///
    /// Each call returns one ready task result. If several tasks are ready at
    /// once, selection follows the set's internal scan order and is not a stable
    /// completion-order guarantee.
    ///
    /// # Performance
    ///
    /// This implementation linearly scans stored handles and registers the same
    /// waker with every pending task; smarter ready-queue wake bookkeeping is a
    /// follow-up optimization.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let result = Rc::new(Cell::new(None));
    /// let result_task = Rc::clone(&result);
    ///
    /// runite::spawn(async move {
    ///     let mut set = runite::task::JoinSet::new();
    ///     set.spawn(async { "done" });
    ///     result_task.set(Some(set.join_next().await.unwrap().unwrap()));
    /// });
    ///
    /// runite::run();
    /// assert_eq!(result.get(), Some("done"));
    /// ```
    pub fn join_next(&mut self) -> impl Future<Output = Option<Result<T, JoinError>>> + '_ {
        std::future::poll_fn(|cx| {
            if self.handles.is_empty() {
                return Poll::Ready(None);
            }

            // 0.1 keeps this deliberately simple: scan every handle and let
            // each pending task store this future's waker. A ready queue keyed
            // by task wakeups would avoid the linear scan in a follow-up.
            for index in 0..self.handles.len() {
                match Pin::new(&mut self.handles[index]).poll(cx) {
                    Poll::Ready(result) => {
                        self.handles.swap_remove(index);
                        return Poll::Ready(Some(result));
                    }
                    Poll::Pending => {}
                }
            }

            Poll::Pending
        })
    }

    /// Aborts all tasks currently owned by the set.
    ///
    /// Aborted handles remain in the set. Drain them with
    /// [`join_next`](Self::join_next) to observe their
    /// [`JoinError::Aborted`] results.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let aborted = Rc::new(Cell::new(false));
    /// let aborted_task = Rc::clone(&aborted);
    ///
    /// runite::spawn(async move {
    ///     let mut set = runite::task::JoinSet::<()>::new();
    ///     set.spawn(async { std::future::pending::<()>().await });
    ///     set.abort_all();
    ///     let err = set.join_next().await.unwrap().unwrap_err();
    ///     aborted_task.set(err.is_aborted());
    /// });
    ///
    /// runite::run();
    /// assert!(aborted.get());
    /// ```
    pub fn abort_all(&mut self) {
        for handle in &self.handles {
            handle.abort();
        }
    }

    /// Detaches all tasks from the set without aborting them.
    ///
    /// After this call the set is empty, and the detached tasks continue running
    /// to completion in the background. Dropping the set after `detach_all` will
    /// not abort those tasks.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let completed = Rc::new(Cell::new(false));
    /// let completed_task = Rc::clone(&completed);
    ///
    /// runite::spawn(async move {
    ///     let mut set = runite::task::JoinSet::new();
    ///     set.spawn(async move { completed_task.set(true); });
    ///     set.detach_all();
    /// });
    ///
    /// runite::run();
    /// assert!(completed.get());
    /// ```
    pub fn detach_all(&mut self) {
        self.handles.clear();
    }

    /// Returns the number of task handles currently owned by the set.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let len = Rc::new(Cell::new(0));
    /// let len_task = Rc::clone(&len);
    ///
    /// runite::spawn(async move {
    ///     let mut set = runite::task::JoinSet::new();
    ///     set.spawn(async { 1 });
    ///     len_task.set(set.len());
    /// });
    ///
    /// runite::run();
    /// assert_eq!(len.get(), 1);
    /// ```
    pub fn len(&self) -> usize {
        self.handles.len()
    }

    /// Returns `true` when the set owns no task handles.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::rc::Rc;
    /// use std::cell::Cell;
    ///
    /// let empty = Rc::new(Cell::new(false));
    /// let empty_task = Rc::clone(&empty);
    ///
    /// runite::spawn(async move {
    ///     let set = runite::task::JoinSet::<usize>::new();
    ///     empty_task.set(set.is_empty());
    /// });
    ///
    /// runite::run();
    /// assert!(empty.get());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.handles.is_empty()
    }
}

impl<T> Default for JoinSet<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Drop for JoinSet<T> {
    fn drop(&mut self) {
        self.abort_all();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{run, spawn};
    use std::cell::{Cell, RefCell};
    use std::rc::Rc;

    #[test]
    fn join_next_returns_results_as_tasks_complete() {
        let results = Rc::new(RefCell::new(Vec::new()));
        let results_task = Rc::clone(&results);

        spawn(async move {
            let mut set = JoinSet::new();
            set.spawn(async { 3 });
            set.spawn(async { 1 });
            set.spawn(async { 2 });

            while let Some(result) = set.join_next().await {
                results_task
                    .borrow_mut()
                    .push(result.expect("task should complete"));
            }
        });

        run();

        results.borrow_mut().sort_unstable();
        assert_eq!(&*results.borrow(), &[1, 2, 3]);
    }

    #[test]
    fn join_next_returns_none_when_empty() {
        let observed = Rc::new(Cell::new(false));
        let observed_task = Rc::clone(&observed);

        spawn(async move {
            let mut set = JoinSet::<usize>::new();
            observed_task.set(set.join_next().await.is_none());
        });

        run();

        assert!(observed.get());
    }

    #[test]
    fn abort_all_surfaces_aborted_join_errors() {
        let aborted = Rc::new(Cell::new(0));
        let aborted_task = Rc::clone(&aborted);

        spawn(async move {
            let mut set = JoinSet::<usize>::new();
            set.spawn(async { std::future::pending::<usize>().await });
            set.spawn(async { std::future::pending::<usize>().await });

            set.abort_all();

            let mut count = 0;
            while let Some(result) = set.join_next().await {
                assert!(result.expect_err("task should be aborted").is_aborted());
                count += 1;
            }
            aborted_task.set(count);
        });

        run();

        assert_eq!(aborted.get(), 2);
    }

    #[test]
    fn drop_aborts_running_tasks() {
        let completed = Rc::new(Cell::new(false));
        let completed_task = Rc::clone(&completed);

        spawn(async move {
            let mut set = JoinSet::new();
            set.spawn(async move {
                completed_task.set(true);
            });
            drop(set);
        });

        run();

        assert!(!completed.get());
    }
}