Skip to main content

dope_core/topology/lending/raw/
owner.rs

1use std::{marker, ops, pin};
2
3use crate::driver::{self, schedule, settings};
4
5use crate::{executor, topology::lending};
6
7#[pin_project::pin_project]
8#[repr(transparent)]
9pub struct Owner<S, T> {
10    #[pin]
11    stored: S,
12    app: marker::PhantomData<T>,
13}
14
15impl<S, T> Owner<S, T> {
16    pub(crate) const fn new(stored: S) -> Self {
17        Self {
18            stored,
19            app: marker::PhantomData,
20        }
21    }
22}
23
24// SAFETY: the pinned stored projection remains unchanged for the entire lifecycle.
25unsafe impl<S: lending::raw::Project<App = T>, T> lending::raw::Project for Owner<S, T> {
26    type App = T;
27
28    fn shared(self: pin::Pin<&Self>) -> pin::Pin<&Self::App> {
29        self.project_ref().stored.shared()
30    }
31
32    fn exclusive(self: pin::Pin<&mut Self>) -> pin::Pin<&mut Self::App> {
33        self.project().stored.exclusive()
34    }
35
36    unsafe fn release(self: pin::Pin<&mut Self>) {
37        // SAFETY: the enclosing lifecycle has finished this exact projection.
38        unsafe { self.project().stored.release() };
39    }
40}
41
42impl<'d, S, T> executor::raw::Layout<'d> for Owner<S, T>
43where
44    T: executor::raw::Layout<'d>,
45{
46    type Scope = <T as executor::raw::Layout<'d>>::Scope;
47    type ParkPlan = <T as executor::raw::Layout<'d>>::ParkPlan;
48}
49
50// SAFETY: lifecycle traversal retains one exact pinned application. Its storage
51// is released once after final traversal, before its enclosing owner is finished.
52unsafe impl<'d, S, T> executor::raw::App<'d> for Owner<S, T>
53where
54    S: lending::raw::Project<App = T>,
55    T: executor::raw::App<'d>,
56{
57    const ROUTES: settings::Routes = T::ROUTES;
58    const DISPATCH_ROUTES: settings::Routes = T::DISPATCH_ROUTES;
59    const WAKE_ROUTES: settings::Routes = T::WAKE_ROUTES;
60    const COORDS: usize = <T as executor::raw::App<'d>>::COORDS;
61    const PARK: bool = T::PARK;
62
63    fn census(app: pin::Pin<&Self>, counts: &mut executor::raw::Census) -> Option<()> {
64        T::census(lending::raw::Project::shared(app), counts)
65    }
66
67    unsafe fn sweep<'app, 'driver, 'turn>(
68        app: pin::Pin<&'app mut Self>,
69        sweep: &mut executor::raw::Sweep<'driver, 'turn, 'd, impl crate::io::Report>,
70    ) where
71        'd: 'driver + 'turn,
72    {
73        // SAFETY: this owner retains the exact installed pinned projection.
74        unsafe { T::sweep(lending::raw::Project::exclusive(app), sweep) }
75    }
76
77    unsafe fn park<'app, 'driver, 'turn>(
78        app: pin::Pin<&'app mut Self>,
79        turn: schedule::Turn<'turn, 'd>,
80        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
81    ) where
82        'd: 'driver + 'turn,
83    {
84        // SAFETY: this owner preserves the same installed pinned projection.
85        unsafe { T::park(lending::raw::Project::exclusive(app), turn, driver) }
86    }
87
88    unsafe fn activate<'app, 'driver, 'turn>(
89        app: pin::Pin<&'app mut Self>,
90        target: driver::route::Erased<'d, driver::route::kind::Any>,
91        turn: schedule::Turn<'turn, 'd>,
92        driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
93    ) -> bool
94    where
95        'd: 'driver + 'turn,
96    {
97        // SAFETY: this owner retains the installed application and its wake routes.
98        unsafe { T::activate(lending::raw::Project::exclusive(app), target, turn, driver) }
99    }
100
101    fn admit(
102        app: pin::Pin<&Self>,
103        needs: &mut settings::claim::Needs,
104    ) -> Result<(), settings::errors::Claim> {
105        T::admit(lending::raw::Project::shared(app), needs)
106    }
107
108    unsafe fn released(app: pin::Pin<&mut Self>) {
109        // SAFETY: this projection retains the exact owner whose external clients ended.
110        unsafe { T::released(lending::raw::Project::exclusive(app)) };
111    }
112
113    fn install(app: pin::Pin<&mut Self>, install: driver::life::Install<'_, 'd>) {
114        T::install(lending::raw::Project::exclusive(app), install);
115    }
116
117    fn has_dispatch_owner(app: pin::Pin<&Self>, route: driver::route::Route) -> bool {
118        T::has_dispatch_owner(lending::raw::Project::shared(app), route)
119    }
120
121    fn dispatch<'event>(
122        app: pin::Pin<&mut Self>,
123        event: executor::raw::Dispatch<'event, 'd, Self::Scope>,
124        turn: schedule::Turn<'_, 'd>,
125        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
126    ) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd, Self::Scope>> {
127        T::dispatch(lending::raw::Project::exclusive(app), event, turn, driver)
128    }
129
130    fn shutdown(
131        app: pin::Pin<&mut Self>,
132        turn: schedule::Turn<'_, 'd>,
133        driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
134    ) {
135        T::shutdown(lending::raw::Project::exclusive(app), turn, driver);
136    }
137
138    fn finish(
139        app: pin::Pin<&mut Self>,
140        install: driver::life::Install<'_, 'd>,
141        finish: &mut driver::life::finish::Finish<'_, 'd, impl crate::io::Report>,
142    ) {
143        let mut stored = app.project().stored;
144        T::finish(stored.as_mut().exclusive(), install, finish);
145        // SAFETY: this exact storage has completed its final traversal.
146        unsafe { lending::raw::Project::release(stored) };
147    }
148
149    fn progress<const STOP: bool>(
150        app: pin::Pin<&Self>,
151        view: executor::raw::Progress<'_, '_, 'd>,
152        waits: Option<&mut schedule::Waits<'d>>,
153    ) -> ops::ControlFlow<()> {
154        T::progress::<STOP>(lending::raw::Project::shared(app), view, waits)
155    }
156}