use std::{marker, mem, ops::ControlFlow, pin, result::Result as StdResult, time};
use crate::{
Errno,
driver::{self, life, schedule::clock},
io::Event,
};
use crate::{
client,
executor::{self, InstallError, drive},
topology::lending,
};
mod access;
mod app;
mod error;
mod lent;
mod manual;
pub(crate) mod run;
pub use access::Access;
pub use app::App;
pub use error::Error;
pub type Result<'report, T, X = std::convert::Infallible> =
std::result::Result<T, Error<'report, T, X>>;
pub use drive::block::Fixed;
pub use manual::Manual;
#[derive(Debug)]
pub enum Cause<'a, X = std::convert::Infallible> {
Drive(&'a drive::Error),
Quiesce {
drive: &'a drive::Error,
disarm: Option<Errno>,
},
Install {
cause: &'a InstallError<X>,
disarm: Option<Errno>,
},
}
pub(crate) struct Core<'a, 'd, Q, Report> {
pub(crate) scope: life::Scope<'a, 'd, Report>,
pub(super) pending: Option<Event<'d>>,
pub(crate) lifecycle: &'a Q,
}
pub struct Session<'i, 'life: 'i, 'd: 'life, Q: 'life, Report, H = (), S = (), P: ?Sized + 'i = ()>
{
core: &'i mut Core<'life, 'd, Q, Report>,
head: H,
phase: S,
source: marker::PhantomData<(*mut &'i (), *mut P)>,
}
enum State<H, E, R, C, X> {
Rejected(E),
Clock(drive::Error),
Executed(H, run::Outcome<R, C, X>, Option<Errno>),
}
#[must_use]
pub struct Completed<
'i,
H,
E,
R,
P: ?Sized = (),
C = drive::clock::Wall,
X = std::convert::Infallible,
> {
state: State<H, E, R, C, X>,
source: marker::PhantomData<(*mut &'i (), *mut P)>,
}
impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, S, P: ?Sized + 'i, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report, H, S, P>
{
pub fn driver(&mut self) -> &mut driver::Local<'d> {
self.core.scope.context()
}
}
impl<'i, 'life: 'i, 'd: 'life, Q: 'life, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report>
{
pub fn scope(&mut self) -> &mut life::Scope<'life, 'd, Report> {
&mut self.core.scope
}
}
impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, S, P: ?Sized + 'i, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report, H, S, P>
where
H: lending::raw::Head<'d>,
{
pub fn reject<E, R, C>(self, error: E) -> Completed<'i, H::Retired, E, R, P, C, Q::Error>
where
Q: executor::Startup,
{
Completed {
state: State::Rejected(error),
source: self.source,
}
}
}
impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, P: ?Sized + 'i, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report, H, (), P>
where
H: lending::raw::Head<'d>,
{
pub fn admit(
self,
start: Option<clock::Deadline<'d>>,
) -> StdResult<
Session<'i, 'life, 'd, Q, Report, H, clock::Sample, P>,
Session<'i, 'life, 'd, Q, Report, H, drive::Error, P>,
> {
match self.core.sample(start) {
Ok(sample) => Ok(Session {
core: self.core,
head: self.head,
phase: sample,
source: self.source,
}),
Err(error) => Err(Session {
core: self.core,
head: self.head,
phase: error,
source: self.source,
}),
}
}
}
impl<'i, 'life: 'i, 'd: 'life, Q: 'life, H, P: ?Sized + 'i, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report, H, drive::Error, P>
where
H: lending::raw::Head<'d>,
{
pub fn complete<E, R, C>(self) -> Completed<'i, H::Retired, E, R, P, C, Q::Error>
where
Q: executor::Startup,
{
Completed {
state: State::Clock(self.phase),
source: self.source,
}
}
}
impl<
'i,
'life: 'i,
'd: 'life,
Q: executor::Startup + 'life,
H,
P: ?Sized + 'i,
Report: crate::io::Report,
> Session<'i, 'life, 'd, Q, Report, H, clock::Sample, P>
where
H: lending::raw::Head<'d>,
{
pub fn with_app<A, E, R>(
self,
app: A,
run: impl for<'run> FnOnce(App<'run, 'life, 'd, H::Joined<A>, Report, Q>) -> R,
) -> Completed<'i, H::Retired, E, R, P, drive::clock::Wall, Q::Error>
where
A: executor::raw::App<'d>,
{
let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
(run(scope.app()), drive::clock::wall())
});
Completed {
state: State::Executed(head, outcome, disarm),
source: self.source,
}
}
pub fn drive_until<A, W, E, T>(
self,
app: A,
wait: W,
) -> Completed<
'i,
H::Retired,
E,
StdResult<ControlFlow<T, executor::Requested<Q>>, drive::Error>,
P,
drive::clock::Wall,
Q::Error,
>
where
A: executor::raw::App<'d>,
W: executor::root::Root<'d>,
W::Output: Into<T>,
{
let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
(
scope
.app()
.drive_until(wait)
.map(|exit| exit.map_break(Into::into)),
drive::clock::wall(),
)
});
Completed {
state: State::Executed(head, outcome, disarm),
source: self.source,
}
}
pub fn with_quiesce<A, E, R>(
self,
app: A,
timeout: time::Duration,
run: impl for<'run> FnOnce(App<'run, 'life, 'd, H::Joined<A>, Report, Q>) -> R,
) -> Completed<
'i,
H::Retired,
E,
R,
P,
drive::clock::Bound<drive::clock::Wall, drive::clock::Span>,
Q::Error,
>
where
A: executor::raw::App<'d>,
{
use drive::clock::{Bound, wall};
let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
let output = run(scope.app());
(output, Bound::for_duration(wall(), timeout))
});
Completed {
state: State::Executed(head, outcome, disarm),
source: self.source,
}
}
pub fn with_at_for<A, E, R>(
self,
app: A,
timeout: time::Duration,
run: impl for<'run> FnOnce(Manual<'run, 'life, 'd, H::Joined<A>, Report, Q>) -> R,
) -> Completed<
'i,
H::Retired,
E,
R,
P,
drive::clock::Bound<drive::clock::Fixed<'d>, drive::clock::Span>,
Q::Error,
>
where
A: executor::raw::App<'d>,
{
use drive::clock::{Bound, at};
let (head, outcome, disarm) = self.core.execute(self.head, app, self.phase, |scope| {
let output = run(Manual {
access: Access::new(&mut *scope.core, scope.owner.view()),
});
let now = scope.core.scope.driver_ref().clock().now().into();
(output, Bound::for_duration(at(now), timeout))
});
Completed {
state: State::Executed(head, outcome, disarm),
source: self.source,
}
}
}
impl<'i, 'life: 'i, 'd: 'life, Q: executor::Startup + 'life, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report>
{
pub fn as_mut(&mut self) -> Session<'_, 'life, 'd, Q, Report> {
Session::new(self.core)
}
pub fn with_app<'report, D, R>(
&mut self,
report: &'report mut Completion,
app: D,
run: impl for<'app> FnOnce(App<'app, 'life, 'd, D, Report, Q>) -> R,
) -> Result<'report, R, Q::Error>
where
D: executor::raw::App<'d>,
{
let sample = self.core.sample(None).map_err(Error::Drive)?;
let ((), outcome, disarm) = self.core.execute((), app, sample, |scope| {
(run(scope.app()), drive::clock::wall())
});
run::complete(outcome, disarm, report)
}
pub fn with_quiesce<'report, D, R>(
&mut self,
report: &'report mut Completion,
app: D,
timeout: time::Duration,
run: impl for<'app> FnOnce(App<'app, 'life, 'd, D, Report, Q>) -> R,
) -> Result<'report, R, Q::Error>
where
D: executor::raw::App<'d>,
{
use drive::clock::{Bound, wall};
let sample = self.core.sample(None).map_err(Error::Drive)?;
let ((), outcome, disarm) = self.core.execute((), app, sample, |scope| {
let output = run(scope.app());
(output, Bound::for_duration(wall(), timeout))
});
run::complete(outcome, disarm, report)
}
pub fn with_at_for<'report, D, R>(
&mut self,
report: &'report mut Completion,
app: D,
start: clock::Deadline<'d>,
timeout: time::Duration,
run: impl for<'app> FnOnce(Manual<'app, 'life, 'd, D, Report, Q>) -> R,
) -> Result<'report, R, Q::Error>
where
D: executor::raw::App<'d>,
{
use drive::clock::{Bound, at};
let sample = self.core.sample(Some(start)).map_err(Error::Drive)?;
let ((), outcome, disarm) = self.core.execute((), app, sample, |scope| {
let output = run(Manual {
access: Access::new(&mut *scope.core, scope.owner.view()),
});
let now = scope.core.scope.driver_ref().clock().now().into();
(output, Bound::for_duration(at(now), timeout))
});
run::complete(outcome, disarm, report)
}
pub fn drive<R>(&mut self, root: R) -> StdResult<R::Output, drive::Error>
where
R: executor::boot::Root<'d>,
{
use drive::{block, turn};
use turn::scope;
let observer = self.core.lifecycle;
let state = scope::Boot::try_new(self.core).map_err(drive::Error::from);
block::run_boot(state, root, observer)
}
pub(crate) fn new(core: &'i mut Core<'life, 'd, Q, Report>) -> Self {
Self {
core,
head: (),
phase: (),
source: marker::PhantomData,
}
}
}
impl<'i, 'life: 'i, 'd: 'life, Q: 'life, O, S, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report, lending::raw::Borrowed<'i, O>, S, O::Provider>
where
O: lending::Root<'d>,
O::Provider: 'i,
{
pub fn root_client(&self) -> <O::Provider as client::Provider<'d>>::Client<'i> {
let root = lending::raw::Project::shared(pin::Pin::new(&self.head));
let provider = lending::Root::provider(root);
client::Provider::provide(client::Context::<'_, 'i, 'd, _>::new(provider))
}
}
impl<'i, 'life: 'i, 'd: 'life, Q: executor::Startup + 'life, S, Report: crate::io::Report>
Session<'i, 'life, 'd, Q, Report, (), S>
{
pub fn with_provider<'report, O, E, R, D, C>(
self,
report: &'report mut Completion,
owner: O,
body: impl for<'loan> FnOnce(
Session<'loan, 'life, 'd, Q, Report, lending::raw::Borrowed<'loan, O>, S, O::Provider>,
) -> (
Completed<'loan, O::ParkPlan, E, R, O::Provider, C, Q::Error>,
D,
),
) -> (lending::Result<'report, E, R, Q::Error>, D)
where
O: lending::Root<'d> + lending::raw::Resume<'d>,
C: drive::clock::Time,
{
let mut owner = pin::pin!(owner);
let scope = Session {
core: &mut *self.core,
head: lending::raw::Owner::new(owner.as_mut()),
phase: self.phase,
source: marker::PhantomData,
};
let (Completed { state, .. }, data) = body(scope);
let result = match state {
State::Rejected(error) => Err(lending::Error::Build(error)),
State::Clock(error) => Err(lending::Error::Run(Error::Drive(error))),
State::Executed(plan, outcome, disarm) => {
let head = lending::raw::Owner::new(owner.as_mut());
run::finish_parent(self.core, (head, plan), report, outcome, disarm)
.map_err(lending::Error::Run)
}
};
(result, data)
}
}
impl<'i, E, R, C, X> Completed<'i, (), E, R, (), C, X> {
pub fn finish<'report>(
self,
report: &'report mut Completion,
) -> lending::Result<'report, E, R, X> {
match self.state {
State::Rejected(error) => Err(lending::Error::Build(error)),
State::Clock(error) => Err(lending::Error::Run(Error::Drive(error))),
State::Executed((), outcome, disarm) => {
run::complete(outcome, disarm, report).map_err(lending::Error::Run)
}
}
}
}
impl<'a, 'd, Q: 'a, Report: crate::io::Report> Core<'a, 'd, Q, Report> {
pub(super) fn new(scope: life::Scope<'a, 'd, Report>, source: &'a Q) -> Self {
Self {
scope,
pending: None,
lifecycle: source,
}
}
fn sample(&self, start: Option<clock::Deadline<'d>>) -> StdResult<clock::Sample, drive::Error> {
let now = start.map_or_else(time::Instant::now, clock::Deadline::into_raw);
self.check_time(now)?;
Ok(clock::Sample::new(now))
}
fn check_time(&self, now: time::Instant) -> StdResult<(), drive::Error> {
let current = self.scope.driver_ref().clock().now().into_raw();
if now < current {
return Err(drive::Error::regression(now, current));
}
Ok(())
}
}
const _: [(); mem::size_of::<Core<'static, 'static, (), ()>>()] =
[(); mem::size_of::<life::Scope<'static, 'static, ()>>()
+ mem::size_of::<Option<Event<'static>>>()
+ mem::size_of::<usize>()];
#[derive(Debug, Default)]
pub struct Completion {
quiesce: Option<drive::Error>,
}
impl Completion {
pub const fn new() -> Self {
Self { quiesce: None }
}
pub(crate) fn failed<T, X>(
&mut self,
output: T,
drive: drive::Error,
disarm: Option<Errno>,
) -> Error<'_, T, X> {
let drive = self.quiesce.insert(drive);
Error::Quiesce {
output,
drive,
disarm,
}
}
}