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
//! Type-erasure related traits and impls.
use crate::;
use crate::;
// -------------------------------------------------------------------------------------------------
// Traits
/// Conversion from a specific listener type to a more general listener type,
/// such as a trait object.
///
/// This trait is typically used via calling [`Listen::listen()`], or an [`IntoListener`] bound
/// on a function’s parameter, to convert listeners of the provided type into a more common type
/// that can be stored in a [`Notifier`]’s listener set.
///
/// # Generic parameters
///
/// * `Self` is the listener type being converted to.
/// * `L` is the listener type being converted from.
/// * `M` is the type of message accepted by the listener.
///
/// # When to implement `FromListener`
///
/// There are two kinds of implementations of `FromListener`:
///
/// * Conversion to trait objects, like [`sync::DynListener`] and [`unsync::DynListener`], or enums.
/// You would write such an implementation if you are using a custom type for your type-erased
/// listeners (e.g. to add more required traits to allow more inspection of the listeners).
///
/// * Reflexive implementations, allowing the use of a [`Notifier`] that accepts only one type of
/// listener and does no dynamic dispatch.
/// You may write such an implementation, `impl FromListener<MyListener, MyMsg> for MyListener`,
/// whenever you implement `Listener`.
///
/// Unfortunately, we cannot provide a blanket implementation of this type,
/// `impl<L: Listener<M>, M> FromListener<L, M> for L`,
/// because it would conflict with the other kind of implementation.
///
/// # Example implementation
///
/// Suppose that you want to use *only* [`Flag`][crate::Flag] and
/// [`WakeFlag`][crate::future::WakeFlag] listeners,
/// perhaps because they use atomic operations and no custom code or locks.
/// A custom implementation of [`FromListener`] can offer this restriction, and
/// potential performance improvement by avoiding indirection and allocation:
///
/// ```
// skip if nosy::future::WakeFlag doesn't exist
/// use nosy::Listen as _;
///
/// /// A non-boxing type for all `Flag` listeners and no other kinds of listener.
/// #[derive(Debug)]
/// enum AnyFlag {
/// NoWake(nosy::FlagListener),
/// Wake(nosy::future::WakeFlagListener),
/// }
///
/// // Dispatches to the listeners in each variant.
/// impl<M> nosy::Listener<M> for AnyFlag {
/// fn receive(&self, messages: &[M]) -> bool {
/// match self {
/// Self::NoWake(l) => l.receive(messages),
/// Self::Wake(l) => l.receive(messages),
/// }
/// }
/// }
///
/// // Whenever possible, provide a reflexive, idempotent implementation,
/// // so that an already-converted listener can still be passed to `listen()` methods.
/// impl<M> nosy::FromListener<AnyFlag, M> for AnyFlag {
/// fn from_listener(listener: AnyFlag) -> AnyFlag {
/// listener
/// }
/// }
///
/// // The actual work of conversion.
/// impl<M> nosy::FromListener<nosy::FlagListener, M> for AnyFlag {
/// fn from_listener(listener: nosy::FlagListener) -> AnyFlag {
/// AnyFlag::NoWake(listener)
/// }
/// }
/// impl<M> nosy::FromListener<nosy::future::WakeFlagListener, M> for AnyFlag {
/// fn from_listener(listener: nosy::future::WakeFlagListener) -> AnyFlag {
/// AnyFlag::Wake(listener)
/// }
/// }
///
/// fn example_usage(notifier: &nosy::Notifier<(), AnyFlag>) {
/// notifier.listen(nosy::Flag::new(false).listener());
/// }
// skip if nosy::future::WakeFlag doesn't exist
/// ```
///
/// # Why not `From`?
///
/// The reason we define this trait instead of using the standard [`From`] trait is that [coherence]
/// would prohibit providing the needed blanket implementations — we cannot write
///
/// ```ignore
/// impl<L, M> From<L> for nosy::unsync::DynListener<M>
/// where
/// L: nosy::Listener<M>,
/// # {
/// # fn from(value: L) -> Self { todo!();}
/// # }
/// ```
///
/// because `L` is an uncovered type parameter and [`DynListener`][crate::unsync::DynListener]
/// is not a local type, and
/// even if that were not the case, this implementation would conflict with
/// the blanket implementation `impl<T> From<T> for T`.
///
/// [coherence]: https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence
/// Conversion from a concrete listener type to (normally) some flavor of boxed trait object.
///
/// This trait is equivalent to [`FromListener`] but with the source and destination swapped.
/// It allows you to write concise bounds like
/// `fn my_listen(listener: impl IntoListener<DynListener<M>, M>)`.
/// Additionally, `IntoListener` bounds are [eligible for elaboration] whereas the corresponding
/// [`FromListener`] bound would not be, and thus would require an additional `L: Listener<M>`
/// bound on every function or impl.
///
/// This trait may not be implemented. Implement [`FromListener`] instead.
///
/// # Generic parameters
///
/// * `Self` is the listener type being converted from.
/// * `L` is the listener type being converted to.
/// * `M` is the type of message accepted by the listener.
///
/// # Example
///
/// [`IntoListener`] is used when writing functions that take listeners as parameters,
/// similar to [`Listen::listen()`]:
///
/// ```
/// use nosy::{Listen, unsync::DynListener};
///
/// struct MyCell {
/// value: core::cell::Cell<usize>,
/// notifier: nosy::Notifier<(), DynListener<()>>,
/// }
///
/// impl MyCell {
/// pub fn get_and_listen(
/// &self,
/// listener: impl nosy::IntoListener<DynListener<()>, ()>,
/// ) -> usize {
/// self.notifier.listen(listener);
/// self.value.get()
/// }
/// }
/// ```
///
/// [eligible for elaboration]: https://github.com/rust-lang/rust/issues/20671
// -------------------------------------------------------------------------------------------------
// Implementations