use core::{marker, mem};
use dope_sys::io::reactor::event::kind::Send;
use o3::base::num::Epoch;
use crate::driver::{
self,
retained::flight::Echo,
route::{Route, Selection, Token, indices::SlotIndex, kind},
};
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Erased<'d, C, S: Selection = Route> {
pub(in crate::driver) token: Token,
pub(in crate::driver) flight: Option<Echo>,
pub(in crate::driver) driver: driver::Invariant<'d>,
pub(in crate::driver) class: marker::PhantomData<(C, S)>,
}
impl<'d> Erased<'d, kind::Any> {
pub(in crate::driver) const fn new(token: Token, driver: driver::Invariant<'d>) -> Self {
Self {
token,
flight: None,
driver,
class: marker::PhantomData,
}
}
}
impl<'d, C, S: Selection> Erased<'d, C, S> {
pub fn matches(self, target: Self) -> bool {
self.token == target.token
}
pub const fn route(self) -> Route {
match S::FIXED {
Some(route) => route,
None => self.token.route(),
}
}
pub const fn erase_route(self) -> Erased<'d, C> {
self.with_marker(Route::FRAMEWORK)
}
pub(crate) const fn with_marker<N: Selection>(self, _: N) -> Erased<'d, C, N> {
Erased {
token: self.token,
flight: self.flight,
driver: self.driver,
class: marker::PhantomData,
}
}
pub const fn slot(self) -> SlotIndex {
self.token.slot()
}
pub const fn epoch(self) -> Epoch {
self.token.epoch()
}
pub(crate) const fn into_token(self) -> Token {
self.token
}
}
impl<C, S: Selection> From<Erased<'_, C, S>> for Token {
fn from(target: Erased<'_, C, S>) -> Self {
target.token
}
}
const _: () = {
assert!(
mem::size_of::<Erased<'static, Send>>()
== mem::size_of::<Token>() + mem::size_of::<usize>()
);
assert!(mem::align_of::<Erased<'static, Send>>() == mem::align_of::<Token>());
};