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
/*Copyright (c) 2022 Diego da Silva Lima. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT>.*/
use Error;
use ;
use Rc;
use RefCell;
use boxed;
/**
This is a minimal design pattern crate exposing the following:
: Transition trait.
Java-style OO inheritance: Inherit trait.
Modula-style late-binding/message-passing/signal-slot: Traits React and Message; types Callbacks<.> and ValuedCallbacks<.>, which
are containers for slots.
The crate uses only abstractions from the standard library and brings no transitive dependencies.
Its main motivation was centralizing a set of traits that I kept re-inventing in my crates. As such, the crate provides
minimal functionality, it just expose maximally generic types and traits useful for GUI code, to improve the software
engineering side of things. Hopefully the examples are enough to represent how they might be useful for GUI code. I
use the crate extensively to organize applications written in GTK, but the abstractions should be agnostic to GUI framework.
**/
/// Like Callbacks<A>, but defines both a set of arguments and a return type.
+ 'static>>>>);
/// Holds a set of callbacks with a given signature. Useful if several objects must respond to
/// the same signal. If more than one object must be taken as argument, use a custom struct or
/// tuples.
pub type Callbacks<A> = ;
// pub type Callbacks<A> = Rc<RefCell<Vec<boxed::Box<dyn Fn(A) + 'static>>>>;
pub type BindResult<T> = ;
/// Generic trait to represent interactions between Views (widgets or sets of grouped widgets affected by data change),
/// Models (data structures that encapsulate data-modifying algorithms) and controls (widgets
/// that modify the data contained in models). Widgets that affect a model (Controls) are represented by having the model imlement React<Widget>.
/// Widgets that are affected by a model (Views) are represented by having the widget implement React<Model>.
/// The implementation will usually bind one or more closures to the argument. Usually, an widget is either a control OR a view
/// with respect to a given model, but might a assume both roles. A widget might also be a view for one model but the control for another model. Models
/// usually encapsulate a call to glib::Receiver::attach(.), waiting for messages that change their state. The models are implemented
/// by closures activated on "signals", implemented using Rust enums. The actual data is not held in the model structure, but is owned
/// by a single closure executing on the main thread whenever some signal enum is received. If required, the model might spawn new
/// threads or wait for response from worker threads, but they should never block.
/// Stateful objects for any reason can live at an invalid state. This trait simply
/// declares which conditions an object might not be valid, associating an Error
/// type to your algorithm.
/// Attempts to initialize an object.
/// Attempts to perform an operation after which the object will not be used anymore.
/// Taking the object by value guarantees it won't be used anymore.
/// TODO move to verifiable crate, and make stateful dependent on it.
/// Trait implemented by stateful structs or enumerations for which
/// certain invariants must be held and can be verified at runtime.
/// You can #[derive(Verify)] if all fields of your structure satisfy
/// verify, in which case the error will be Box<dyn Error> wrapping the
/// first violation found.
/*pub struct SliceError<T, E> {
ix : usize,
err : E
}
impl<E> Verify for [T]
where
T : Verify<Error=E>
{
fn verify(&self) -> Result<(), SliceError<E> {
self.iter().map(|v| v.verify() )
}
}*/
/// Trait implemented by structs whose state can be characterized as having one of a few
/// discrete states at any given time. stateful structs or enumerations (usually enumerations)
/// that can be at one of a few states known at compile time. Some of the transitions
/// from state T->U might be invalid and must be specified at the implementation
/// of self.transition. TODO rename to transition?
/** Trait useful for Java-style inheritance. It allows T to implement
all of parent's methods if the parent's methods live in a trait with
a default implementation for T : Inherit<Self>. Note this trait only allows
for single-parent relationships (tree-like inheritance diagram). For multiple
inheritance (DAG-like inheritance diagram) Parent should be a trait type argument.
A way to simulate multiple inheritance is to use Type Parent = (A, B), then to use
the fields/methods of A use parent().0 and to use the fields/methods of B use parent().1 **/
/*/// Assumes an operation is executed within the specified time. Perhaps
/// Best implemented via a macro to be applied over the function
/// call.
pub trait Timed {
fn timed()
}*/