dope-manifold 0.43.0

Owned application and service composition for dope
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use dope_core::driver::{
    result,
    route::{Identity, KeyTag, indices},
};
pub(in crate::client) mod claim;
pub(in crate::client) mod closing;
pub(in crate::client) mod drain;
pub mod engine;
pub(in crate::client) mod flow;
mod lane;
mod lifetime;
mod state;

use std::{marker, mem};

use claim::Urgency;
use closing::{Closing, retire};
use dope_core::driver::{
    route,
    schedule::{app::Work, clock, ready},
};
use dope_net::{
    link::{
        event, pool,
        slot::{transmit, types},
    },
    wire,
};
pub use lane::Lane;
pub use lifetime::Lifetime;
use o3::{
    base::num::bounded,
    depot::{self, index::Initialized},
};

use crate::{
    app::dispatch::identity,
    client::{
        app::Quiesced,
        attempt, close,
        session::{self, app},
    },
    service::bindings::{Bound, endpoint},
};

/// The native slot retains the exact application state and connection ownership.
pub(in crate::client) type Slot<'w, 'd, ID, W, C, O> =
    types::Connection<'w, 'd, ID, W, State<'d, C, O>>;

/// A live connection and its retained owner remain tied to their exact key.
pub(in crate::client) type Source<O, C, K> = depot::raw::Proof<((O, C), K), Initialized<()>>;

/// Driver- and route-branded connector identity.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Id<'d, ID: Identity>(pool::Key<'d, ID>);

impl<'d, ID: Identity> Id<'d, ID> {
    pub(in crate::client) fn from_key(key: pool::Key<'d, ID>) -> Self {
        Self(key)
    }

    pub const fn index(self) -> bounded::U32<0, { route::SLOT_MASK as u32 }> {
        self.0.index()
    }

    /// Projects this exact generation into its generation-independent lane.
    pub const fn lane(self) -> Lane<'d, ID> {
        Lane::new(self.0.lane())
    }

    pub(in crate::client) const fn key(self) -> pool::Key<'d, ID> {
        self.0
    }
}

impl<ID: Identity> identity::Identity for Id<'_, ID> {
    fn slot(self) -> indices::SlotIndex {
        self.0.lane()
    }
}

/// Move-only pre-connect proof. Exact-generation resources use
/// [`Self::connection`], while generation-free resources use [`Self::lane`].
#[must_use = "connection activation must be admitted or rejected"]
pub struct Activation<'a, 'd, ID: Identity> {
    attempt: &'a attempt::Completion<'a, 'd, ID>,
    connection: Id<'d, ID>,
    ready: ready::Target<'d>,
}

impl<'a, 'd, ID: Identity> Activation<'a, 'd, ID> {
    pub(in crate::client) const fn new(
        attempt: &'a attempt::Completion<'a, 'd, ID>,
        conn: Id<'d, ID>,
        ready: ready::Target<'d>,
    ) -> Self {
        Self {
            attempt,
            connection: conn,
            ready,
        }
    }

    /// Returns the exact connection attempt being activated.
    pub const fn attempt(&self) -> attempt::Id<'d, ID> {
        self.attempt.id()
    }

    pub const fn completion(&self) -> &attempt::Completion<'a, 'd, ID> {
        self.attempt
    }

    /// Returns the exact generation being activated.
    pub const fn connection(&self) -> Id<'d, ID> {
        self.connection
    }

    /// Returns the generation-independent physical lane.
    pub const fn lane(&self) -> Lane<'d, ID> {
        self.connection.lane()
    }

    /// Returns the driver-owned wake target for this activation.
    pub const fn ready(&self) -> ready::Target<'d> {
        self.ready
    }
}

/// Move-only proof that one exact connection generation and application drained.
/// Its invariant application type is independent of replaceable connection state.
#[must_use = "retired connection proof owns the terminal close cause"]
pub struct Retired<'d, ID: Identity, A> {
    app: Quiesced<'d, ID>,
    reason: close::Reason,
    transmit: Option<Transmit>,
    source: marker::PhantomData<fn(A) -> A>,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Transmit {
    Io(event::SendFailure<dope_core::Errno>),
    Rejected(event::SendFailure<result::SubmitError>),
    Invalid(transmit::Invalid),
}

impl<'d, ID: Identity, A> Retired<'d, ID, A> {
    const fn new(app: Quiesced<'d, ID>, reason: close::Reason, transmit: Option<Transmit>) -> Self {
        Self {
            app,
            reason,
            transmit,
            source: marker::PhantomData,
        }
    }

    pub const fn connection(&self) -> Id<'d, ID> {
        self.app.connection()
    }

    pub const fn reason(&self) -> close::Reason {
        self.reason
    }

    pub const fn transmit(&self) -> Option<&Transmit> {
        self.transmit.as_ref()
    }

    /// Moves the terminal diagnostic without consuming the retirement proof.
    pub(in crate::client) fn take_transmit(&mut self) -> Option<Transmit> {
        self.transmit.take()
    }
}

