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
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
//! Single-value watch channels.
//!
//! A watch channel stores the latest value and notifies receivers when a newer
//! version is published. It is best for sharing state snapshots, configuration,
//! or status values where receivers do not need every intermediate update.
//! Waiters are tied to the runite runtime thread that first polls them; multiple
//! sends coalesce, so a receiver that waits once observes that the version
//! changed and then borrows the latest value.
//!
//! # Examples
//!
//! Borrow the latest value immediately, then wait for a later change.
//!
//! ```
//! let (sender, mut receiver) = runite::channel::watch::channel("initial");
//! assert_eq!(*receiver.borrow(), "initial");
//!
//! runite::spawn(async move {
//!     sender.send("updated").unwrap();
//! });
//!
//! runite::spawn(async move {
//!     receiver.changed().await.unwrap();
//!     assert_eq!(*receiver.borrow(), "updated");
//! });
//!
//! runite::run();
//! ```

use std::fmt;
use std::future::poll_fn;
use std::ops::Deref;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard};
use std::task::{Context, Poll};

use crate::op::completion::{CompletionFuture, CompletionHandle};
use crate::sys::current::channel::runtime_waiter;

/// Creates a watch channel initialized with `initial`.
///
/// A watch channel stores a single latest value. Receivers can borrow the
/// current value at any time and await notification when a newer version is
/// published.
///
/// # Examples
///
/// ```
/// let (sender, receiver) = runite::channel::watch::channel(1);
/// assert_eq!(*sender.borrow(), 1);
/// assert_eq!(*receiver.borrow(), 1);
/// ```
pub fn channel<T: Send + 'static>(initial: T) -> (Sender<T>, Receiver<T>) {
    let shared = Arc::new(Shared {
        value: RwLock::new(initial),
        version: AtomicU64::new(0),
        book: Arc::new(Mutex::new(Book {
            sender_count: 1,
            receiver_count: 1,
            waiters: Vec::new(),
            next_waiter_id: 1,
        })),
    });
    (
        Sender {
            shared: Arc::clone(&shared),
        },
        Receiver {
            shared,
            version: 0,
            wait: None,
        },
    )
}

/// Sending half of a watch channel.
///
/// Cloning a sender creates another producer for the same latest-value slot.
/// Sending replaces the stored value and wakes receivers that are waiting for a
/// newer version on their owning runtime threads. Intermediate values coalesce:
/// receivers observe that the version changed, then borrow the latest value.
pub struct Sender<T: Send + 'static> {
    shared: Arc<Shared<T>>,
}

/// Receiving half of a watch channel.
///
/// A receiver tracks the last version it observed. Use [`changed`](Self::changed)
/// to wait for a newer value, then [`borrow_and_update`](Self::borrow_and_update)
/// to access it and mark that version as seen.
pub struct Receiver<T: Send + 'static> {
    shared: Arc<Shared<T>>,
    version: u64,
    wait: Option<CompletionFuture<Result<u64, RecvError>>>,
}

/// Borrowed watch value.
///
/// This guard dereferences to the current value and holds a **read** lock on the
/// value slot while it is alive. Multiple `Ref`s may be held at once — including
/// several on the same thread — because reads are shared. Holding a `Ref` across
/// a [`Sender::send`] (or any mutation) on the *same thread* will deadlock,
/// because the send needs the write lock; keep borrows short and never hold one
/// across `.await`.
pub struct Ref<'a, T: Send + 'static> {
    guard: RwLockReadGuard<'a, T>,
}

/// Shared channel storage.
///
/// The value and the bookkeeping live behind **separate** locks so that a
/// `Ref` (a value read lock) does not block, and is not blocked by, waiter
/// bookkeeping. This is what lets two `Ref`s coexist on one thread — the old
/// single-`Mutex` design deadlocked on the second borrow.
struct Shared<T: Send + 'static> {
    /// Current value, guarded by an `RwLock` so multiple `Ref` borrows can be
    /// held concurrently.
    value: RwLock<T>,
    /// Monotonic version, bumped **under the `value` write lock** so a borrower
    /// that reads the value and the version under the `value` read lock always
    /// observes a consistent pair.
    version: AtomicU64,
    /// Waiter registry and reference counts. Held in its own `Arc<Mutex<_>>` so
    /// the (`Send`) cancel closure can capture just the bookkeeping without
    /// capturing `T`, keeping the channel usable with `Send`-but-not-`Sync`
    /// values on a single thread.
    book: Arc<Mutex<Book>>,
}

