|
| 1 | +use std::{ |
| 2 | + ops::{Deref, DerefMut}, |
| 3 | + pin::Pin, |
| 4 | + task::{Context, Poll}, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::Waiter; |
| 8 | + |
| 9 | +/// A pollable computation backed by kio channels. |
| 10 | +/// |
| 11 | +/// Implementors write only [`Self::poll`], registering the [`Waiter`] with the |
| 12 | +/// channels they read. Wrap the value in [`Pending`] to get a real |
| 13 | +/// [`std::future::Future`]. |
| 14 | +/// |
| 15 | +/// This exists because a kio [`Waiter`] holds the strong `Arc<Waker>` while the |
| 16 | +/// channel's [`crate::WaiterList`] keeps only a `Weak`. A bare |
| 17 | +/// [`std::future::Future`] would have to stash the strong `Waiter` in a field and |
| 18 | +/// replace it every poll (or lose its wakeup); [`Pending`] does that once so each |
| 19 | +/// implementor doesn't have to. |
| 20 | +pub trait Future: Unpin { |
| 21 | + type Output; |
| 22 | + |
| 23 | + /// Poll for the output, registering `waiter` with the relevant channels if not |
| 24 | + /// yet ready. |
| 25 | + /// |
| 26 | + /// Takes `&self`: kio channels poll immutably, so a pollable can be driven |
| 27 | + /// through a shared borrow (e.g. while it lives inside an `&self`-borrowed enum). |
| 28 | + /// Carry any per-poll mutable state in a kio channel or a [`std::cell`] type. |
| 29 | + fn poll(&self, waiter: &Waiter) -> Poll<Self::Output>; |
| 30 | +} |
| 31 | + |
| 32 | +/// Adapts a kio [`Future`] into a [`std::future::Future`], retaining the strong |
| 33 | +/// [`Waiter`] between polls so its weak registration stays live. |
| 34 | +/// |
| 35 | +/// Derefs to the inner value, so any inherent methods you define on it are |
| 36 | +/// reachable through the pending handle (e.g. a non-blocking `poll`, or an |
| 37 | +/// `update`). |
| 38 | +pub struct Pending<F> { |
| 39 | + inner: F, |
| 40 | + // Retain the previous waiter so its Weak registration survives until the next |
| 41 | + // poll replaces it (see [`crate::WaiterList`]). |
| 42 | + waiter: Option<Waiter>, |
| 43 | +} |
| 44 | + |
| 45 | +impl<F> Pending<F> { |
| 46 | + /// Wrap a [`Future`] so it can be `.await`ed. |
| 47 | + pub fn new(inner: F) -> Self { |
| 48 | + Self { inner, waiter: None } |
| 49 | + } |
| 50 | + |
| 51 | + /// Consume the wrapper, returning the inner value. |
| 52 | + pub fn into_inner(self) -> F { |
| 53 | + self.inner |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<F> Deref for Pending<F> { |
| 58 | + type Target = F; |
| 59 | + |
| 60 | + fn deref(&self) -> &F { |
| 61 | + &self.inner |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<F> DerefMut for Pending<F> { |
| 66 | + fn deref_mut(&mut self) -> &mut F { |
| 67 | + &mut self.inner |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<F: Future> std::future::Future for Pending<F> { |
| 72 | + type Output = F::Output; |
| 73 | + |
| 74 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F::Output> { |
| 75 | + // Replacing drops the previous waiter, killing its Weak ref in the list so |
| 76 | + // the inner poll's register call can recycle the slot (see `WaiterList`). |
| 77 | + // `Pending<F>` is `Unpin` (F is, via the trait bound), so this deref is sound. |
| 78 | + let this = &mut *self; |
| 79 | + this.waiter = Some(Waiter::new(cx.waker().clone())); |
| 80 | + Future::poll(&this.inner, this.waiter.as_ref().unwrap()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod test { |
| 86 | + use super::*; |
| 87 | + use crate::Producer; |
| 88 | + |
| 89 | + /// A pollable that waits for the channel value to reach a threshold, with an |
| 90 | + /// inherent method reachable through `Pending`'s `DerefMut`. |
| 91 | + struct AtLeast { |
| 92 | + consumer: crate::Consumer<u64>, |
| 93 | + threshold: u64, |
| 94 | + } |
| 95 | + |
| 96 | + impl AtLeast { |
| 97 | + fn bump_threshold(&mut self) { |
| 98 | + self.threshold += 1; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + impl Future for AtLeast { |
| 103 | + type Output = u64; |
| 104 | + |
| 105 | + fn poll(&self, waiter: &Waiter) -> Poll<u64> { |
| 106 | + let threshold = self.threshold; |
| 107 | + match self.consumer.poll(waiter, |v| { |
| 108 | + let current = **v; |
| 109 | + if current >= threshold { |
| 110 | + Poll::Ready(current) |
| 111 | + } else { |
| 112 | + Poll::Pending |
| 113 | + } |
| 114 | + }) { |
| 115 | + Poll::Ready(Ok(v)) => Poll::Ready(v), |
| 116 | + _ => Poll::Pending, |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn pending_derefs_and_drives() { |
| 123 | + use std::task::Waker; |
| 124 | + |
| 125 | + let producer = Producer::new(0u64); |
| 126 | + let mut pending = Pending::new(AtLeast { |
| 127 | + consumer: producer.consume(), |
| 128 | + threshold: 5, |
| 129 | + }); |
| 130 | + |
| 131 | + // Inherent method on the inner reached via DerefMut. |
| 132 | + pending.bump_threshold(); // threshold now 6 |
| 133 | + |
| 134 | + // The kio-level poll (reached through Deref) is pending until the value catches up. |
| 135 | + assert!(Future::poll(&*pending, &Waiter::noop()).is_pending()); |
| 136 | + |
| 137 | + if let Ok(mut v) = producer.write() { |
| 138 | + *v = 6; |
| 139 | + } |
| 140 | + |
| 141 | + // The std Future resolves once the threshold is met. |
| 142 | + let mut cx = Context::from_waker(Waker::noop()); |
| 143 | + let mut pending = std::pin::pin!(pending); |
| 144 | + assert_eq!(std::future::Future::poll(pending.as_mut(), &mut cx), Poll::Ready(6)); |
| 145 | + } |
| 146 | +} |
0 commit comments