use std::{marker, ops, pin};
use crate::driver::{self, schedule, settings};
use crate::{executor, topology::lending};
#[pin_project::pin_project]
#[repr(transparent)]
pub struct Owner<S, T> {
#[pin]
stored: S,
app: marker::PhantomData<T>,
}
impl<S, T> Owner<S, T> {
pub(crate) const fn new(stored: S) -> Self {
Self {
stored,
app: marker::PhantomData,
}
}
}
unsafe impl<S: lending::raw::Project<App = T>, T> lending::raw::Project for Owner<S, T> {
type App = T;
fn shared(self: pin::Pin<&Self>) -> pin::Pin<&Self::App> {
self.project_ref().stored.shared()
}
fn exclusive(self: pin::Pin<&mut Self>) -> pin::Pin<&mut Self::App> {
self.project().stored.exclusive()
}
unsafe fn release(self: pin::Pin<&mut Self>) {
unsafe { self.project().stored.release() };
}
}
impl<'d, S, T> executor::raw::Layout<'d> for Owner<S, T>
where
T: executor::raw::Layout<'d>,
{
type Scope = <T as executor::raw::Layout<'d>>::Scope;
type ParkPlan = <T as executor::raw::Layout<'d>>::ParkPlan;
}
unsafe impl<'d, S, T> executor::raw::App<'d> for Owner<S, T>
where
S: lending::raw::Project<App = T>,
T: executor::raw::App<'d>,
{
const ROUTES: settings::Routes = T::ROUTES;
const DISPATCH_ROUTES: settings::Routes = T::DISPATCH_ROUTES;
const WAKE_ROUTES: settings::Routes = T::WAKE_ROUTES;
const COORDS: usize = <T as executor::raw::App<'d>>::COORDS;
const PARK: bool = T::PARK;
fn census(app: pin::Pin<&Self>, counts: &mut executor::raw::Census) -> Option<()> {
T::census(lending::raw::Project::shared(app), counts)
}
unsafe fn sweep<'app, 'driver, 'turn>(
app: pin::Pin<&'app mut Self>,
sweep: &mut executor::raw::Sweep<'driver, 'turn, 'd, impl crate::io::Report>,
) where
'd: 'driver + 'turn,
{
unsafe { T::sweep(lending::raw::Project::exclusive(app), sweep) }
}
unsafe fn park<'app, 'driver, 'turn>(
app: pin::Pin<&'app mut Self>,
turn: schedule::Turn<'turn, 'd>,
driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
) where
'd: 'driver + 'turn,
{
unsafe { T::park(lending::raw::Project::exclusive(app), turn, driver) }
}
unsafe fn activate<'app, 'driver, 'turn>(
app: pin::Pin<&'app mut Self>,
target: driver::route::Erased<'d, driver::route::kind::Any>,
turn: schedule::Turn<'turn, 'd>,
driver: &'driver mut driver::life::Lifecycle<'d, impl crate::io::Report>,
) -> bool
where
'd: 'driver + 'turn,
{
unsafe { T::activate(lending::raw::Project::exclusive(app), target, turn, driver) }
}
fn admit(
app: pin::Pin<&Self>,
needs: &mut settings::claim::Needs,
) -> Result<(), settings::errors::Claim> {
T::admit(lending::raw::Project::shared(app), needs)
}
unsafe fn released(app: pin::Pin<&mut Self>) {
unsafe { T::released(lending::raw::Project::exclusive(app)) };
}
fn install(app: pin::Pin<&mut Self>, install: driver::life::Install<'_, 'd>) {
T::install(lending::raw::Project::exclusive(app), install);
}
fn has_dispatch_owner(app: pin::Pin<&Self>, route: driver::route::Route) -> bool {
T::has_dispatch_owner(lending::raw::Project::shared(app), route)
}
fn dispatch<'event>(
app: pin::Pin<&mut Self>,
event: executor::raw::Dispatch<'event, 'd, Self::Scope>,
turn: schedule::Turn<'_, 'd>,
driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
) -> ops::ControlFlow<executor::raw::Dispatch<'event, 'd, Self::Scope>> {
T::dispatch(lending::raw::Project::exclusive(app), event, turn, driver)
}
fn shutdown(
app: pin::Pin<&mut Self>,
turn: schedule::Turn<'_, 'd>,
driver: &mut driver::life::Lifecycle<'d, impl crate::io::Report>,
) {
T::shutdown(lending::raw::Project::exclusive(app), turn, driver);
}
fn finish(
app: pin::Pin<&mut Self>,
install: driver::life::Install<'_, 'd>,
finish: &mut driver::life::finish::Finish<'_, 'd, impl crate::io::Report>,
) {
let mut stored = app.project().stored;
T::finish(stored.as_mut().exclusive(), install, finish);
unsafe { lending::raw::Project::release(stored) };
}
fn progress<const STOP: bool>(
app: pin::Pin<&Self>,
view: executor::raw::Progress<'_, '_, 'd>,
waits: Option<&mut schedule::Waits<'d>>,
) -> ops::ControlFlow<()> {
T::progress::<STOP>(lending::raw::Project::shared(app), view, waits)
}
}