struct Book {
    sender_count: usize,
    receiver_count: usize,
    waiters: Vec<WatchWaiter>,
    next_waiter_id: usize,
}

struct WatchWaiter {
    id: usize,
    version: u64,
    handle: CompletionHandle<Result<u64, RecvError>>,
}

#[derive(Debug, Eq, PartialEq)]
/// Error returned when sending fails because there are no receivers.
///
/// The unsent replacement value is returned to the caller.
pub struct SendError<T>(pub T);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Error returned when a watch channel closes before another change arrives.
///
/// Existing values can still be borrowed; this error means all senders were
/// dropped while the receiver was waiting for a newer version.
pub struct RecvError;

impl Book {
    fn enqueue_waiter(
        &mut self,
        version: u64,
        handle: CompletionHandle<Result<u64, RecvError>>,
    ) -> usize {
        let id = self.next_waiter_id;
        self.next_waiter_id = self.next_waiter_id.wrapping_add(1);
        self.waiters.push(WatchWaiter {
            id,
            version,
            handle,
        });
        id
    }

    fn remove_waiter(&mut self, waiter_id: usize) {
        if let Some(index) = self
            .waiters
            .iter()
            .position(|waiter| waiter.id == waiter_id)
        {
            self.waiters.swap_remove(index);
        }
    }

    fn wake_changed(
        &mut self,
        current_version: u64,
    ) -> Vec<CompletionHandle<Result<u64, RecvError>>> {
        let mut ready = Vec::new();
        let mut index = 0;
        while index < self.waiters.len() {
            if self.waiters[index].version < current_version {
                ready.push(self.waiters.swap_remove(index).handle);
            } else {
                index += 1;
            }
        }
        ready
    }

    fn close_sender(&mut self) -> Vec<CompletionHandle<Result<u64, RecvError>>> {
        self.sender_count = self
            .sender_count
            .checked_sub(1)
            .expect("sender count underflow: more drops than creates");
        if self.sender_count == 0 {
            self.waiters.drain(..).map(|waiter| waiter.handle).collect()
        } else {
            Vec::new()
        }
    }
}

impl<T: Send + 'static> Shared<T> {
    fn lock_book(&self) -> std::sync::MutexGuard<'_, Book> {
        self.book
            .lock()
            .expect("watch state should not be poisoned")
    }

    /// Replaces the value via `mutate` and bumps the version, both under the
    /// value write lock so borrowers see a consistent (value, version) pair.
    /// Returns the new version.
    fn write_value(&self, mutate: impl FnOnce(&mut T)) -> u64 {
        let mut slot = self
            .value
            .write()
            .expect("watch state should not be poisoned");
        mutate(&mut slot);
        // Bump under the write lock: readers taking the value read lock cannot
        // observe the new value with the old version, or vice versa.
        self.version.fetch_add(1, Ordering::Release) + 1
    }
}

impl<T: Send + 'static> Clone for Sender<T> {
    fn clone(&self) -> Self {
        self.shared.lock_book().sender_count += 1;
        Self {
            shared: Arc::clone(&self.shared),
        }
    }
}

impl<T: Send + 'static> Clone for Receiver<T> {
    fn clone(&self) -> Self {
        self.shared.lock_book().receiver_count += 1;
        Self {
            shared: Arc::clone(&self.shared),
            version: self.version,
            wait: None,
        }
    }
}