impl<'w, 'd: 'w, ID: Identity, N: session::Parser<'d, ID>, W: wire::Spec + 'd>
    Retired<'d, ID, app::State<'w, 'd, ID, N, W>>
{
    pub(in crate::client) fn into_session(self) -> Retired<'d, ID, N> {
        Retired {
            app: self.app,
            reason: self.reason,
            transmit: self.transmit,
            source: marker::PhantomData,
        }
    }
}

impl<'life, 'd: 'life, P: ?Sized, L, F, ID: Identity, const MAX: usize>
    Retired<'d, ID, Bound<'life, 'd, P, L, ID, MAX, F>>
{
    pub(crate) fn into_policy(self) -> Retired<'d, ID, endpoint::State<'life, 'd, P, L, ID, MAX>> {
        Retired {
            app: self.app,
            reason: self.reason,
            transmit: self.transmit,
            source: marker::PhantomData,
        }
    }
}

pub(crate) use state::State;

/// Callback-scoped proof to mutate exactly one branded connection's close
/// state. During retirement it carries a non-empty packed-state proof.
pub struct CloseRequest<'a, 'd, ID: Identity> {
    target: CloseTarget<'a>,
    brand: marker::PhantomData<fn(Id<'d, ID>) -> Id<'d, ID>>,
}

enum CloseTarget<'a> {
    Open(&'a mut Closing),
    Retiring(closing::RetireRef<'a>),
}

/// Exact progress of one connection's close machine.
///
/// The machine only exists once a close request has sealed its generation, so
/// [`CloseStatus::Open`] is expressed by the absence of any phase.
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ClosePhase {
    Withdrawing,
    Requested,
    Quarantining,
    Retiring,
    Settled,
    Finalized,
    Claimed,
}

/// Observable close progress of one connection generation.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum CloseStatus {
    Open,
    Closing(ClosePhase, close::Directive),
}

impl CloseStatus {
    pub const fn is_receiving(&self) -> bool {
        matches!(self, Self::Open | Self::Closing(ClosePhase::Withdrawing, _))
    }
}

impl<'a, 'd, ID: Identity> CloseRequest<'a, 'd, ID> {
    fn new(closing: &'a mut Closing) -> Self {
        Self {
            target: CloseTarget::Open(closing),
            brand: marker::PhantomData,
        }
    }

    pub(super) fn reborrow(&mut self) -> CloseRequest<'_, 'd, ID> {
        match &mut self.target {
            CloseTarget::Open(closing) => CloseRequest::new(closing),
            CloseTarget::Retiring(state) => CloseRequest {
                target: CloseTarget::Retiring(state.reborrow()),
                brand: marker::PhantomData,
            },
        }
    }

    pub fn request(&mut self, rule: close::Directive) {
        match &mut self.target {
            CloseTarget::Open(closing) => {
                closing.apply(rule);
            }
            CloseTarget::Retiring(state) => state.request(rule),
        }
    }

    pub const fn status(&self) -> CloseStatus {
        match &self.target {
            CloseTarget::Open(closing) => closing.status(),
            CloseTarget::Retiring(state) => state.status(),
        }
    }
}

#[repr(transparent)]
pub struct Ref<'a, 'd, ID: Identity, W: wire::Spec, C, O = ()> {
    active: state::ActiveRef<'a, 'd, ID, W, C, O>,
}

impl<'a, 'd, ID: Identity, W: wire::Spec, C, O> Ref<'a, 'd, ID, W, C, O> {
    pub(super) const fn new(active: state::ActiveRef<'a, 'd, ID, W, C, O>) -> Self {
        Self { active }
    }

    pub fn id(&self) -> Id<'d, ID> {
        self.active.id()
    }

    pub(in crate::client) const fn last_recv(&self) -> clock::Moment<'d> {
        self.active.last_recv()
    }

    pub fn state(&self) -> &C {
        self.active.conn()
    }
}

#[must_use = "established connection context owns the activation turn"]
pub struct Established<'a, 'd, ID: Identity, W: wire::Spec, C, O = ()> {
    active: state::ActiveMut<'a, 'd, ID, W, C, O>,
    expiry: Option<clock::Deadline<'d>>,
}

impl<'a, 'd, ID: Identity, W: wire::Spec, C, O> Established<'a, 'd, ID, W, C, O> {
    pub(super) const fn new(
        active: state::ActiveMut<'a, 'd, ID, W, C, O>,
        expiry: Option<clock::Deadline<'d>>,
    ) -> Self {
        Self { active, expiry }
    }

    pub fn id(&self) -> Id<'d, ID> {
        self.active.id()
    }

    pub fn wake_target(&self) -> ready::Target<'d> {
        self.active.ready()
    }

    pub(super) fn into_io(
        self,
    ) -> (
        Lifetime<'d, ID>,
        &'a mut C,
        CloseRequest<'a, 'd, ID>,
        ready::Target<'d>,
    ) {
        let lifetime = Lifetime::new(self.active.id(), self.expiry);
        let ready = self.active.ready();
        let (conn, closing) = self.active.into_parts();
        (lifetime, conn, CloseRequest::new(closing), ready)
    }
}

