dope-core 0.30.1

Owned native I/O and completion driver for dope
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use std::{mem, pin};

use o3::{
    alloc,
    base::cell::region,
    buffer::{
        hold::limits,
        resident::{Budget, Reuse},
    },
};

use crate::{
    BranchError, driver,
    driver::{
        life,
        retained::flight,
        route::{App, lease, queues},
        schedule::{clock, clock::timer},
        settings,
        settings::{claim, errors},
    },
};

mod cleanup;
mod failure;
mod mapping;
pub mod path;
mod release;
mod removal;
pub mod resident;
mod scope;

pub use cleanup::Cleanup;
pub use failure::Failure;
pub use mapping::Mapping;
pub use release::Release;
pub use removal::Removal;
pub use scope::Scope;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Address(*mut core::ffi::c_void);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Bytes(usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Count(usize);

impl Address {
    pub(crate) const fn new(address: *mut core::ffi::c_void) -> Self {
        Self(address)
    }
}

impl Bytes {
    pub(crate) const fn new(bytes: usize) -> Self {
        Self(bytes)
    }
}

impl Count {
    pub(crate) const fn new(count: usize) -> Self {
        Self(count)
    }
}

/// Resident bytes requested for one driver-lifetime accounting domain.
#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(transparent)]
pub struct Resident(usize);

/// Failure to issue one resident domain from a bounded allocator.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Exhausted {
    Domains { capacity: usize },
    Retained { index: usize, cause: Reuse },
}

const _: () = assert!(size_of::<Exhausted>() == size_of::<usize>() * 4);

impl Resident {
    pub const fn bytes(bytes: usize) -> Self {
        Self(bytes)
    }

    pub const fn from_limit(limit: limits::Bound) -> Self {
        Self(limit.bytes())
    }

    const fn get(self) -> usize {
        self.0
    }
}

/// Projects one exact leaf from a generative storage build output.
pub trait Path<Root, Leaf>: Copy
where
    Root: Plan,
    Leaf: Plan,
{
    fn get<'life, 'p, 'd>(self, root: &'life Root::Output<'p, 'd>) -> &'life Leaf::Output<'p, 'd>
    where
        'd: 'p,
        'p: 'life,
        'd: 'life,
        Self: 'd,
        Root: 'd,
        Leaf: 'd;
}

/// Restricted construction context for driver-domain storage.
///
/// Exposes stable identities and route reservation without operation submission.
/// A build cannot publish into its output before the runtime pins that output.
///
#[repr(transparent)]
pub struct Context<'a, 'd>(pub(in crate::driver) &'a mut life::Local<'d>);

impl<'a, 'd> Context<'a, 'd> {
    pub(super) fn new(local: &'a mut life::Local<'d>) -> Self {
        Self(local)
    }

    pub fn driver(&self) -> driver::Reference<'d> {
        self.0.driver
    }

    /// Exact receive-pool geometry of this driver generation.
    pub fn receive(&self) -> settings::Receive {
        self.0.driver.receive().layout()
    }

    pub fn region(&self) -> &region::Token<region::Identity<'d>> {
        &self.0.region.0
    }

    pub fn state(&self) -> &region::Token<region::Identity<'d, 1>> {
        &self.0.region.1
    }

    pub fn timer(&self) -> timer::Timer<'d> {
        self.0.driver.timer()
    }

    pub fn clock(&self) -> clock::Clock<'_, 'd> {
        self.0.driver.scheduler().clock()
    }

    /// Copies the resident-domain authority for this exact driver generation.
    pub fn allocator(&self) -> resident::Alloc<'d> {
        self.0.resident
    }

    /// Claims a lifetime-branded resident accounting domain for storage owned
    /// by the build output.
    pub fn budget(&self, cap: Resident) -> Result<Budget<'d>, Exhausted> {
        self.0.resident.try_budget(cap.get())
    }

    /// Lends one affine construction step while retaining this outer authority.
    pub fn reborrow(&mut self) -> Context<'_, 'd> {
        Context(&mut *self.0)
    }

    /// Stages fixed submission groups without changing the live flight arena.
    pub fn try_flights<Tags, const GROUPS: usize>(
        self,
        need: flight::Need<Tags, GROUPS>,
    ) -> Result<flight::Prepared<'a, 'd, Tags, GROUPS>, alloc::Error> {
        let Self(local) = self;
        local.flights.try_prepare(need, local.driver)
    }

    pub fn try_reserve<B: App>(self) -> Result<lease::Claim<'d, B>, lease::Error> {
        let Self(local) = self;
        lease::Claim::try_new(local)
    }

    pub(in crate::driver) fn release(&mut self, retired: queues::Retired<'d>) {
        lease::Registry::release(self.0, retired);
    }
}