impl<T: Send + 'static> Sender<T> {
    /// Replaces the watched value and notifies receivers.
    ///
    /// Returns [`SendError`] with the value if no receivers remain.
    ///
    /// # Examples
    ///
    /// ```
    /// runite::spawn(async {
    ///     let (sender, mut receiver) = runite::channel::watch::channel("old");
    ///     sender.send("new").unwrap();
    ///     receiver.changed().await.unwrap();
    ///     assert_eq!(*receiver.borrow(), "new");
    /// });
    ///
    /// runite::run();
    /// ```
    pub fn send(&self, value: T) -> Result<(), SendError<T>> {
        let waiters = {
            let mut book = self.shared.lock_book();
            if book.receiver_count == 0 {
                return Err(SendError(value));
            }
            let version = self.shared.write_value(|slot| *slot = value);
            book.wake_changed(version)
        };
        self.complete_changed(waiters);
        Ok(())
    }

    /// Mutates the watched value and notifies receivers.
    ///
    /// This notifies even if the closure leaves the value unchanged; use
    /// [`send_if_modified`](Self::send_if_modified) to make notification
    /// conditional. Unlike [`send`](Self::send), this method still mutates the
    /// stored value and does not return [`SendError`] when no receivers remain.
    ///
    /// # Examples
    ///
    /// ```
    /// let (sender, receiver) = runite::channel::watch::channel(1);
    /// sender.send_modify(|value| *value += 1);
    /// assert_eq!(*receiver.borrow(), 2);
    /// ```
    pub fn send_modify(&self, f: impl FnOnce(&mut T)) {
        let waiters = {
            let mut book = self.shared.lock_book();
            let version = self.shared.write_value(f);
            book.wake_changed(version)
        };
        self.complete_changed(waiters);
    }

    /// Mutates the watched value and notifies receivers if `f` returns `true`.
    ///
    /// Unlike [`send`](Self::send), this method does not return [`SendError`] if
    /// no receivers remain. It returns whether `f` reported a modification; when
    /// it returns `true`, the version is advanced even with zero receivers.
    ///
    /// # Examples
    ///
    /// ```
    /// let (sender, receiver) = runite::channel::watch::channel(1);
    /// assert!(!sender.send_if_modified(|_| false));
    /// assert!(sender.send_if_modified(|value| {
    ///     *value = 3;
    ///     true
    /// }));
    /// assert_eq!(*receiver.borrow(), 3);
    /// ```
    pub fn send_if_modified(&self, f: impl FnOnce(&mut T) -> bool) -> bool {
        let waiters = {
            let mut book = self.shared.lock_book();
            // Run `f` under the value write lock; only bump the version and
            // collect waiters if it reports a modification, otherwise bail
            // (dropping both guards) without advancing the version.
            let version = {
                let mut slot = self
                    .shared
                    .value
                    .write()
                    .expect("watch state should not be poisoned");
                if !f(&mut slot) {
                    return false;
                }
                self.shared.version.fetch_add(1, Ordering::Release) + 1
            };
            book.wake_changed(version)
        };
        self.complete_changed(waiters);
        true
    }

    /// Borrows the current value from the sender side.
    ///
    /// The returned [`Ref`] holds a read lock on the value until dropped.
    ///
    /// # Examples
    ///
    /// ```
    /// let (sender, _receiver) = runite::channel::watch::channel("visible");
    /// assert_eq!(*sender.borrow(), "visible");
    /// ```
    pub fn borrow(&self) -> Ref<'_, T> {
        Ref {
            guard: self
                .shared
                .value
                .read()
                .expect("watch state should not be poisoned"),
        }
    }

    /// Creates a new receiver that starts at the current version.
    ///
    /// The receiver considers the current value already observed and waits for
    /// subsequent calls to [`send`](Self::send) or mutation methods.
    ///
    /// # Examples
    ///
    /// ```
    /// let (sender, _receiver) = runite::channel::watch::channel(1);
    /// let second = sender.subscribe();
    /// assert_eq!(*second.borrow(), 1);
    /// ```
    pub fn subscribe(&self) -> Receiver<T> {
        let version = {
            let mut book = self.shared.lock_book();
            book.receiver_count += 1;
            self.shared.version.load(Ordering::Acquire)
        };
        Receiver {
            shared: Arc::clone(&self.shared),
            version,
            wait: None,
        }
    }

    /// Returns the number of active receivers.
    ///
    /// # Examples
    ///
    /// ```
    /// let (sender, receiver) = runite::channel::watch::channel(1);
    /// assert_eq!(sender.receiver_count(), 1);
    /// drop(receiver);
    /// assert_eq!(sender.receiver_count(), 0);
    /// ```
    pub fn receiver_count(&self) -> usize {
        self.shared.lock_book().receiver_count
    }

    fn complete_changed(&self, waiters: Vec<CompletionHandle<Result<u64, RecvError>>>) {
        let version = self.shared.version.load(Ordering::Acquire);
        for waiter in waiters {
            waiter.complete(Ok(version));
        }
    }
}