pub struct Ctx<'a, 'd, ID: Identity, W: wire::Spec, C, O = ()> {
    active: state::ActiveMut<'a, 'd, ID, W, C, O>,
    work: Work<'a, 'd>,
}

/// Borrows the exact connection and close request for an I/O failure callback.
pub struct Io<'a, 'd, ID: Identity, W: wire::Spec, C, O = ()> {
    id: Id<'d, ID>,
    conn: &'a mut C,
    closing: CloseRequest<'a, 'd, ID>,
    ready: ready::Target<'d>,
    spec: marker::PhantomData<fn(W, O)>,
}

impl<'a, 'd, ID: Identity, W: wire::Spec, C, O> Io<'a, 'd, ID, W, C, O> {
    pub const fn id(&self) -> Id<'d, ID> {
        self.id
    }

    pub(super) fn into_parts(
        self,
    ) -> (
        Id<'d, ID>,
        &'a mut C,
        CloseRequest<'a, 'd, ID>,
        ready::Target<'d>,
    ) {
        (self.id, self.conn, self.closing, self.ready)
    }
}

impl<'a, 'd, ID: Identity, W: wire::Spec, C, O> Ctx<'a, 'd, ID, W, C, O> {
    pub(super) const fn new(
        active: state::ActiveMut<'a, 'd, ID, W, C, O>,
        work: Work<'a, 'd>,
    ) -> Self {
        Self { active, work }
    }

    pub fn id(&self) -> Id<'d, ID> {
        self.active.id()
    }

    pub fn state(&self) -> &C {
        self.active.conn()
    }

    pub fn state_mut(&mut self) -> &mut C {
        self.active.conn_mut()
    }

    /// Borrows the first exact send failure retained by this connection.
    pub const fn transmit(&self) -> Option<&Transmit> {
        self.active.transmit()
    }

    /// Latches one indivisible close decision into this exact connection.
    pub fn request(&mut self, rule: close::Directive) {
        self.active.closing_mut().apply(rule);
    }

    /// Returns the exact application-turn capability carried by this callback.
    pub fn work(&self) -> Work<'a, 'd> {
        self.work
    }

    /// Returns the exact driver- and generation-branded readiness target.
    pub fn wake_target(&self) -> ready::Target<'d> {
        self.active.ready()
    }

    pub(super) fn into_parts(
        self,
    ) -> (
        Id<'d, ID>,
        &'a mut C,
        CloseRequest<'a, 'd, ID>,
        Work<'a, 'd>,
        ready::Target<'d>,
    ) {
        let id = self.active.id();
        let ready = self.active.ready();
        let (conn, closing) = self.active.into_parts();
        (id, conn, CloseRequest::new(closing), self.work, ready)
    }

    pub(super) fn into_retire(
        self,
    ) -> (
        Id<'d, ID>,
        &'a mut C,
        retire::Retire<'a, 'd, ID>,
        Work<'a, 'd>,
        ready::Target<'d>,
    ) {
        let id = self.active.id();
        let ready = self.active.ready();
        let (conn, closing, proof) = self.active.into_retire();
        (
            id,
            conn,
            retire::Retire::new(closing, proof),
            self.work,
            ready,
        )
    }

    pub(super) fn into_io(
        self,
    ) -> (
        Id<'d, ID>,
        &'a mut C,
        CloseRequest<'a, 'd, ID>,
        ready::Target<'d>,
    ) {
        let (id, conn, close, _, ready) = self.into_parts();
        (id, conn, close, ready)
    }
}

const _: () = assert!(mem::size_of::<Id<'static, KeyTag<0>>>() == mem::size_of::<route::Token>());
const _: () = assert!(mem::align_of::<Id<'static, KeyTag<0>>>() == mem::align_of::<route::Token>());
const _: () = assert!(
    mem::size_of::<Ctx<'static, 'static, KeyTag<0>, wire::Identity, ()>>()
        == 7 * mem::size_of::<usize>()
);
const _: () = {
    assert!(
        mem::size_of::<Io<'static, 'static, KeyTag<0>, wire::Identity, ()>>()
            == 7 * mem::size_of::<usize>()
    );
    assert!(
        mem::size_of::<Option<Io<'static, 'static, KeyTag<0>, wire::Identity, ()>>>()
            == 7 * mem::size_of::<usize>()
    );
};
const _: () = assert!(
    mem::size_of::<Established<'static, 'static, KeyTag<0>, wire::Identity, ()>>()
        + mem::size_of::<Id<'static, KeyTag<0>>>()
        + mem::size_of::<Work<'static, 'static>>()
        == mem::size_of::<Ctx<'static, 'static, KeyTag<0>, wire::Identity, ()>>()
            + mem::size_of::<Lifetime<'static, KeyTag<0>>>()
);
const _: () = assert!(
    mem::size_of::<Ref<'static, 'static, KeyTag<0>, wire::Identity, ()>>()
        == 3 * mem::size_of::<usize>()
);