Skip to main content

dope_core/executor/raw/
app.rs

1use std::{ops, pin};
2
3use crate::driver::{self, life, schedule, settings};
4
5use crate::executor;
6
7/// Application storage metadata independent of lifecycle eligibility.
8pub trait Layout<'d> {
9    type Scope: driver::route::Selection;
10    /// Pre-park state selected once and retained with this pinned application.
11    type ParkPlan: executor::raw::Plan;
12}
13
14/// # Safety
15/// Visit each owner once through unchanged pinned projections, presence, and operation authority.
16/// Admission includes every present owner's route, without merging duplicate owners.
17/// Keep the root pinned; shutdown preserves client storage and finish destroys dependents before providers.
18/// A fixed `Scope` names this owner's admitted completion route.
19/// `ParkPlan = ()` runs `park` directly; `Flat` runs the unchanged `census`/`sweep` traversal.
20pub unsafe trait App<'d>: Sized + Layout<'d> {
21    const ROUTES: settings::Routes;
22
23    /// Routes on which a nested application may observe a completion.
24    const DISPATCH_ROUTES: settings::Routes = Self::ROUTES;
25
26    /// Routes whose installed owners may receive activation.
27    const WAKE_ROUTES: settings::Routes = settings::Routes::EMPTY;
28
29    /// Upper bound on idle coordinator probes, including delegated probes.
30    const COORDS: usize = 0;
31
32    const PARK: bool = false;
33
34    fn census(_: pin::Pin<&Self>, counts: &mut executor::raw::Census) -> Option<()> {
35        if Self::PARK {
36            counts.include_leaf(Self::COORDS)?;
37        }
38        Some(())
39    }
40
41    /// # Safety
42    /// Traverse the same admitted pinned owners in their census order.
43    unsafe fn sweep<'app, 'driver, 'turn>(
44        app: pin::Pin<&'app mut Self>,
45        sweep: &mut executor::raw::Sweep<'driver, 'turn, 'd, impl crate::io::Report>,
46    ) where
47        'd: 'driver + 'turn,
48    {
49        if Self::PARK {
50            // SAFETY: this leaf retains the census-admitted root and turn.
51            unsafe { sweep.leaf(app) };
52        }
53    }
54
55    /// # Safety
56    /// The exact pinned root is installed and admitted. Run this leaf's work
57    /// or this aggregate's own coordinator with the same driver and turn.
58    unsafe fn park<'app, 'driver, 'turn>(
59        _: pin::Pin<&'app mut Self>,
60        _: schedule::Turn<'turn, 'd>,
61        _: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
62    ) where
63        'd: 'driver + 'turn,
64    {
65    }
66
67    /// Accumulates the routes of the owners actually present in this pinned root.
68    fn admit(
69        _: pin::Pin<&Self>,
70        needs: &mut settings::claim::Needs,
71    ) -> Result<(), settings::errors::Claim> {
72        *needs = needs.include(settings::claim::Claim::new(Self::ROUTES))?;
73        Ok(())
74    }
75
76    /// # Safety
77    /// All external clients of this exact pinned owner have ended.
78    unsafe fn released(_: pin::Pin<&mut Self>) {}
79
80    fn install(_: pin::Pin<&mut Self>, _: life::Install<'_, 'd>) {}
81
82    /// Returns whether a present owner handled this activation. On `false`,
83    /// the caller may continue through the remaining owners of the same root.
84    ///
85    /// # Safety
86    /// Use this exact installed pinned root, driver and turn, with a target
87    /// belonging to the same driver and declared by `WAKE_ROUTES`.
88    unsafe fn activate<'app, 'driver, 'turn>(
89        _: pin::Pin<&'app mut Self>,
90        _: driver::route::Erased<'d, driver::route::kind::Any>,
91        _: schedule::Turn<'turn, 'd>,
92        _: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
93    ) -> bool
94    where
95        'd: 'driver + 'turn,
96    {
97        false
98    }
99
100    /// Resolves overlapping candidate routes against the receivers actually installed in this root.
101    fn has_dispatch_owner(_: pin::Pin<&Self>, route: driver::route::Route) -> bool {
102        Self::DISPATCH_ROUTES.has_route(route)
103    }
104
105    /// A selected receiver consumes the completion or returns it to the driver.
106    fn dispatch<'event>(
107        _: pin::Pin<&mut Self>,
108        event: executor::raw::Dispatch<'event, 'd, Self::Scope>,
109        _: schedule::Turn<'_, 'd>,
110        _: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
111    ) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd, Self::Scope>> {
112        ops::ControlFlow::Break(event)
113    }
114
115    /// Reports waits into the same traversal until an owner can run immediately.
116    fn progress<const STOP: bool>(
117        _: pin::Pin<&Self>,
118        _: executor::raw::Progress<'_, '_, 'd>,
119        _: Option<&mut schedule::Waits<'d>>,
120    ) -> ops::ControlFlow<()> {
121        ops::ControlFlow::Continue(())
122    }
123
124    fn shutdown(
125        _: pin::Pin<&mut Self>,
126        _: schedule::Turn<'_, 'd>,
127        _: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
128    ) {
129    }
130
131    fn finish(
132        _: pin::Pin<&mut Self>,
133        _: life::Install<'_, 'd>,
134        _: &mut life::finish::Finish<'_, 'd, impl crate::io::Report>,
135    ) {
136    }
137}
138
139impl<'d> Layout<'d> for () {
140    type Scope = std::convert::Infallible;
141    type ParkPlan = ();
142}
143
144// SAFETY: the empty root has no routes, retained owners, or lifecycle work.
145unsafe impl<'d> App<'d> for () {
146    const ROUTES: settings::Routes = settings::Routes::EMPTY;
147}