/// One generative storage output family.
///
/// A plan may borrow from its enclosing owner. Its output is available only
/// for driver generations which the consumed plan itself outlives.
pub trait Plan {
    type Output<'p, 'd>: 'p
    where
        Self: 'd,
        'd: 'p;
}

/// Pinned storage retaining its plan, backing loan, and driver generation.
#[repr(transparent)]
pub struct Loan<'a, 'p: 'a, 'd: 'p, S: Plan + 'd> {
    value: pin::Pin<&'a S::Output<'p, 'd>>,
}

impl<'a, 'p: 'a, 'd: 'p, S: Plan + 'd> Loan<'a, 'p, 'd, S> {
    pub(in crate::driver) fn new(value: pin::Pin<&'a S::Output<'p, 'd>>) -> Self {
        Self { value }
    }

    /// Preserves the source borrow when this temporary view is consumed.
    ///
    ///
    pub fn pinned(&self) -> pin::Pin<&'a S::Output<'p, 'd>> {
        self.value
    }
}

const _: () = {
    assert!(mem::size_of::<Loan<'static, 'static, 'static, ()>>() == mem::size_of::<usize>());
    assert!(mem::align_of::<Loan<'static, 'static, 'static, ()>>() == mem::align_of::<usize>());
};

/// Fallibly constructs driver-domain storage through the non-submitting [`Context`].
pub trait Build: Plan {
    type Error;

    type Store<'d>
    where
        Self: 'd;

    fn try_store<'d>(&self) -> Result<Self::Store<'d>, Self::Error>
    where
        Self: 'd;

    fn try_build<'p, 'd>(
        self,
        store: &'p mut Self::Store<'d>,
        ctx: Context<'_, 'd>,
    ) -> Result<Self::Output<'p, 'd>, Self::Error>
    where
        Self: 'd,
        'd: 'p;
}

/// Constructs driver-domain storage that owns all of its backing.
pub trait Create: Plan {
    type Error;

    fn try_build<'p, 'd>(self, ctx: Context<'_, 'd>) -> Result<Self::Output<'p, 'd>, Self::Error>
    where
        Self: 'd,
        'd: 'p;
}

/// Builds storage without contributing driver-resource claims.
pub trait Claimless: Plan {}

/// Constructs storage without introducing a recoverable failure path.
pub trait Direct: Plan {
    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
    where
        Self: 'd,
        'd: 'p;
}

#[repr(transparent)]
pub struct Value<T>(T);

impl<T> Value<T> {
    pub const fn new(value: T) -> Self {
        Self(value)
    }
}

impl<T> Plan for Value<T> {
    type Output<'p, 'd>
        = T
    where
        'd: 'p,
        Self: 'd;
}

impl<T> Claimless for Value<T> {}

impl<T> Direct for Value<T> {
    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
    where
        'd: 'p,
        Self: 'd,
    {
        self.0
    }
}

impl Plan for () {
    type Output<'p, 'd>
        = ()
    where
        'd: 'p,
        Self: 'd;
}

impl Claimless for () {}

impl Direct for () {
    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
    where
        'd: 'p,
        Self: 'd,
    {
        self
    }
}

impl<A: Plan, B: Plan> Plan for (A, B) {
    type Output<'p, 'd>
        = (A::Output<'p, 'd>, B::Output<'p, 'd>)
    where
        'd: 'p,
        Self: 'd;
}

impl<A: Build, B: Build> Build for (A, B) {
    type Error = BranchError<A::Error, B::Error>;
    type Store<'d>
        = (A::Store<'d>, B::Store<'d>)
    where
        Self: 'd;

    fn try_store<'d>(&self) -> Result<Self::Store<'d>, Self::Error>
    where
        Self: 'd,
    {
        let first = self.0.try_store().map_err(BranchError::First)?;
        let second = self.1.try_store().map_err(BranchError::Second)?;
        Ok((first, second))
    }

    fn try_build<'p, 'd>(
        self,
        store: &'p mut Self::Store<'d>,
        mut ctx: Context<'_, 'd>,
    ) -> Result<Self::Output<'p, 'd>, Self::Error>
    where
        'd: 'p,
        Self: 'd,
    {
        let (first, second) = self;
        let first = first
            .try_build(&mut store.0, ctx.reborrow())
            .map_err(BranchError::First)?;
        let second = second
            .try_build(&mut store.1, ctx)
            .map_err(BranchError::Second)?;
        Ok((first, second))
    }
}

impl<A: Claimless, B: Claimless> Claimless for (A, B) {}

impl<A: Direct, B: Direct> Direct for (A, B) {
    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
    where
        'd: 'p,
        Self: 'd,
    {
        let first = Direct::build(self.0);
        let second = Direct::build(self.1);
        (first, second)
    }
}

const _: () = {
    assert!(std::mem::size_of::<Context<'static, 'static>>() == std::mem::size_of::<usize>());
    assert!(std::mem::align_of::<Context<'static, 'static>>() == std::mem::align_of::<usize>());
    assert!(std::mem::size_of::<Value<usize>>() == std::mem::size_of::<usize>());
    assert!(std::mem::align_of::<Value<usize>>() == std::mem::align_of::<usize>());
    assert!(std::mem::needs_drop::<Value<String>>() == std::mem::needs_drop::<String>());
};

/// Builds one storage plan while carrying an adjacent owned value.
#[repr(transparent)]
pub struct With<F, T>((F, T));

impl<F, T> With<F, T> {
    pub const fn new(plan: F, value: T) -> Self {
        Self((plan, value))
    }
}

impl<F, T> settings::Resource for With<F, T>
where
    F: settings::Resource,
{
    fn include_in(&self, needs: claim::Needs) -> Result<claim::Needs, errors::Claim> {
        self.0.0.include_in(needs)
    }
}

impl<F, T> Plan for With<F, T>
where
    F: Plan,
{
    type Output<'p, 'd>
        = (F::Output<'p, 'd>, T)
    where
        'd: 'p,
        Self: 'd;
}

impl<F, T> Build for With<F, T>
where
    F: Build,
{
    type Error = F::Error;
    type Store<'d>
        = F::Store<'d>
    where
        Self: 'd;

    fn try_store<'d>(&self) -> Result<Self::Store<'d>, Self::Error>
    where
        Self: 'd,
    {
        self.0.0.try_store()
    }

    fn try_build<'p, 'd>(
        self,
        store: &'p mut Self::Store<'d>,
        ctx: Context<'_, 'd>,
    ) -> Result<Self::Output<'p, 'd>, Self::Error>
    where
        'd: 'p,
        Self: 'd,
    {
        let (plan, value) = self.0;
        Ok((plan.try_build(store, ctx)?, value))
    }
}

impl<F: Direct, T> Direct for With<F, T> {
    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
    where
        'd: 'p,
        Self: 'd,
    {
        let (plan, value) = self.0;
        (plan.build(), value)
    }
}

impl<F: Claimless, T> Claimless for With<F, T> {}

const _: () = {
    assert!(mem::size_of::<With<usize, u8>>() == mem::size_of::<(usize, u8)>());
    assert!(mem::align_of::<With<usize, u8>>() == mem::align_of::<(usize, u8)>());
    assert!(mem::needs_drop::<With<String, ()>>() == mem::needs_drop::<String>());
};