14
14
15
15
use std:: borrow:: Borrow ;
16
16
17
- use std:: collections:: HashMap ;
18
17
use std:: hash:: Hash ;
19
18
use std:: marker:: PhantomData ;
20
19
use swimos_api:: address:: Address ;
@@ -27,6 +26,7 @@ use crate::downlink_lifecycle::{
27
26
OnFailed , OnFailedShared , OnSynced , OnSyncedShared , StatefulMapLifecycle , StatelessMapLifecycle ,
28
27
} ;
29
28
use crate :: lifecycle_fn:: { WithHandlerContext , WithHandlerContextBorrow } ;
29
+ use crate :: map_storage:: MapOpsWithEntry ;
30
30
use crate :: {
31
31
agent_model:: downlink:: { MapDownlinkHandle , OpenMapDownlinkAction } ,
32
32
config:: MapDownlinkConfig ,
@@ -38,21 +38,24 @@ use crate::{
38
38
event_handler:: HandlerAction ,
39
39
} ;
40
40
41
+ type DlBuilderType < K , V , M , Context > = fn ( Context ) -> ( K , V , M ) ;
42
+
41
43
/// A builder for constructing a map downlink. Each lifecycle event handler is independent and, by
42
44
/// default, they all do nothing.
43
45
pub struct StatelessMapDownlinkBuilder <
44
46
Context ,
45
47
K ,
46
48
V ,
47
- LC = StatelessMapDownlinkLifecycle < Context , K , V > ,
49
+ M ,
50
+ LC = StatelessMapDownlinkLifecycle < Context , K , V , M > ,
48
51
> {
49
- _type : PhantomData < fn ( Context ) -> ( K , V ) > ,
52
+ _type : PhantomData < DlBuilderType < K , V , M , Context > > ,
50
53
address : Address < Text > ,
51
54
config : MapDownlinkConfig ,
52
55
inner : LC ,
53
56
}
54
57
55
- impl < Context , K , V > StatelessMapDownlinkBuilder < Context , K , V > {
58
+ impl < Context , K , V , M > StatelessMapDownlinkBuilder < Context , K , V , M > {
56
59
pub fn new ( address : Address < Text > , config : MapDownlinkConfig ) -> Self {
57
60
StatelessMapDownlinkBuilder {
58
61
_type : PhantomData ,
@@ -63,20 +66,20 @@ impl<Context, K, V> StatelessMapDownlinkBuilder<Context, K, V> {
63
66
}
64
67
}
65
68
66
- type StatefulBuilderVar < Context , K , V , State > = ( fn ( Context , State ) -> ( K , V ) , State ) ;
69
+ type StatefulBuilderVar < Context , K , V , M , State > = ( fn ( Context , State ) -> ( K , V , M ) , State ) ;
67
70
68
71
/// A builder for constructing a map downlink. The lifecycle event handlers share state and, by default,
69
72
/// they all do nothing.
70
- pub struct StatefulMapDownlinkBuilder < Context , K , V , State , LC > {
71
- _type : PhantomData < StatefulBuilderVar < Context , K , V , State > > ,
73
+ pub struct StatefulMapDownlinkBuilder < Context , K , V , M , State , LC > {
74
+ _type : PhantomData < StatefulBuilderVar < Context , K , V , M , State > > ,
72
75
address : Address < Text > ,
73
76
config : MapDownlinkConfig ,
74
77
inner : LC ,
75
78
}
76
79
77
- impl < Context , K , V , LC > StatelessMapDownlinkBuilder < Context , K , V , LC >
80
+ impl < Context , K , V , M , LC > StatelessMapDownlinkBuilder < Context , K , V , M , LC >
78
81
where
79
- LC : StatelessMapLifecycle < Context , K , V > ,
82
+ LC : StatelessMapLifecycle < Context , K , V , M > ,
80
83
{
81
84
/// Specify a handler for the `on_linked` event.
82
85
///
85
88
pub fn on_linked < F > (
86
89
self ,
87
90
handler : F ,
88
- ) -> StatelessMapDownlinkBuilder < Context , K , V , LC :: WithOnLinked < WithHandlerContext < F > > >
91
+ ) -> StatelessMapDownlinkBuilder < Context , K , V , M , LC :: WithOnLinked < WithHandlerContext < F > > >
89
92
where
90
93
WithHandlerContext < F > : OnLinked < Context > ,
91
94
{
@@ -110,9 +113,9 @@ where
110
113
pub fn on_synced < F > (
111
114
self ,
112
115
handler : F ,
113
- ) -> StatelessMapDownlinkBuilder < Context , K , V , LC :: WithOnSynced < WithHandlerContext < F > > >
116
+ ) -> StatelessMapDownlinkBuilder < Context , K , V , M , LC :: WithOnSynced < WithHandlerContext < F > > >
114
117
where
115
- WithHandlerContext < F > : OnSynced < HashMap < K , V > , Context > ,
118
+ WithHandlerContext < F > : OnSynced < M , Context > ,
116
119
{
117
120
let StatelessMapDownlinkBuilder {
118
121
address,
@@ -135,7 +138,7 @@ where
135
138
pub fn on_unlinked < F > (
136
139
self ,
137
140
handler : F ,
138
- ) -> StatelessMapDownlinkBuilder < Context , K , V , LC :: WithOnUnlinked < WithHandlerContext < F > > >
141
+ ) -> StatelessMapDownlinkBuilder < Context , K , V , M , LC :: WithOnUnlinked < WithHandlerContext < F > > >
139
142
where
140
143
WithHandlerContext < F > : OnUnlinked < Context > ,
141
144
{
@@ -160,7 +163,7 @@ where
160
163
pub fn on_failed < F > (
161
164
self ,
162
165
handler : F ,
163
- ) -> StatelessMapDownlinkBuilder < Context , K , V , LC :: WithOnFailed < WithHandlerContext < F > > >
166
+ ) -> StatelessMapDownlinkBuilder < Context , K , V , M , LC :: WithOnFailed < WithHandlerContext < F > > >
164
167
where
165
168
WithHandlerContext < F > : OnFailed < Context > ,
166
169
{
@@ -185,11 +188,17 @@ where
185
188
pub fn on_update < F , B > (
186
189
self ,
187
190
handler : F ,
188
- ) -> StatelessMapDownlinkBuilder < Context , K , V , LC :: WithOnUpdate < WithHandlerContextBorrow < F , B > > >
191
+ ) -> StatelessMapDownlinkBuilder <
192
+ Context ,
193
+ K ,
194
+ V ,
195
+ M ,
196
+ LC :: WithOnUpdate < WithHandlerContextBorrow < F , B > > ,
197
+ >
189
198
where
190
199
B : ?Sized ,
191
200
V : Borrow < B > ,
192
- WithHandlerContextBorrow < F , B > : OnDownlinkUpdate < K , V , Context > ,
201
+ WithHandlerContextBorrow < F , B > : OnDownlinkUpdate < K , V , M , Context > ,
193
202
{
194
203
let StatelessMapDownlinkBuilder {
195
204
address,
@@ -212,9 +221,9 @@ where
212
221
pub fn on_remove < F > (
213
222
self ,
214
223
handler : F ,
215
- ) -> StatelessMapDownlinkBuilder < Context , K , V , LC :: WithOnRemove < WithHandlerContext < F > > >
224
+ ) -> StatelessMapDownlinkBuilder < Context , K , V , M , LC :: WithOnRemove < WithHandlerContext < F > > >
216
225
where
217
- WithHandlerContext < F > : OnDownlinkRemove < K , V , Context > ,
226
+ WithHandlerContext < F > : OnDownlinkRemove < K , V , M , Context > ,
218
227
{
219
228
let StatelessMapDownlinkBuilder {
220
229
address,
@@ -237,9 +246,9 @@ where
237
246
pub fn on_clear < F > (
238
247
self ,
239
248
handler : F ,
240
- ) -> StatelessMapDownlinkBuilder < Context , K , V , LC :: WithOnClear < WithHandlerContext < F > > >
249
+ ) -> StatelessMapDownlinkBuilder < Context , K , V , M , LC :: WithOnClear < WithHandlerContext < F > > >
241
250
where
242
- WithHandlerContext < F > : OnDownlinkClear < K , V , Context > ,
251
+ WithHandlerContext < F > : OnDownlinkClear < M , Context > ,
243
252
{
244
253
let StatelessMapDownlinkBuilder {
245
254
address,
@@ -261,7 +270,7 @@ where
261
270
pub fn with_state < State : Send > (
262
271
self ,
263
272
state : State ,
264
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithShared < State > > {
273
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithShared < State > > {
265
274
let StatelessMapDownlinkBuilder {
266
275
address,
267
276
config,
@@ -277,14 +286,15 @@ where
277
286
}
278
287
}
279
288
280
- impl < Context , K , V , LC > StatelessMapDownlinkBuilder < Context , K , V , LC >
289
+ impl < Context , K , V , M , LC > StatelessMapDownlinkBuilder < Context , K , V , M , LC >
281
290
where
282
291
Context : AgentDescription + ' static ,
283
292
K : Form + Hash + Eq + Ord + Clone + Send + Sync + ' static ,
284
293
K :: Rec : Send ,
285
294
V : Form + Send + Sync + ' static ,
286
295
V :: Rec : Send ,
287
- LC : StatelessMapLifecycle < Context , K , V > + ' static ,
296
+ M : Default + MapOpsWithEntry < K , V , K > + Send + ' static ,
297
+ LC : StatelessMapLifecycle < Context , K , V , M > + ' static ,
288
298
{
289
299
/// Complete the downlink and create a [`HandlerAction`] that will open the downlink when it is
290
300
/// executed.
@@ -301,9 +311,9 @@ where
301
311
}
302
312
}
303
313
304
- impl < Context , K , V , State , LC > StatefulMapDownlinkBuilder < Context , K , V , State , LC >
314
+ impl < Context , K , V , M , State , LC > StatefulMapDownlinkBuilder < Context , K , V , M , State , LC >
305
315
where
306
- LC : StatefulMapLifecycle < Context , State , K , V > + ' static ,
316
+ LC : StatefulMapLifecycle < Context , State , K , V , M > + ' static ,
307
317
{
308
318
/// Specify a handler for the `on_linked` event.
309
319
///
@@ -312,7 +322,7 @@ where
312
322
pub fn on_linked < F > (
313
323
self ,
314
324
handler : F ,
315
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithOnLinked < FnHandler < F > > >
325
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithOnLinked < FnHandler < F > > >
316
326
where
317
327
FnHandler < F > : OnLinkedShared < Context , State > ,
318
328
{
@@ -337,9 +347,9 @@ where
337
347
pub fn on_synced < F > (
338
348
self ,
339
349
handler : F ,
340
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithOnSynced < FnHandler < F > > >
350
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithOnSynced < FnHandler < F > > >
341
351
where
342
- FnHandler < F > : OnSyncedShared < HashMap < K , V > , Context , State > ,
352
+ FnHandler < F > : OnSyncedShared < M , Context , State > ,
343
353
{
344
354
let StatefulMapDownlinkBuilder {
345
355
address,
@@ -362,7 +372,7 @@ where
362
372
pub fn on_unlinked < F > (
363
373
self ,
364
374
handler : F ,
365
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithOnUnlinked < FnHandler < F > > >
375
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithOnUnlinked < FnHandler < F > > >
366
376
where
367
377
FnHandler < F > : OnUnlinkedShared < Context , State > ,
368
378
{
@@ -387,7 +397,7 @@ where
387
397
pub fn on_failed < F > (
388
398
self ,
389
399
handler : F ,
390
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithOnFailed < FnHandler < F > > >
400
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithOnFailed < FnHandler < F > > >
391
401
where
392
402
FnHandler < F > : OnFailedShared < Context , State > ,
393
403
{
@@ -409,14 +419,15 @@ where
409
419
///
410
420
/// # Arguments
411
421
/// * `handler` - The event handler.
422
+ #[ allow( clippy:: type_complexity) ]
412
423
pub fn on_update < F , B > (
413
424
self ,
414
425
handler : F ,
415
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithOnUpdate < BorrowHandler < F , B > > >
426
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithOnUpdate < BorrowHandler < F , B > > >
416
427
where
417
428
B : ?Sized ,
418
429
V : Borrow < B > ,
419
- BorrowHandler < F , B > : OnDownlinkUpdateShared < K , V , Context , State > ,
430
+ BorrowHandler < F , B > : OnDownlinkUpdateShared < K , V , M , Context , State > ,
420
431
{
421
432
let StatefulMapDownlinkBuilder {
422
433
address,
@@ -439,9 +450,9 @@ where
439
450
pub fn on_remove < F > (
440
451
self ,
441
452
handler : F ,
442
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithOnRemove < FnHandler < F > > >
453
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithOnRemove < FnHandler < F > > >
443
454
where
444
- FnHandler < F > : OnDownlinkRemoveShared < K , V , Context , State > ,
455
+ FnHandler < F > : OnDownlinkRemoveShared < K , V , M , Context , State > ,
445
456
{
446
457
let StatefulMapDownlinkBuilder {
447
458
address,
@@ -464,9 +475,9 @@ where
464
475
pub fn on_clear < F > (
465
476
self ,
466
477
handler : F ,
467
- ) -> StatefulMapDownlinkBuilder < Context , K , V , State , LC :: WithOnClear < FnHandler < F > > >
478
+ ) -> StatefulMapDownlinkBuilder < Context , K , V , M , State , LC :: WithOnClear < FnHandler < F > > >
468
479
where
469
- FnHandler < F > : OnDownlinkClearShared < K , V , Context , State > ,
480
+ FnHandler < F > : OnDownlinkClearShared < M , Context , State > ,
470
481
{
471
482
let StatefulMapDownlinkBuilder {
472
483
address,
@@ -483,15 +494,16 @@ where
483
494
}
484
495
}
485
496
486
- impl < Context , K , V , State , LC > StatefulMapDownlinkBuilder < Context , K , V , State , LC >
497
+ impl < Context , K , V , M , State , LC > StatefulMapDownlinkBuilder < Context , K , V , M , State , LC >
487
498
where
488
499
Context : AgentDescription + ' static ,
489
500
K : Form + Hash + Eq + Ord + Clone + Send + Sync + ' static ,
490
501
K :: Rec : Send ,
491
502
V : Form + Send + Sync + ' static ,
492
503
V :: Rec : Send ,
504
+ M : Default + MapOpsWithEntry < K , V , K > + Send + ' static ,
493
505
State : Send + ' static ,
494
- LC : StatefulMapLifecycle < Context , State , K , V > + ' static ,
506
+ LC : StatefulMapLifecycle < Context , State , K , V , M > + ' static ,
495
507
{
496
508
/// Complete the downlink and create a [`HandlerAction`] that will open the downlink when it is
497
509
/// executed.
0 commit comments