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