use std::pin;
use dope_core::driver::{
route::{self, Identity, kind},
schedule,
};
use o3::buffer::bytes::{Prefix, bounded};
use crate::{
app::{dispatch, dispatch::event},
file::appender::{self, state::State},
};
pub(super) fn bytes<'a, const N: usize>(
pending: &'a appender::Pending<'_, N>,
) -> Prefix<'a, { bounded::MAX }> {
let bytes = pending.bytes();
unsafe {
Prefix::from_raw(bounded::raw::Input::new(
bytes.get_unchecked(bytes.len() - pending.remaining().into_raw()..),
))
}
}
unsafe impl<'p, 'd, ID: Identity, const N: usize, const B: usize> dispatch::raw::Manifold<'d>
for appender::Appender<'p, 'd, ID, N, B>
{
type Route = <ID as route::Materialize<kind::Write>>::With;
type Dispatch = event::Driven<dispatch::raw::Retained>;
type Install = dispatch::raw::Omit;
type Finish = Self;
type Activate = dispatch::raw::Omit;
type Probe = Self;
type PrePark = dispatch::raw::Retained;
type Shutdown = dispatch::raw::Plain;
const COORDS: usize = 0;
unsafe fn pre_park<'a, 'turn>(
mut self: pin::Pin<&'a mut Self>,
_: schedule::Turn<'turn, 'd>,
driver: &mut dispatch::raw::Context<'_, 'd, Self::PrePark>,
) -> pin::Pin<&'a mut Self> {
let this = self.as_mut().get_mut();
if let Some(pending) = this.prepare() {
this.submit(pending, driver);
}
self
}
fn shutdown<'a, 'turn>(
mut self: pin::Pin<&'a mut Self>,
_: schedule::Turn<'turn, 'd>,
_: &mut dispatch::raw::Context<'_, 'd, Self::Shutdown>,
) -> pin::Pin<&'a mut Self> {
self.as_mut().get_mut().close();
self
}
}
unsafe impl<'p, 'd, ID: Identity, const N: usize, const B: usize> dispatch::raw::Probe<'d, Self>
for appender::Appender<'p, 'd, ID, N, B>
{
type Plan = ();
fn select() -> Option<Self::Plan> {
Some(())
}
fn run(call: dispatch::raw::Call<'_, '_, '_, 'd, Self, ()>) -> schedule::Progress<'d> {
match call.owner().get_ref().progress() {
State::Runnable => schedule::Progress::Runnable,
State::Waiting => schedule::Progress::waiting(call.region()),
State::Quiescent => schedule::Progress::Quiescent,
}
}
}
unsafe impl<'p, 'd, ID: Identity, const N: usize, const B: usize> dispatch::raw::Control<'d>
for appender::Appender<'p, 'd, ID, N, B>
{
type View<'step>
= appender::Control<'step, 'p, 'd, ID, N, B>
where
Self: 'step,
'd: 'step;
fn view<'step>(owner: dispatch::raw::Installed<'step, 'd, Self>) -> Self::View<'step>
where
'd: 'step,
{
appender::Control { inner: owner }
}
}