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
//! Pull-based stepping traits for widget event loops.
//!
//! Mogwai widgets are driven by a pull-based event loop: a caller awaits the
//! widget's next event, reacts to it, then awaits again. This module
//! formalizes that convention as four traits so the compiler — not prose —
//! enforces the contract and so generic composition (e.g. racing N children)
//! becomes possible.
//!
//! ## When to use which trait
//!
//! | Trait | Receiver | Use when |
//! |------|----------|----------|
//! | [`Step`] | `&self` | `step` only awaits event listeners (interior mutability). Lets a parent race multiple children concurrently. |
//! | [`StepMut`] | `&mut self` | `step` mutates the widget's own fields or drives a mutable resource. Children cannot be raced concurrently. |
//! | [`StepWith<T>`] | `&self` | A container of `T`-typed children that races a per-child future (supplied by a closure) against its own event. Return type is a GAT `Output<Ev>`. |
//! | [`StepWithMut<T>`] | `&mut self` | Same, but with exclusive access to each child. Return type is a GAT `Output<Ev>`. |
//!
//! ## Object safety
//!
//! These traits use `impl Future` returns (RPITIT) and are therefore **not**
//! object-safe. If a `dyn Step` need ever arises, add a boxed companion trait
//! with a blanket bridge as a non-breaking addition.
use ;
/// Pull-based event source — immutable borrow.
///
/// Implement this when `step` only awaits event listeners (which use interior
/// mutability). This lets a parent race multiple children's `step()` futures
/// concurrently without borrow conflicts.
///
/// ## Example
///
/// ```no_run
/// use mogwai::{prelude::*, step::Step};
///
/// struct Button<V: View> {
/// on_click: V::EventListener,
/// }
///
/// impl<V: View> Step for Button<V> {
/// type Output = V::Event;
/// fn step(&self) -> impl Future<Output = V::Event> {
/// self.on_click.next()
/// }
/// }
/// ```
/// Pull-based event source — exclusive borrow.
///
/// Implement this when `step` mutates the widget's own fields or drives a
/// mutable resource it owns. A parent cannot race two `StepMut` children
/// concurrently; use [`Step`] instead when concurrent racing is needed.
///
/// ## Example
///
/// ```no_run
/// use mogwai::{prelude::*, step::StepMut};
///
/// struct Counter {
/// count: u32,
/// on_click: <mogwai::web::Web as View>::EventListener,
/// }
///
/// impl StepMut for Counter {
/// type Output = ();
/// fn step_mut(&mut self) -> impl Future<Output = ()> {
/// async move {
/// let _ev = self.on_click.next().await;
/// self.count += 1;
/// }
/// }
/// }
/// ```
/// A container of `T`-typed children that races a per-child future (supplied by
/// a closure) against its own event future — immutable borrow.
///
/// This generalizes the `List::step` / `ButtonGroup::step` / `TabList::step`
/// pattern: the container owns `N` children of type `T`, and the caller
/// decides how each child produces a future of type `Ev`.
///
/// The return type is a generic associated type (GAT) `Output<Ev>` so a
/// container can produce a different event type depending on the child event
/// `Ev` (e.g. an enum with `Tabs(Self::TabEvent)` and `Panes(Ev)` variants).
///
/// ## Example
///
/// ```no_run
/// use mogwai::{
/// prelude::*,
/// step::{Step, StepWith},
/// };
/// use std::pin::Pin;
///
/// struct List<V: View, T> {
/// items: Vec<T>,
/// // ...
/// # _phantom: std::marker::PhantomData<V>,
/// }
///
/// impl<V: View, T> StepWith<T> for List<V, T> {
/// type Output<Ev: 'static> = ();
/// fn step_with<Ev>(
/// &self,
/// f: impl for<'a> FnMut(&'a T) -> Pin<Box<dyn Future<Output = Ev> + 'a>>,
/// ) -> impl Future<Output = Self::Output<Ev>>
/// where
/// Ev: 'static,
/// {
/// async move {
/// // race all children's futures...
/// # std::future::pending::<()>().await
/// }
/// }
/// }
/// ```