Skip to main content

dope_runtime/executor/raw/
app.rs

1use std::{ops, pin};
2
3use dope_core::driver::{self, schedule, settings};
4
5use crate::executor;
6
7/// Composes a deferred parent loan with its pinned field projection.
8pub fn project_field<'a, P: 'a, T: 'a>(
9    parent: impl FnOnce() -> pin::Pin<&'a mut P>,
10    field: impl FnOnce(pin::Pin<&'a mut P>) -> pin::Pin<&'a mut T>,
11) -> impl FnOnce() -> pin::Pin<&'a mut T> {
12    move || field(parent())
13}
14
15/// Application storage metadata independent of lifecycle eligibility.
16pub trait Layout<'d> {
17    /// Pre-park state selected once and retained with this pinned application.
18    type ParkPlan;
19}
20
21/// # Safety
22/// Visit each owner once through unchanged pinned projections, presence, and operation authority.
23/// Admission includes every present owner's route, without merging duplicate owners.
24/// Keep the root pinned; shutdown preserves client storage and finish destroys dependents before providers.
25pub unsafe trait App<'d>: Sized + Layout<'d> {
26    const ROUTES: settings::Routes;
27
28    /// Routes on which a nested application may observe a completion.
29    const DISPATCH_ROUTES: settings::Routes = Self::ROUTES;
30
31    /// Routes whose installed owners may receive activation.
32    const WAKE_ROUTES: settings::Routes = settings::Routes::EMPTY;
33
34    /// Upper bound on idle coordinator probes, including delegated probes.
35    const COORDS: usize;
36
37    const PARK: bool;
38
39    fn select_park() -> Self::ParkPlan;
40
41    /// Prepares the selected coordinator shares once after installation.
42    /// The count does not exceed `COORDS`; field presence and shares remain
43    /// stable through finish. `None` reports count overflow.
44    fn prepare_park(_: pin::Pin<&Self>, _: &mut Self::ParkPlan) -> Option<executor::raw::Count> {
45        Some(executor::raw::Count::new(Self::COORDS))
46    }
47
48    /// # Safety
49    /// The exact pinned root is installed and this plan completed preparation
50    /// and admission for that root. The park authority belongs to the same root.
51    unsafe fn park<'app, 'driver, 'turn>(
52        app: pin::Pin<&'app mut Self>,
53        plan: &mut Self::ParkPlan,
54        park: executor::raw::Park<'driver, 'turn, 'd>,
55    ) where
56        'd: 'driver + 'turn;
57
58    /// Accumulates the routes of the owners actually present in this pinned root.
59    fn admit(
60        _: pin::Pin<&Self>,
61        needs: &mut settings::claim::Needs,
62    ) -> Result<(), settings::errors::Claim> {
63        *needs = needs.include(settings::claim::Claim::new(Self::ROUTES))?;
64        Ok(())
65    }
66
67    fn released(released: executor::raw::Released<'_, Self>) -> executor::raw::Released<'_, Self> {
68        released
69    }
70
71    fn install<'app, 'owner>(
72        install: executor::raw::Install<'app, 'owner, 'd, Self>,
73    ) -> executor::raw::Install<'app, 'owner, 'd, Self> {
74        install
75    }
76
77    /// Returns whether a present owner handled this activation. On `false`,
78    /// the caller may continue through the remaining owners of the same root.
79    ///
80    /// # Safety
81    /// The activation retains this exact installed pinned root and a target
82    /// declared by `WAKE_ROUTES`.
83    unsafe fn activate<'app, 'driver, 'turn>(
84        _: pin::Pin<&'app mut Self>,
85        _: executor::raw::Activate<'driver, 'turn, 'd>,
86    ) -> bool
87    where
88        'd: 'driver + 'turn,
89    {
90        false
91    }
92
93    fn dispatch<'event>(
94        _: pin::Pin<&mut Self>,
95        event: executor::raw::Dispatch<'event, 'd>,
96        _: schedule::Turn<'_, 'd>,
97        _: driver::Context<'_, 'd>,
98    ) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd>> {
99        ops::ControlFlow::Break(event)
100    }
101
102    /// A nested miss preserves the event without borrowing its owner.
103    fn dispatch_from<'app, 'event, const NESTED: bool>(
104        project: impl FnOnce() -> pin::Pin<&'app mut Self>,
105        event: executor::raw::Dispatch<'event, 'd>,
106        turn: schedule::Turn<'_, 'd>,
107        driver: driver::Context<'_, 'd>,
108    ) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd>>
109    where
110        Self: 'app,
111    {
112        if !NESTED
113            || Self::ROUTES
114                .intersection(Self::DISPATCH_ROUTES)
115                .has_route(event.route())
116        {
117            Self::dispatch(project(), event, turn, driver)
118        } else {
119            ops::ControlFlow::Break(event)
120        }
121    }
122
123    fn progress(
124        app: pin::Pin<&Self>,
125        view: executor::raw::Progress<'_, '_, 'd>,
126    ) -> schedule::Progress<'d>;
127
128    fn stopping(
129        app: pin::Pin<&Self>,
130        view: executor::raw::Progress<'_, '_, 'd>,
131    ) -> schedule::Progress<'d>;
132
133    fn shutdown<'app, 'turn, 'driver>(
134        shutdown: executor::raw::Shutdown<'app, 'turn, 'driver, 'd, Self>,
135    ) -> executor::raw::Shutdown<'app, 'turn, 'driver, 'd, Self> {
136        shutdown
137    }
138
139    fn finish<'app, 'finish, 'owner, 'epoch>(
140        finish: executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self>,
141    ) -> executor::raw::Finish<'app, 'finish, 'owner, 'epoch, 'd, Self> {
142        finish
143    }
144}