13
13
// limitations under the License.
14
14
15
15
use std:: any:: Any ;
16
+ use std:: borrow:: Borrow ;
16
17
use std:: fmt:: Debug ;
17
18
use std:: hash:: Hash ;
18
19
use std:: time:: Duration ;
@@ -41,7 +42,7 @@ use crate::event_handler::{
41
42
} ;
42
43
use crate :: event_handler:: { GetAgentUri , HandlerAction , SideEffect } ;
43
44
use crate :: item:: {
44
- JoinLikeItem , MapLikeItem , MutableMapLikeItem , MutableValueLikeItem , TransformableMapLikeItem ,
45
+ InspectableMapLikeItem , JoinLikeItem , MapLikeItem , MutableMapLikeItem , MutableValueLikeItem ,
45
46
ValueLikeItem ,
46
47
} ;
47
48
use crate :: lanes:: command:: { CommandLane , DoCommand } ;
@@ -157,45 +158,86 @@ impl<Agent: 'static> HandlerContext<Agent> {
157
158
158
159
/// Create an event handler that will get the value of a value lane store of the agent.
159
160
///
160
- /// # Arguments
161
- /// * `lane ` - Projection to the value lane.
161
+ /// #Arguments
162
+ /// * `item ` - Projection to the value lane or store .
162
163
pub fn get_value < Item , T > (
163
164
& self ,
164
- lane : fn ( & Agent ) -> & Item ,
165
+ item : fn ( & Agent ) -> & Item ,
165
166
) -> impl HandlerAction < Agent , Completion = T > + Send + ' static
166
167
where
167
168
Item : ValueLikeItem < T > ,
168
169
T : Clone + Send + ' static ,
169
170
{
170
- Item :: get_handler :: < Agent > ( lane )
171
+ Item :: get_handler :: < Agent > ( item )
171
172
}
172
173
173
- /// Create an event handler that will set a new value into value lane or store of the agent.
174
+ /// Create an event handler that will set a new value into a value lane or store of the agent.
174
175
///
175
- /// # Arguments
176
- /// * `lane ` - Projection to the value lane.
176
+ /// #Arguments
177
+ /// * `item ` - Projection to the value lane or store .
177
178
/// * `value` - The value to set.
178
179
pub fn set_value < Item , T > (
179
180
& self ,
180
- lane : fn ( & Agent ) -> & Item ,
181
+ item : fn ( & Agent ) -> & Item ,
181
182
value : T ,
182
183
) -> impl HandlerAction < Agent , Completion = ( ) > + Send + ' static
183
184
where
184
185
Item : MutableValueLikeItem < T > ,
185
186
T : Send + ' static ,
186
187
{
187
- Item :: set_handler :: < Agent > ( lane, value)
188
+ Item :: set_handler :: < Agent > ( item, value)
189
+ }
190
+
191
+ /// Create an event handler that will transform the value of a value lane or store of the agent.
192
+ ///
193
+ /// #Arguments
194
+ /// * `item` - Projection to the value lane or store.
195
+ /// * `f` - A closure that produces a new value from a reference to the existing value.
196
+ pub fn transform_value < ' a , Item , T , F > (
197
+ & self ,
198
+ item : fn ( & Agent ) -> & Item ,
199
+ f : F ,
200
+ ) -> impl HandlerAction < Agent , Completion = ( ) > + Send + ' a
201
+ where
202
+ Agent : ' static ,
203
+ Item : ValueLikeItem < T > + MutableValueLikeItem < T > + ' static ,
204
+ T : ' static ,
205
+ F : FnOnce ( & T ) -> T + Send + ' a ,
206
+ {
207
+ Item :: with_value_handler :: < Item , Agent , F , T , T > ( item, f)
208
+ . and_then ( move |v| Item :: set_handler ( item, v) )
209
+ }
210
+
211
+ /// Create an event handler that will inspect the value of a value lane or store and generate a result from it.
212
+ /// This differs from using [`Self::get_value`] in that it does not require a clone to be made of the existing value.
213
+ ///
214
+ /// #Arguments
215
+ /// * `item` - Projection to the value lane or store.
216
+ /// * `f` - A closure that produces a value from a reference to the current value of the item.
217
+ pub fn with_value < ' a , Item , T , F , B , U > (
218
+ & self ,
219
+ item : fn ( & Agent ) -> & Item ,
220
+ f : F ,
221
+ ) -> impl HandlerAction < Agent , Completion = U > + Send + ' a
222
+ where
223
+ Agent : ' static ,
224
+ Item : ValueLikeItem < T > + ' static ,
225
+ T : Borrow < B > ,
226
+ B : ' static ,
227
+ F : FnOnce ( & B ) -> U + Send + ' a ,
228
+ {
229
+ Item :: with_value_handler :: < Item , Agent , F , B , U > ( item, f)
188
230
}
189
231
190
232
/// Create an event handler that will update an entry in a map lane or store of the agent.
191
233
///
192
- /// # Arguments
193
- /// * `lane ` - Projection to the map lane.
234
+ /// #Arguments
235
+ /// * `item ` - Projection to the map lane or store .
194
236
/// * `key - The key to update.
195
237
/// * `value` - The new value.
196
238
pub fn update < Item , K , V > (
197
239
& self ,
198
- lane : fn ( & Agent ) -> & Item ,
240
+ item : fn ( & Agent ) -> & Item ,
199
241
key : K ,
200
242
value : V ,
201
243
) -> impl HandlerAction < Agent , Completion = ( ) > + Send + ' static
@@ -204,73 +246,100 @@ impl<Agent: 'static> HandlerContext<Agent> {
204
246
K : Send + Clone + Eq + Hash + ' static ,
205
247
V : Send + ' static ,
206
248
{
207
- Item :: update_handler :: < Agent > ( lane , key, value)
249
+ Item :: update_handler :: < Agent > ( item , key, value)
208
250
}
209
251
210
- /// Create an event handler that will transform the value in an entry of a map lane or store of the agent.
252
+ /// Create an event handler that will inspect an entry in the map and produce a new value from it.
253
+ /// This differs from using [`Self::get_entry`] in that it does not require that a clone be made of the existing value.
211
254
///
212
- /// # Arguments
213
- /// * `lane ` - Projection to the map lane.
255
+ /// #Arguments
256
+ /// * `item ` - Projection to the map lane or store .
214
257
/// * `key - The key to update.
215
258
/// * `f` - A function to apple to the entry in the map.
216
- pub fn with_entry < ' a , Item , K , V , F > (
259
+ pub fn with_entry < ' a , Item , K , V , F , B , U > (
217
260
& self ,
218
- lane : fn ( & Agent ) -> & Item ,
261
+ item : fn ( & Agent ) -> & Item ,
262
+ key : K ,
263
+ f : F ,
264
+ ) -> impl HandlerAction < Agent , Completion = U > + Send + ' a
265
+ where
266
+ Agent : ' static ,
267
+ Item : InspectableMapLikeItem < K , V > + ' static ,
268
+ K : Send + Clone + Eq + Hash + ' static ,
269
+ V : Borrow < B > + ' static ,
270
+ B : ?Sized + ' static ,
271
+ F : FnOnce ( Option < & B > ) -> U + Send + ' a ,
272
+ {
273
+ Item :: with_entry_handler :: < Agent , F , B , U > ( item, key, f)
274
+ }
275
+
276
+ /// Create an event handler that will transform the value in an entry of a map lane or store of the agent.
277
+ /// If the map contains an entry with that key, it will be updated (or removed) based on the result of the calling
278
+ /// the closure on it. If the map does not contain an entry with that key, the closure will be called with [`None`]
279
+ /// and an entry will be inserted if it returns a value.
280
+ ///
281
+ /// #Arguments
282
+ /// * `item` - Projection to the map lane.
283
+ /// * `key - The key to update.
284
+ /// * `f` - A closure to apply to the entry in the map to produce the replacement.
285
+ pub fn transform_entry < ' a , Item , K , V , F > (
286
+ & self ,
287
+ item : fn ( & Agent ) -> & Item ,
219
288
key : K ,
220
289
f : F ,
221
290
) -> impl HandlerAction < Agent , Completion = ( ) > + Send + ' a
222
291
where
223
292
Agent : ' static ,
224
- Item : TransformableMapLikeItem < K , V > + ' static ,
293
+ Item : MutableMapLikeItem < K , V > + ' static ,
225
294
K : Send + Clone + Eq + Hash + ' static ,
226
- V : Clone + ' static ,
227
- F : FnOnce ( Option < V > ) -> Option < V > + Send + ' a ,
295
+ V : ' static ,
296
+ F : FnOnce ( Option < & V > ) -> Option < V > + Send + ' a ,
228
297
{
229
- Item :: with_handler :: < Agent , F > ( lane , key, f)
298
+ Item :: transform_entry_handler :: < Agent , F > ( item , key, f)
230
299
}
231
300
232
301
/// Create an event handler that will remove an entry from a map lane or store of the agent.
233
302
///
234
- /// # Arguments
235
- /// * `lane ` - Projection to the map lane.
303
+ /// #Arguments
304
+ /// * `item ` - Projection to the map lane or store .
236
305
/// * `key - The key to remove.
237
306
pub fn remove < Item , K , V > (
238
307
& self ,
239
- lane : fn ( & Agent ) -> & Item ,
308
+ item : fn ( & Agent ) -> & Item ,
240
309
key : K ,
241
310
) -> impl HandlerAction < Agent , Completion = ( ) > + Send + ' static
242
311
where
243
312
Item : MutableMapLikeItem < K , V > ,
244
313
K : Send + Clone + Eq + Hash + ' static ,
245
314
V : Send + ' static ,
246
315
{
247
- Item :: remove_handler :: < Agent > ( lane , key)
316
+ Item :: remove_handler :: < Agent > ( item , key)
248
317
}
249
318
250
319
/// Create an event handler that will clear a map lane or store of the agent.
251
320
///
252
- /// # Arguments
253
- /// * `lane ` - Projection to the map lane.
321
+ /// #Arguments
322
+ /// * `item ` - Projection to the map lane or store .
254
323
pub fn clear < Item , K , V > (
255
324
& self ,
256
- lane : fn ( & Agent ) -> & Item ,
325
+ item : fn ( & Agent ) -> & Item ,
257
326
) -> impl HandlerAction < Agent , Completion = ( ) > + Send + ' static
258
327
where
259
328
Item : MutableMapLikeItem < K , V > ,
260
329
K : Send + Clone + Eq + Hash + ' static ,
261
330
V : Send + ' static ,
262
331
{
263
- Item :: clear_handler :: < Agent > ( lane )
332
+ Item :: clear_handler :: < Agent > ( item )
264
333
}
265
334
266
335
/// Create an event handler that replaces the entire contents of a map lane or store.
267
336
///
268
- /// # Arguments
269
- /// * `lane ` - Projection to the map lane.
337
+ /// #Arguments
338
+ /// * `item ` - Projection to the map lane or store .
270
339
/// * `entries` - The new entries for the lane.
271
340
pub fn replace_map < Item , K , V , I > (
272
341
& self ,
273
- lane : fn ( & Agent ) -> & Item ,
342
+ item : fn ( & Agent ) -> & Item ,
274
343
entries : I ,
275
344
) -> impl HandlerAction < Agent , Completion = ( ) > + Send + ' static
276
345
where
@@ -283,44 +352,44 @@ impl<Agent: 'static> HandlerContext<Agent> {
283
352
let context = * self ;
284
353
let insertions = entries
285
354
. into_iter ( )
286
- . map ( move |( k, v) | context. update ( lane , k, v) ) ;
287
- self . clear ( lane ) . followed_by ( Sequentially :: new ( insertions) )
355
+ . map ( move |( k, v) | context. update ( item , k, v) ) ;
356
+ self . clear ( item ) . followed_by ( Sequentially :: new ( insertions) )
288
357
}
289
358
290
359
/// Create an event handler that will attempt to get an entry from a map-like item of the agent.
291
360
/// This includes map lanes and stores and join lanes.
292
361
///
293
- /// # Arguments
294
- /// * `lane ` - Projection to the map lane .
362
+ /// #Arguments
363
+ /// * `item ` - Projection to the map-like item .
295
364
/// * `key - The key to fetch.
296
365
pub fn get_entry < Item , K , V > (
297
366
& self ,
298
- lane : fn ( & Agent ) -> & Item ,
367
+ item : fn ( & Agent ) -> & Item ,
299
368
key : K ,
300
369
) -> impl HandlerAction < Agent , Completion = Option < V > > + Send + ' static
301
370
where
302
371
Item : MapLikeItem < K , V > ,
303
372
K : Send + Clone + Eq + Hash + ' static ,
304
373
V : Send + Clone + ' static ,
305
374
{
306
- Item :: get_handler :: < Agent > ( lane , key)
375
+ Item :: get_handler :: < Agent > ( item , key)
307
376
}
308
377
309
378
/// Create an event handler that will attempt to get the entire contents of a map-like item of the
310
379
/// agent. This includes map lanes and stores and join lanes.
311
380
///
312
- /// # Arguments
313
- /// * `lane ` - Projection to the map lane .
381
+ /// #Arguments
382
+ /// * `item ` - Projection to the map-like item .
314
383
pub fn get_map < Item , K , V > (
315
384
& self ,
316
- lane : fn ( & Agent ) -> & Item ,
385
+ item : fn ( & Agent ) -> & Item ,
317
386
) -> impl HandlerAction < Agent , Completion = HashMap < K , V > > + Send + ' static
318
387
where
319
388
Item : MapLikeItem < K , V > ,
320
389
K : Send + Clone + Eq + Hash + ' static ,
321
390
V : Send + Clone + ' static ,
322
391
{
323
- Item :: get_map_handler :: < Agent > ( lane )
392
+ Item :: get_map_handler :: < Agent > ( item )
324
393
}
325
394
326
395
/// Create an event handler that will send a command to a command lane of the agent.
0 commit comments