impl<T: Send + 'static> Receiver<T> {
    /// Waits until the watched value changes.
    ///
    /// Multiple sends before this receiver borrows and updates coalesce into one
    /// observed version change; use [`borrow_and_update`](Self::borrow_and_update)
    /// to mark the latest value as seen.
    ///
    /// Returns [`RecvError`] if all senders are dropped before a newer version
    /// becomes available.
    ///
    /// # Examples
    ///
    /// ```
    /// runite::spawn(async {
    ///     let (sender, mut receiver) = runite::channel::watch::channel(0);
    ///     sender.send(1).unwrap();
    ///     receiver.changed().await.unwrap();
    ///     assert_eq!(*receiver.borrow(), 1);
    /// });
    ///
    /// runite::run();
    /// ```
    ///
    /// # Cancel safety
    ///
    /// Cancel-safe: dropping the returned future before it resolves does not
    /// advance the receiver's observed version, so a later `changed` still
    /// reports the pending change.
    ///
    /// # Panics
    ///
    /// Panics if this future is first polled outside a runtime-managed thread.
    pub async fn changed(&mut self) -> Result<(), RecvError> {
        poll_fn(|cx| self.poll_changed(cx)).await
    }

    /// Borrows the current value without marking it observed.
    ///
    /// A later [`changed`](Self::changed) call can still complete immediately if
    /// this value is newer than the receiver's recorded version.
    ///
    /// # Examples
    ///
    /// ```
    /// let (sender, receiver) = runite::channel::watch::channel(1);
    /// sender.send(2).unwrap();
    /// assert_eq!(*receiver.borrow(), 2);
    /// ```
    pub fn borrow(&self) -> Ref<'_, T> {
        Ref {
            guard: self
                .shared
                .value
                .read()
                .expect("watch state should not be poisoned"),
        }
    }

    /// Borrows the current value and marks it observed.
    ///
    /// The returned [`Ref`] holds a read lock on the value until dropped.
    ///
    /// # Examples
    ///
    /// ```
    /// let (sender, mut receiver) = runite::channel::watch::channel(1);
    /// sender.send(2).unwrap();
    /// assert_eq!(*receiver.borrow_and_update(), 2);
    /// ```
    pub fn borrow_and_update(&mut self) -> Ref<'_, T> {
        // Read the value and the version under the same value read lock so the
        // recorded version matches the value being returned.
        let guard = self
            .shared
            .value
            .read()
            .expect("watch state should not be poisoned");
        self.version = self.shared.version.load(Ordering::Acquire);
        Ref { guard }
    }

    fn poll_changed(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), RecvError>> {
        if let Some(future) = self.wait.as_mut() {
            match Pin::new(future).poll(cx) {
                Poll::Ready(result) => {
                    self.wait.take();
                    match result {
                        Ok(version) if version > self.version => {
                            self.version = version;
                            Poll::Ready(Ok(()))
                        }
                        Ok(_) => {
                            // Stale completion: a waiter registered at an older
                            // version fired, but `self.version` has since caught
                            // up or passed it (e.g. via `borrow_and_update`).
                            // Accepting it would regress `self.version` and
                            // report a spurious change, so discard it and
                            // re-register instead of moving the version backward.
                            self.poll_changed(cx)
                        }
                        Err(_) => Poll::Ready(Err(RecvError)),
                    }
                }
                Poll::Pending => Poll::Pending,
            }
        } else {
            let (future, handle) = runtime_waiter::<Result<u64, RecvError>>();
            let immediate = {
                let mut book = self.shared.lock_book();
                // Read the version under the book lock: `send` bumps the version
                // (under the value lock) and then wakes waiters under this same
                // book lock, so checking the version and enqueueing here cannot
                // race with a send in a way that loses the wakeup.
                let current = self.shared.version.load(Ordering::Acquire);
                if current > self.version {
                    Some(Ok(current))
                } else if book.sender_count == 0 {
                    Some(Err(RecvError))
                } else {
                    let waiter_id = book.enqueue_waiter(self.version, handle.clone());
                    set_cancel_waiter(&handle, &self.shared.book, waiter_id);
                    None
                }
            };

            if let Some(result) = immediate {
                handle.complete(result);
            }

            self.wait = Some(future);
            self.poll_changed(cx)
        }
    }
}

