use std::marker::PhantomData;
use crate::{Bhv, Status};
#[derive(Clone)]
pub struct Cond<Ctx, C>(C, PhantomData<Ctx>)
where
C: Fn(&Ctx) -> bool;
#[derive(Clone)]
pub struct Action<Ctx, A>(A, PhantomData<Ctx>)
where
A: FnMut(&mut Ctx);
#[derive(Clone)]
pub struct AsyncAction<Ctx, A>(A, PhantomData<Ctx>)
where A: FnMut(&mut Ctx) -> Status;
#[inline]
pub fn cond<Ctx, C>(c: C) -> Cond<Ctx, C>
where
C: Fn(&Ctx) -> bool,
{
Cond(c, PhantomData)
}
#[inline]
pub fn action<Ctx, A>(a: A) -> Action<Ctx, A>
where
A: FnMut(&mut Ctx),
{
Action(a, PhantomData)
}
#[inline]
pub fn async_action<Ctx, A>(a: A) -> AsyncAction<Ctx, A>
where A: FnMut(&mut Ctx) -> Status { AsyncAction(a, PhantomData) }
impl<Ctx, C> Bhv for Cond<Ctx, C>
where
C: Fn(&Ctx) -> bool,
{
type Context = Ctx;
#[inline]
fn update(&mut self, ctx: &mut Self::Context) -> Status {
if self.0(ctx) {
Status::Success
} else {
Status::Failure
}
}
}
impl<Ctx, A> Bhv for Action<Ctx, A>
where
A: FnMut(&mut Ctx),
{
type Context = Ctx;
#[inline]
fn update(&mut self, ctx: &mut Self::Context) -> Status {
self.0(ctx);
Status::Success
}
}
impl<Ctx, A> Bhv for AsyncAction<Ctx, A>
where A: FnMut(&mut Ctx) -> Status,
{
type Context = Ctx;
#[inline]
fn update(&mut self, ctx: &mut Self::Context) -> Status {
self.0(ctx)
}
}