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
// Copyright Christian Daley 2021
// Copyright Frank Mori Hess 2007-2008.
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//! # signals2
//!
//! `signals2` is a thread-safe signal/slot library inspired by the [boost::signals2](https://www.boost.org/doc/libs/1_76_0/doc/html/signals2.html)
//! C++ library. [Signals](Signal) are objects that contain a list of callback functions ("slots") to be executed when the signal is
//! "emitted". Signals and their corresponding slots can be managed through the use of [connections](Connection)
//! and [shared connection blocks](SharedConnectionBlock).
//!
//! `signals2` contains no unsafe code and compiles on stable Rust 1.53.
//!
//! `signals2` is distributed under the [Boost Software License, Version 1.0](https://www.boost.org/LICENSE_1_0.txt).
//!
//! ### Links
//! * [Github](https://github.com/christiandaley/signals2/)
//! * [Changelog](https://github.com/christiandaley/signals2/blob/master/CHANGES.md)
use ;
use ;
/// Defines the combiner trait and several simple combiners that can be used.
use ;
/// Defines different `emit` traits for signals.
pub use ;
/// Defines different `connect` traits for signals.
pub use ;
/// A handle to a signal with a slot function signature of `Args -> R`. `C` defines the combiner used
/// to generate a return value when `emit` is envoked. `G` defines the ordering of groups of slots. **Arguments given
/// to the signal must implement `Clone`. If you need to emit a signal with an argument that doesn't implement clone, that
/// argument should be wrapped in an `Arc<T>` (as an example) to make it cloneable.**
/// # Examples
/// ```
/// use signals2::*;
///
/// let sig: Signal<()> = Signal::new();
/// sig.connect(|| println!("Hello, world!"));
/// sig.emit(); // prints "Hello, world!"
/// ```
/// The only required template parameter for a `Signal` is the type of the parameters that the slot functions will
/// accept, represented as a tuple. If we want our signal to have slot functions that accept two `i32`s as parameters, the
/// template parameter will be `(i32, i32)`. Slot functions may accept 0-12 parameters.
/// ```
/// use signals2::*;
///
/// let sig: Signal<(i32, i32)> = Signal::new();
/// sig.connect(|x, y| println!("x + y = {}", x + y));
/// sig.emit(2, 3); // prints "x + y = 5"
/// ```
/// Special care must be taken when creating `Signals` with slots that accept only one parameter. The single parameter type
/// must still be represented as a tuple in the type signature of the `Signal`. The Rust compiler does not recognize `(T)` as a tuple-type
/// of arity one, rather it recognizes it as simply the type `T`. A comma must be used to force the Rust compiler to recognize it as a tuple, e.x. `(T,)`
/// ```
/// use signals2::*;
///
/// let sig: Signal<(i32,)> = Signal::new(); // Note that using Signal<(i32)> or Signal<i32> will not compile!
/// sig.connect(|x| println!("x = {}", x));
/// sig.emit(7); // prints "x = 7"
/// ```
/// Slot functions can have return values, and the return value of the entire `Signal` is determined by the [Combiner] type.
/// The default combiner simply returns an `Option<R>` with a value of `Some(x)` where `x` is the value returned by
/// the last slot executed, or `None` in the case that no slots were executed.
/// ```
/// use signals2::*;
///
/// let sig: Signal<(i32, i32), i32> = Signal::new();
/// assert_eq!(sig.emit(2, 3), None); // no slots have been added yet
/// sig.connect(|x, y| x + y);
/// assert_eq!(sig.emit(2, 3), Some(5));
/// ```
/// A weak reference to a signal's slots. Useful for allowing slots to maintain a persistant reference to their
/// owning signal without causing a memory leak.
/// # Example
/// ```
/// use signals2::*;
///
/// let sig: Signal<()> = Signal::new();
/// let weak_sig = sig.weak();
/// sig.connect(move || {
/// // if we had captured a cloned sig here it would cause a memory leak.
/// // Signals maintain strong references to their slot functions, so a slot function
/// // should not maintain a strong reference to its own signal or else a memory leak
/// // will occur.
/// weak_sig.upgrade().unwrap().connect(|| println!("Hello, world!"));
/// });
///
/// sig.emit(); // prints nothing
/// sig.emit(); // prints "Hello, world!" once
/// sig.emit(); // prints "Hello, world!" twice
/// // etc...
/// ```
/// A handle to a signal that allows new slots to be connected to the underlying signal.
/// Useful in cases where it is undesireable to allow unresitriced access to a signal while
/// still allowing new slots to be connected. Internally, a `ConnectHandle` uses a [WeakSignal].
/// If the underlying signal no longer exists, `connect` will return a connection that is in a
/// disconnected state.
/// # Example
/// ```
/// use signals2::*;
///
/// let sig: Signal<(), i32> = Signal::new();
/// let connect_handle = sig.get_connect_handle();
/// let conn = connect_handle.connect(|| 1);
/// assert!(conn.connected());
/// assert_eq!(sig.emit(), Some(1));
///
/// std::mem::drop(sig);
/// let conn = connect_handle.connect(|| 2);
/// assert!(!conn.connected());
/// ```
/// A handle to a signal that allows the signal to be emitted. Useful in cases where it is
/// undesireable to allow unresitriced access to a signal while still allowing the signal to be
/// emitted. Internally, an `EmitHandle` uses a [WeakSignal]. The result of calling `emit` on an
/// `EmitHandle` is an `Option<C::Output>` where `C` is the combiner type of the signal. If the
/// underlying signal no longer exists, `None` is returned.
/// # Example
/// ```
/// use signals2::*;
///
/// let sig: Signal<(), i32> = Signal::new();
/// let emit_handle = sig.get_emit_handle();
/// sig.connect(|| 1);
/// assert_eq!(emit_handle.emit(), Some(Some(1)));
///
/// std::mem::drop(sig);
/// assert_eq!(emit_handle.emit(), None);
/// ```