use core::marker::PhantomData;
use crate::{Handler, StageError};
pub trait Before<C, R, E> {
fn run_before(&self, context: &mut C, resources: &mut R) -> Result<(), StageError<E>>;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NoBefore;
impl<C, R, E> Before<C, R, E> for NoBefore {
fn run_before(&self, _context: &mut C, _resources: &mut R) -> Result<(), StageError<E>> {
Ok(())
}
}
pub struct BeforeFn<F, Params> {
function: F,
marker: PhantomData<fn() -> Params>,
}
impl<F, Params> BeforeFn<F, Params> {
pub(crate) const fn new(function: F) -> Self {
Self {
function,
marker: PhantomData,
}
}
pub const fn function(&self) -> &F {
&self.function
}
pub fn function_mut(&mut self) -> &mut F {
&mut self.function
}
pub fn into_inner(self) -> F {
self.function
}
}
impl<C, R, E, F, Params> Before<C, R, E> for BeforeFn<F, Params>
where
F: Handler<C, R, (), E, Params>,
{
fn run_before(&self, context: &mut C, resources: &mut R) -> Result<(), StageError<E>> {
self.function.run(context, resources)
}
}