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
//! Async/await integration for hyphae.
//!
//! This module provides async Stream adapters for Cells, enabling use with
//! async runtimes like tokio, async-std, or smol.
//!
//! Enable with the `async` feature flag.
use futures_core::Stream;
use crate::{signal::Signal, subscription::SubscriptionGuard, traits::Watchable};
/// A Stream adapter for watching Cell values.
///
/// Created by [`AsyncWatchableExt::to_stream`].
pub struct CellStream<T: 'static> {
receiver: flume::r#async::RecvStream<'static, T>,
_guard: SubscriptionGuard,
}
impl<T: 'static> Stream for CellStream<T> {
type Item = T;
fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
std::pin::Pin::new(&mut self.receiver).poll_next(cx)
}
}
/// Extension trait for async operations on Watchable types.
pub trait AsyncWatchableExt<T>: Watchable<T> {
/// Convert this watchable into an async Stream.
///
/// The stream will emit the current value immediately, then emit
/// each subsequent value as it changes.
///
/// # Example
///
/// ```ignore
/// use hyphae::{Cell, Mutable, AsyncWatchableExt};
/// use futures::StreamExt;
///
/// async fn example() {
/// let cell = Cell::new(0);
/// let mut stream = cell.to_stream();
///
/// // First value is current
/// assert_eq!(stream.next().await, Some(0));
///
/// cell.set(1);
/// assert_eq!(stream.next().await, Some(1));
/// }
/// ```
fn to_stream(&self) -> CellStream<T>
where
T: Clone + Send + Sync + 'static,
Self: Clone + Send + Sync + 'static,
{
let (sender, receiver) = flume::unbounded();
let guard = self.subscribe(move |signal| {
if let Signal::Value(value) = signal {
let _ = sender.send((**value).clone());
}
});
CellStream {
receiver: receiver.into_stream(),
_guard: guard,
}
}
/// Convert this watchable into a bounded async Stream.
///
/// If the consumer is slower than the producer and the buffer fills,
/// the sender will block (applying backpressure to notify calls).
fn to_stream_bounded(&self, capacity: usize) -> CellStream<T>
where
T: Clone + Send + Sync + 'static,
Self: Clone + Send + Sync + 'static,
{
let (sender, receiver) = flume::bounded(capacity);
let guard = self.subscribe(move |signal| {
if let Signal::Value(value) = signal {
let _ = sender.send((**value).clone());
}
});
CellStream {
receiver: receiver.into_stream(),
_guard: guard,
}
}
}
impl<T, W: Watchable<T>> AsyncWatchableExt<T> for W {}
#[cfg(test)]
mod tests {
use std::{
pin::Pin,
task::{Context, Poll, Waker},
};
use futures_core::Stream;
use super::*;
use crate::{Cell, Mutable};
// Simple test waker that does nothing
fn noop_waker() -> Waker {
use std::{sync::Arc, task::Wake};
struct NoopWaker;
impl Wake for NoopWaker {
fn wake(self: Arc<Self>) {}
}
Arc::new(NoopWaker).into()
}
#[test]
fn test_to_stream_immediate_value() {
let cell = Cell::new(42);
let mut stream = cell.to_stream();
let waker = noop_waker();
let mut cx = Context::from_waker(&waker);
// First poll should return the initial value
match Pin::new(&mut stream).poll_next(&mut cx) {
Poll::Ready(Some(value)) => assert_eq!(value, 42),
_ => panic!("Expected Ready with initial value"),
}
}
#[test]
fn test_to_stream_updates() {
let cell = Cell::new(0);
let mut stream = cell.to_stream();
let waker = noop_waker();
let mut cx = Context::from_waker(&waker);
// Consume initial value
let _ = Pin::new(&mut stream).poll_next(&mut cx);
// Set new value
cell.set(1);
// Should get the new value
match Pin::new(&mut stream).poll_next(&mut cx) {
Poll::Ready(Some(value)) => assert_eq!(value, 1),
_ => panic!("Expected Ready with new value"),
}
}
#[test]
fn test_to_stream_bounded() {
let cell = Cell::new(0);
let mut stream = cell.to_stream_bounded(2);
let waker = noop_waker();
let mut cx = Context::from_waker(&waker);
// Consume initial value
let _ = Pin::new(&mut stream).poll_next(&mut cx);
// Set values
cell.set(1);
cell.set(2);
// Should get both values
match Pin::new(&mut stream).poll_next(&mut cx) {
Poll::Ready(Some(value)) => assert_eq!(value, 1),
_ => panic!("Expected Ready"),
}
match Pin::new(&mut stream).poll_next(&mut cx) {
Poll::Ready(Some(value)) => assert_eq!(value, 2),
_ => panic!("Expected Ready"),
}
}
}