impl<'a, T: Send + 'static> Deref for Ref<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.guard
    }
}

fn set_cancel_waiter(
    handle: &CompletionHandle<Result<u64, RecvError>>,
    book: &Arc<Mutex<Book>>,
    waiter_id: usize,
) {
    let cancel_book = Arc::clone(book);
    let cancel_handle = handle.clone();
    handle.set_cancel(move || {
        let mut book = cancel_book
            .lock()
            .expect("watch state should not be poisoned");
        book.remove_waiter(waiter_id);
        drop(book);
        cancel_handle.finish(None);
    });
}

impl<T: Send + 'static> Drop for Sender<T> {
    fn drop(&mut self) {
        let waiters = {
            let mut book = self.shared.lock_book();
            book.close_sender()
        };
        for waiter in waiters {
            waiter.complete(Err(RecvError));
        }
    }
}

impl<T: Send + 'static> Drop for Receiver<T> {
    fn drop(&mut self) {
        let mut book = self.shared.lock_book();
        book.receiver_count = book
            .receiver_count
            .checked_sub(1)
            .expect("receiver count underflow: more drops than creates");
    }
}

impl<T> fmt::Display for SendError<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "channel closed")
    }
}

impl<T: fmt::Debug> std::error::Error for SendError<T> {}

impl fmt::Display for RecvError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "channel closed")
    }
}

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

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use crate::{queue_macrotask, run, spawn};

    use super::{RecvError, channel};

    #[test]
    fn receiver_borrows_initial_value() {
        let (_sender, receiver) = channel(5usize);
        assert_eq!(*receiver.borrow(), 5);
    }

    #[test]
    fn two_borrows_on_same_thread_do_not_deadlock() {
        // Two live borrows on one thread must not deadlock: `Ref` holds a
        // shared read lock on the value slot, not an exclusive channel lock,
        // so any number of concurrent borrows coexist.
        let (sender, receiver) = channel(7usize);
        let a = receiver.borrow();
        let b = sender.borrow();
        let c = receiver.borrow();
        assert_eq!((*a, *b, *c), (7, 7, 7));
    }

    #[test]
    fn changed_fires_after_send() {
        let observed = Arc::new(Mutex::new(None::<usize>));
        let observed_for_task = Arc::clone(&observed);

        queue_macrotask(move || {
            let (sender, mut receiver) = channel(1usize);
            spawn(async move {
                sender.send(2).unwrap();
                receiver.changed().await.unwrap();
                *observed_for_task.lock().unwrap() = Some(*receiver.borrow());
            });
        });
        run();

        assert_eq!(*observed.lock().unwrap(), Some(2));
    }

    #[test]
    fn rapid_sends_coalesce_to_latest_value() {
        let observed = Arc::new(Mutex::new(None::<usize>));
        let observed_for_task = Arc::clone(&observed);

        queue_macrotask(move || {
            let (sender, mut receiver) = channel(0usize);
            spawn(async move {
                sender.send(1).unwrap();
                sender.send(2).unwrap();
                sender.send(3).unwrap();
                receiver.changed().await.unwrap();
                *observed_for_task.lock().unwrap() = Some(*receiver.borrow_and_update());
            });
        });
        run();

        assert_eq!(*observed.lock().unwrap(), Some(3));
    }

    #[test]
    fn changed_errors_after_all_senders_drop() {
        let observed = Arc::new(Mutex::new(None::<RecvError>));
        let observed_for_task = Arc::clone(&observed);

        queue_macrotask(move || {
            let (sender, mut receiver) = channel(0usize);
            spawn(async move {
                drop(sender);
                *observed_for_task.lock().unwrap() = Some(receiver.changed().await.unwrap_err());
            });
        });
        run();

        assert_eq!(*observed.lock().unwrap(), Some(RecvError));
    }

    #[test]
    fn send_modify_updates_value() {
        let observed = Arc::new(Mutex::new(None::<usize>));
        let observed_for_task = Arc::clone(&observed);

        queue_macrotask(move || {
            let (sender, mut receiver) = channel(1usize);
            spawn(async move {
                sender.send_modify(|value| *value += 41);
                receiver.changed().await.unwrap();
                *observed_for_task.lock().unwrap() = Some(*receiver.borrow());
            });
        });
        run();

        assert_eq!(*observed.lock().unwrap(), Some(42));
    }
}