use std::marker;
use dope_sys::Errno;
use o3::base::cell::region;
use crate::{
driver::{
self,
life::{Final, native::resolver::Resolver},
receive::credit,
route,
route::kind,
schedule::clock,
},
io::{
self,
event::{batch::Flow, net::recv},
transfer,
},
};
#[must_use]
pub struct Finish<'a, 'd, Report> {
last: Resolver<'a, 'd, Report, Final>,
forced: bool,
disarm: Option<Errno>,
}
impl<'a, 'd, Report: io::Report> Finish<'a, 'd, Report> {
pub(super) fn new(last: Resolver<'a, 'd, Report, Final>, forced: bool) -> Self {
Self {
last,
forced,
disarm: None,
}
}
pub(super) fn resume(self) {
self.last.resume();
}
pub fn is_forced(&self) -> bool {
self.forced
}
pub fn fail_disarm(&mut self, cause: Errno) {
if self.disarm.is_none() {
self.disarm = Some(cause);
}
}
pub fn disarm_error(&self) -> Option<Errno> {
self.disarm
}
pub fn clock<'f>(&'f self) -> clock::Clock<'f, 'd> {
self.last.lifecycle.driver_ref().clock()
}
pub fn reclaim(&mut self, done: recv::Data<'d, impl driver::route::Selection>) {
self.last.lifecycle.reclaim(done);
}
pub fn receive_result(
&mut self,
target: route::Erased<'d, kind::Recv, impl route::Selection>,
flow: Flow,
result: Result<transfer::Len, Errno>,
) {
self.last.lifecycle.receive_result(target, flow, result);
}
pub fn region_token<'f>(&'f mut self) -> &'f mut region::Token<region::Identity<'d>> {
&mut self.last.lifecycle.local.region.0
}
pub fn regions(
&mut self,
) -> &mut (
region::Token<region::Identity<'d>>,
region::Token<region::Identity<'d, 1>>,
) {
&mut self.last.lifecycle.local.region
}
pub fn state_token<'f>(&'f mut self) -> &'f mut region::Token<region::Identity<'d, 1>> {
&mut self.last.lifecycle.local.region.1
}
pub(in crate::driver) fn brand(&self) -> driver::Invariant<'d> {
self.last.lifecycle.driver_ref().brand
}
pub fn credit<'f>(&'f mut self) -> Credit<'f, 'd> {
Credit::new(self.brand())
}
}
const _: () = {
assert!(
std::mem::size_of::<Finish<'static, 'static, ()>>()
== std::mem::size_of::<(Resolver<'static, 'static, (), Final>, bool)>()
);
};
#[repr(transparent)]
pub struct Credit<'a, 'd> {
borrow: marker::PhantomData<(&'a mut (), driver::Invariant<'d>)>,
}
impl<'a, 'd> Credit<'a, 'd> {
const fn new(_brand: driver::Invariant<'d>) -> Self {
Self {
borrow: marker::PhantomData,
}
}
pub fn detach(self, storage: &credit::Storage<'d>) {
for slot in storage.entries.iter() {
slot.detach();
}
}
}
const _: () = {
assert!(std::mem::size_of::<Credit<'static, 'static>>() == 0);
};