use std::marker;
use dope_sys::table::token::host;
use crate::{
driver::{
self,
retained::flight::{GENERATION_BITS, INDEX_BITS, Target, storage},
route::{self, kind},
},
io::{
event::{batch::Flow, net::send},
transfer::Len,
},
};
#[must_use = "a validated flight completion must be committed"]
pub(crate) struct Complete<'d, C: kind::Class = kind::Any> {
pub(super) completion: storage::Complete<'d, Target, INDEX_BITS, GENERATION_BITS>,
pub(super) driver: driver::Reference<'d>,
pub(super) class: marker::PhantomData<C>,
}
impl<'d> Complete<'d> {
pub(crate) fn bind<C: kind::Flight>(self, proof: kind::Witness<C>) -> Complete<'d, C> {
Complete {
completion: self.completion,
driver: self.driver,
class: proof.marker(),
}
}
pub(crate) fn kind(&self) -> kind::Native {
self.completion.value().kind
}
}
impl<'d, C: kind::Class> Complete<'d, C> {
pub(crate) fn target<K: kind::Flight>(&self, _: kind::Witness<K>) -> route::Erased<'d, K> {
self.completion.value().bind(
self.driver.brand,
super::raw::Echo::from_inner(self.completion.echo()),
)
}
pub(crate) const fn driver(&self) -> driver::Reference<'d> {
self.driver
}
pub(crate) fn complete(self) -> route::Erased<'d, C> {
let echo = super::raw::Echo::from_inner(self.completion.echo());
self.completion.complete().bind(self.driver.brand, echo)
}
pub(crate) fn settle(self, flow: Flow) -> route::Erased<'d, C> {
let echo = super::raw::Echo::from_inner(self.completion.echo());
self.completion.settle(flow).bind(self.driver.brand, echo)
}
}
impl<'d> Complete<'d, kind::SendOwned> {
pub(crate) fn owned(self, result: Result<Len, crate::Errno>) -> send::Owned<'d> {
send::Owned::new(self.completion, self.driver.brand, result)
}
}
impl<C: kind::Class> host::Flight<Flow> for Complete<'_, C> {
type Key = super::raw::Echo;
fn finish(self) {
self.completion.finish();
}
fn end(self, flow: Flow) -> Self::Key {
let echo = super::raw::Echo::from_inner(self.completion.echo());
if flow == Flow::Last {
self.completion.finish();
}
echo
}
}