hibana 0.8.0

Const-projected Affine Multiparty Session Types for choreography-first Rust protocols
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
mod cache_slots;

#[cfg(not(test))]
use super::super::frontier::{
    GlobalFrontierObservedState, frontier_global_observed_state_ptr_from_storage,
};
use super::{
    ActiveEntrySet, CursorEndpoint, EndpointSlot, EpochTable, FrontierKind,
    FrontierObservationDomain, FrontierObservationKey, FrontierScratchLayout, LabelUniverse,
    MintConfigMarker, ObservedEntrySet, OfferEntryObservedState, OfferEntryState, Port, ScopeId,
    Transport, cached_offer_entry_observed_state, checked_state_index,
    frontier_cached_observation_key_view_from_storage,
    frontier_global_active_entries_view_from_storage, frontier_observed_entries_view_from_storage,
    lane_port, state_index_to_usize,
};
impl<'r, const ROLE: u8, T, U, C, E, const MAX_RV: usize, Mint, B>
    CursorEndpoint<'r, ROLE, T, U, C, E, MAX_RV, Mint, B>
where
    T: Transport + 'r,
    U: LabelUniverse,
    C: crate::runtime::config::Clock,
    E: EpochTable,
    Mint: MintConfigMarker,
    B: EndpointSlot,
{
    #[inline]
    pub(in crate::endpoint::kernel) fn global_frontier_scratch_parts(
        &self,
    ) -> (*mut [u8], FrontierScratchLayout, usize) {
        let port = self.port_for_lane(self.primary_lane);
        (
            lane_port::frontier_scratch_ptr(port),
            self.cursor.frontier_scratch_layout(),
            self.cursor.max_frontier_entries(),
        )
    }

    #[cfg(not(test))]
    #[inline]
    pub(in crate::endpoint::kernel) fn global_frontier_observed_state(
        &self,
    ) -> GlobalFrontierObservedState {
        if !self.frontier_state.global_frontier_scratch_initialized {
            return GlobalFrontierObservedState::EMPTY;
        }
        let (scratch_ptr, layout, _) = self.global_frontier_scratch_parts();
        /* SAFETY: frontier observation storage is carved from the endpoint scratch layout at checked aligned offsets. */
        unsafe { *frontier_global_observed_state_ptr_from_storage(scratch_ptr, layout) }
    }

    #[cfg(not(test))]
    #[inline]
    pub(in crate::endpoint::kernel) fn global_frontier_observed_state_mut(
        &mut self,
    ) -> &mut GlobalFrontierObservedState {
        self.init_global_frontier_scratch_if_needed();
        let (scratch_ptr, layout, _) = self.global_frontier_scratch_parts();
        /* SAFETY: the pointer comes from pinned owner storage and this path holds unique mutable access for the borrow. */
        unsafe { &mut *frontier_global_observed_state_ptr_from_storage(scratch_ptr, layout) }
    }

    #[inline]
    pub(in crate::endpoint::kernel) fn init_global_frontier_scratch_if_needed(&mut self) {
        if self.frontier_state.global_frontier_scratch_initialized {
            return;
        }
        let (scratch_ptr, layout, frontier_entry_capacity) = self.global_frontier_scratch_parts();
        let mut active_entries = frontier_global_active_entries_view_from_storage(
            scratch_ptr,
            layout,
            frontier_entry_capacity,
        );
        active_entries.clear();
        let mut cached_key = frontier_cached_observation_key_view_from_storage(
            scratch_ptr,
            layout,
            frontier_entry_capacity,
        );
        cached_key.clear();
        #[cfg(test)]
        {
            self.frontier_state.global_frontier_observed.clear();
        }
        #[cfg(not(test))]
        /* SAFETY: initialization owns exclusive writable storage for this field and writes it exactly once before exposure. */
        unsafe {
            frontier_global_observed_state_ptr_from_storage(scratch_ptr, layout)
                .write(GlobalFrontierObservedState::EMPTY);
        }
        self.frontier_state.global_frontier_scratch_initialized = true;
    }

    #[inline]
    pub(in crate::endpoint::kernel) fn global_active_entries(&self) -> ActiveEntrySet {
        if !self.frontier_state.global_frontier_scratch_initialized {
            return ActiveEntrySet::EMPTY;
        }
        let (scratch_ptr, layout, frontier_entry_capacity) = self.global_frontier_scratch_parts();
        frontier_global_active_entries_view_from_storage(
            scratch_ptr,
            layout,
            frontier_entry_capacity,
        )
    }

    #[inline]
    pub(in crate::endpoint::kernel) fn cached_global_frontier_observation_key(
        &self,
    ) -> FrontierObservationKey {
        if !self.frontier_state.global_frontier_scratch_initialized {
            return FrontierObservationKey::EMPTY;
        }
        let (scratch_ptr, layout, frontier_entry_capacity) = self.global_frontier_scratch_parts();
        let key = frontier_cached_observation_key_view_from_storage(
            scratch_ptr,
            layout,
            frontier_entry_capacity,
        );
        key
    }

    #[inline]
    pub(in crate::endpoint::kernel) fn empty_observed_entries_scratch(
        &mut self,
    ) -> ObservedEntrySet {
        let port = self.port_for_lane(self.primary_lane);
        let scratch_ptr = lane_port::frontier_scratch_ptr(port);
        let layout = self.cursor.frontier_scratch_layout();
        let mut observed = frontier_observed_entries_view_from_storage(
            scratch_ptr,
            layout,
            self.cursor.max_frontier_entries(),
        );
        observed.clear();
        observed
    }

    #[inline]
    pub(in crate::endpoint::kernel) fn cached_frontier_observed_entries(
        &self,
        domain: FrontierObservationDomain,
        key: FrontierObservationKey,
    ) -> Option<ObservedEntrySet> {
        self.cached_frontier_observed_entries_ref(domain, &key)
    }

    #[inline]
    pub(super) fn cached_frontier_observed_entries_ref(
        &self,
        domain: FrontierObservationDomain,
        key: &FrontierObservationKey,
    ) -> Option<ObservedEntrySet> {
        #[cfg(test)]
        {
            return self.frontier_state.cached_frontier_observed_entries(
                domain,
                self.cached_global_frontier_observation_key(),
                key,
            );
        }
        #[cfg(not(test))]
        {
            if domain.uses_root_entries() {
                let slot_idx = self
                    .frontier_state
                    .root_frontier_slot(domain.root_scope())?;
                let slot = self.frontier_state.root_frontier_state[slot_idx];
                let observed_key = self
                    .frontier_state
                    .root_frontier_state
                    .observed_key(slot_idx);
                if observed_key != *key || slot.observed_entries.dynamic_controller_mask != 0 {
                    return None;
                }
                return Some(observed_key.observed_entries(slot.observed_entries));
            }
            let cached_key = self.cached_global_frontier_observation_key();
            let global = self.global_frontier_observed_state();
            if cached_key != *key || global.summary.dynamic_controller_mask != 0 {
                return None;
            }
            Some(cached_key.observed_entries(global.summary))
        }
    }

    #[inline]
    pub(in crate::endpoint::kernel) fn frontier_observation_cache_snapshot(
        &self,
        domain: FrontierObservationDomain,
    ) -> (FrontierObservationKey, ObservedEntrySet) {
        #[cfg(test)]
        {
            return self
                .frontier_state
                .frontier_observation_cache(domain, self.cached_global_frontier_observation_key());
        }
        #[cfg(not(test))]
        {
            if domain.uses_root_entries() {
                let Some(slot_idx) = self.frontier_state.root_frontier_slot(domain.root_scope())
                else {
                    return (FrontierObservationKey::EMPTY, ObservedEntrySet::EMPTY);
                };
                let row = self.frontier_state.root_frontier_state[slot_idx];
                let observed_key = self
                    .frontier_state
                    .root_frontier_state
                    .observed_key(slot_idx);
                return (
                    observed_key,
                    observed_key.observed_entries(row.observed_entries),
                );
            }
            let cached_key = self.cached_global_frontier_observation_key();
            let global = self.global_frontier_observed_state();
            (cached_key, cached_key.observed_entries(global.summary))
        }
    }

    #[inline]
    pub(in crate::endpoint::kernel) fn write_frontier_observation_snapshot(
        &mut self,
        domain: FrontierObservationDomain,
        key: FrontierObservationKey,
        observed_entries: ObservedEntrySet,
    ) {
        #[cfg(test)]
        {
            if !domain.uses_root_entries() {
                self.init_global_frontier_scratch_if_needed();
            }
            let cached_global_key = self.cached_global_frontier_observation_key();
            self.frontier_state.store_frontier_observation(
                domain,
                cached_global_key,
                key,
                observed_entries,
            );
        }
        #[cfg(not(test))]
        {
            if domain.uses_root_entries() {
                let Some(slot_idx) = self.frontier_state.root_frontier_slot(domain.root_scope())
                else {
                    return;
                };
                self.frontier_state
                    .root_frontier_state
                    .replace_root_observed_key(slot_idx, key);
                let slot = &mut self.frontier_state.root_frontier_state[slot_idx];
                slot.observed_entries = observed_entries.summary();
                return;
            }
            self.init_global_frontier_scratch_if_needed();
            let (scratch_ptr, layout, frontier_entry_capacity) =
                self.global_frontier_scratch_parts();
            let mut cached_key = frontier_cached_observation_key_view_from_storage(
                scratch_ptr,
                layout,
                frontier_entry_capacity,
            );
            cached_key.copy_from(key);
            let global = self.global_frontier_observed_state_mut();
            global.summary = observed_entries.summary();
        }
    }

    pub(in crate::endpoint::kernel) fn replace_offer_entry_observation_with_frontier_mask(
        &self,
        observed_entries: &mut ObservedEntrySet,
        entry_idx: usize,
        observed: OfferEntryObservedState,
    ) -> bool {
        let Some(frontier_mask) = self.offer_entry_frontier_mask_for_entry(entry_idx) else {
            return false;
        };
        observed_entries.replace_observation_with_frontier_mask(entry_idx, observed, frontier_mask)
    }

    pub(in crate::endpoint::kernel) fn recompute_offer_entry_observation_with_frontier_mask(
        &mut self,
        observed_entries: &mut ObservedEntrySet,
        entry_idx: usize,
    ) -> bool {
        let Some(observed) = self.recompute_offer_entry_observed_state_non_consuming(entry_idx)
        else {
            return false;
        };
        self.replace_offer_entry_observation_with_frontier_mask(
            observed_entries,
            entry_idx,
            observed,
        )
    }

    pub(in crate::endpoint::kernel) fn cached_entry_slot_move(
        active_entries: ActiveEntrySet,
        cached_key: FrontierObservationKey,
        entry_idx: usize,
    ) -> Option<(usize, usize)> {
        let new_slot_idx = active_entries.slot_for_entry(entry_idx)?;
        let len = active_entries.len();
        let entry = checked_state_index(entry_idx)?;
        let mut old_slot_idx = 0usize;
        while old_slot_idx < len {
            if cached_key.entry_state(old_slot_idx) == entry {
                break;
            }
            old_slot_idx += 1;
        }
        if old_slot_idx >= len || old_slot_idx == new_slot_idx {
            return None;
        }
        let mut slot_idx = 0usize;
        while slot_idx < len {
            let shifted = if slot_idx == new_slot_idx {
                cached_key.entry_state(old_slot_idx)
            } else if old_slot_idx < new_slot_idx
                && slot_idx >= old_slot_idx
                && slot_idx < new_slot_idx
            {
                cached_key.entry_state(slot_idx + 1)
            } else if old_slot_idx > new_slot_idx
                && slot_idx > new_slot_idx
                && slot_idx <= old_slot_idx
            {
                cached_key.entry_state(slot_idx - 1)
            } else {
                cached_key.entry_state(slot_idx)
            };
            if active_entries.entry_state(slot_idx) != shifted {
                return None;
            }
            slot_idx += 1;
        }
        Some((old_slot_idx, new_slot_idx))
    }

    pub(in crate::endpoint::kernel) fn cached_entry_slot_insert(
        active_entries: ActiveEntrySet,
        cached_key: FrontierObservationKey,
        entry_idx: usize,
    ) -> Option<usize> {
        let insert_slot_idx = active_entries.slot_for_entry(entry_idx)?;
        let len = active_entries.len();
        if len == 0 {
            return None;
        }
        let cached_len = len - 1;
        let entry = checked_state_index(entry_idx)?;
        let mut slot_idx = 0usize;
        while slot_idx < cached_len {
            if cached_key.entry_state(slot_idx) == entry {
                return None;
            }
            slot_idx += 1;
        }
        let mut active_slot_idx = 0usize;
        while active_slot_idx < len {
            let inserted = if active_slot_idx == insert_slot_idx {
                entry
            } else if active_slot_idx < insert_slot_idx {
                cached_key.entry_state(active_slot_idx)
            } else {
                cached_key.entry_state(active_slot_idx - 1)
            };
            if active_entries.entry_state(active_slot_idx) != inserted {
                return None;
            }
            active_slot_idx += 1;
        }
        Some(insert_slot_idx)
    }

    pub(in crate::endpoint::kernel) fn cached_entry_slot_remove(
        active_entries: ActiveEntrySet,
        cached_key: FrontierObservationKey,
        entry_idx: usize,
    ) -> Option<usize> {
        let len = active_entries.len();
        let cached_len = len + 1;
        let entry = checked_state_index(entry_idx)?;
        let mut removed_slot_idx = 0usize;
        while removed_slot_idx < cached_len {
            if cached_key.entry_state(removed_slot_idx) == entry {
                break;
            }
            removed_slot_idx += 1;
        }
        if removed_slot_idx >= cached_len {
            return None;
        }
        let mut active_slot_idx = 0usize;
        while active_slot_idx < len {
            let removed = if active_slot_idx < removed_slot_idx {
                cached_key.entry_state(active_slot_idx)
            } else {
                cached_key.entry_state(active_slot_idx + 1)
            };
            if active_entries.entry_state(active_slot_idx) != removed {
                return None;
            }
            active_slot_idx += 1;
        }
        Some(removed_slot_idx)
    }

    pub(in crate::endpoint::kernel) fn cached_entry_slot_replace(
        active_entries: ActiveEntrySet,
        cached_key: FrontierObservationKey,
        entry_idx: usize,
    ) -> Option<(usize, usize, usize)> {
        let len = active_entries.len();
        if len == 0 {
            return None;
        }
        let entry = checked_state_index(entry_idx)?;
        let mut replaced_slot_idx = None;
        let mut slot_idx = 0usize;
        while slot_idx < len {
            let cached_entry = cached_key.entry_state(slot_idx);
            let active_entry = active_entries.entry_state(slot_idx);
            if cached_entry != active_entry {
                if replaced_slot_idx.is_some() {
                    return None;
                }
                if cached_entry != entry && active_entry != entry {
                    return None;
                }
                replaced_slot_idx = Some(slot_idx);
            }
            slot_idx += 1;
        }
        let slot_idx = replaced_slot_idx?;
        let old_entry_idx = state_index_to_usize(cached_key.entry_state(slot_idx));
        let new_entry_idx = state_index_to_usize(active_entries.entry_state(slot_idx));
        Some((slot_idx, old_entry_idx, new_entry_idx))
    }
}