use dope_sys::io::reactor::event::Report;
use std::{marker, mem, ops::ControlFlow, pin};
use crate::driver::{self, life, life::finish::Finish, schedule};
use crate::{client, executor, topology::lending};
use o3::base::cell::region;
mod project;
mod resume;
#[repr(transparent)]
pub struct Borrowed<'a, T>(pin::Pin<&'a mut T>);
impl<'loan, O> Borrowed<'loan, O> {
pub(crate) const fn new(owner: pin::Pin<&'loan mut O>) -> Self {
Self(owner)
}
pub fn root_client<'d>(&self) -> <O::Provider as client::Provider<'d>>::Client<'loan>
where
'd: 'loan,
O: lending::Root<'d>,
O::Provider: 'loan,
{
let provider = lending::Root::provider(self.0.as_ref());
client::Provider::provide(client::Context::<'_, 'loan, 'd, _>::new(provider))
}
}
pub use project::Project;
pub use resume::Resume;
unsafe impl<'d, A, B> Resume<'d> for crate::client::Pair<'d, A, B>
where
A: lending::Root<'d> + Resume<'d>,
B: lending::Root<'d> + Resume<'d>,
{
}
#[pin_project::pin_project]
#[derive(dope_gen::Application)]
#[dispatcher(core = crate, driver = 'd, lending = true)]
pub struct Bound<'app, 'd, H, T> {
#[dispatcher(state)]
driver: marker::PhantomData<driver::Reference<'d>>,
#[pin]
#[app]
head: pin::Pin<&'app mut H>,
#[pin]
#[app]
tail: mem::ManuallyDrop<T>,
}
impl<'app, 'd, H, T> Bound<'app, 'd, H, T> {
pub(crate) const fn new(head: Borrowed<'app, H>, tail: T) -> Self {
const {
assert!(mem::size_of::<Self>() == mem::size_of::<(pin::Pin<&'app mut H>, T)>());
assert!(mem::align_of::<Self>() == mem::align_of::<(pin::Pin<&'app mut H>, T)>());
assert!(!mem::needs_drop::<Self>());
}
Self {
driver: marker::PhantomData,
head: head.0,
tail: mem::ManuallyDrop::new(tail),
}
}
pub fn tail(self: pin::Pin<&Self>) -> pin::Pin<&T> {
Project::shared(self.project_ref().tail)
}
}
impl<'d, H, T> lending::Root<'d> for Bound<'_, 'd, H, T>
where
T: lending::Root<'d>,
{
type Provider = T::Provider;
fn provider(self: pin::Pin<&Self>) -> pin::Pin<&Self::Provider> {
lending::Root::provider(Project::shared(self.project_ref().tail))
}
}
unsafe impl<'d, H, T> executor::raw::Drain<'d> for Bound<'_, 'd, H, T>
where
Self: executor::raw::App<'d>,
T: executor::raw::Drain<'d>,
{
fn begin(
self: pin::Pin<&mut Self>,
turn: schedule::Turn<'_, 'd>,
driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
) {
executor::raw::Drain::begin(Project::exclusive(self.project().tail), turn, driver);
}
fn progress(
self: pin::Pin<&Self>,
region: ®ion::Token<region::Identity<'d>>,
state: Option<&mut schedule::Waits<'d>>,
) -> ControlFlow<()> {
executor::raw::Drain::progress(self.tail(), region, state)
}
fn force(
self: pin::Pin<&mut Self>,
turn: schedule::Turn<'_, 'd>,
driver: &mut life::Lifecycle<'d, impl Report<Event = crate::Reported>>,
) {
executor::raw::Drain::force(Project::exclusive(self.project().tail), turn, driver);
}
}
impl<'parent, 'd, H, T> Bound<'parent, 'd, H, T>
where
H: executor::raw::App<'d>,
T: executor::raw::App<'d>,
{
pub(crate) unsafe fn finish_tail(
app: pin::Pin<&mut Self>,
install: life::Install<'_, 'd>,
finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
plan: &executor::raw::Flat,
) -> <H as executor::raw::Layout<'d>>::ParkPlan {
let app = app.project();
let mut tail = app.tail;
T::finish(Project::exclusive(tail.as_mut()), install, finish);
unsafe { Project::release(tail) };
let head = Project::shared(app.head.as_ref());
unsafe { plan.resume(head) }
}
}
const _: () = {
assert!(mem::size_of::<Bound<'_, '_, (), ()>>() == mem::size_of::<&mut ()>());
assert!(mem::align_of::<Bound<'_, '_, (), ()>>() == mem::align_of::<&mut ()>());
assert!(!mem::needs_drop::<Bound<'_, '_, (), ()>>());
};
pub unsafe trait Head<'d>: Sized {
type Joined<T: executor::raw::App<'d>>: executor::raw::App<'d>;
type Retired;
type Time<C: Copy>;
fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T>;
unsafe fn retire<T, C: Copy>(
app: pin::Pin<&mut Self::Joined<T>>,
install: life::Install<'_, 'd>,
finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
source: C,
) -> (Self::Retired, Self::Time<C>)
where
T: executor::raw::App<'d>;
}
unsafe impl<'d> Head<'d> for () {
type Joined<T: executor::raw::App<'d>> = T;
type Retired = ();
type Time<C: Copy> = ();
fn join<T: executor::raw::App<'d>>(self, tail: T) -> T {
tail
}
unsafe fn retire<T, C: Copy>(
app: pin::Pin<&mut T>,
install: life::Install<'_, 'd>,
finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
_: &mut T::ParkPlan,
_: C,
) -> ((), ())
where
T: executor::raw::App<'d>,
{
T::finish(app, install, finish);
((), ())
}
}
unsafe impl<'parent, 'd: 'parent, H> Head<'d> for Borrowed<'parent, H>
where
H: Resume<'d>,
{
type Joined<T: executor::raw::App<'d>> = Bound<'parent, 'd, H, T>;
type Retired = H::ParkPlan;
type Time<C: Copy> = C;
fn join<T: executor::raw::App<'d>>(self, tail: T) -> Self::Joined<T> {
Bound::new(self, tail)
}
unsafe fn retire<T, C: Copy>(
app: pin::Pin<&mut Self::Joined<T>>,
install: life::Install<'_, 'd>,
finish: &mut Finish<'_, 'd, impl Report<Event = crate::Reported>>,
plan: &mut <Self::Joined<T> as executor::raw::Layout<'d>>::ParkPlan,
source: C,
) -> (Self::Retired, C)
where
T: executor::raw::App<'d>,
{
(
unsafe { Bound::finish_tail(app, install, finish, plan) },
source,
)
}
}