use crate::old_impl::core::{Bhv, Status};
pub struct WhenAny<C>(pub(crate) Box<[Box<dyn Bhv<Context=C>>]>);
pub struct WhenAll<C>(pub(crate) Box<[Box<dyn Bhv<Context=C>>]>);
impl<C> WhenAny<C> {
#[inline]
pub fn new(bhvs: Box<[Box<dyn Bhv<Context=C>>]>) -> Self {
Self(bhvs)
}
}
impl<C> WhenAll<C> {
#[inline]
pub fn new(bhvs: Box<[Box<dyn Bhv<Context=C>>]>) -> Self {
Self(bhvs)
}
}
impl<C> Bhv for WhenAny<C> {
type Context = C;
fn update(&mut self, ctx: &mut Self::Context) -> Status {
let mut any_running = false;
for node in self.0.iter_mut() {
match node.update(ctx) {
Status::Running => any_running = true,
Status::Failure => continue,
Status::Success => return Status::Success,
}
}
if any_running {
Status::Running
} else {
Status::Failure
}
}
}
impl<C> Bhv for WhenAll<C> {
type Context = C;
fn update(&mut self, ctx: &mut Self::Context) -> Status {
let mut any_running = false;
for node in self.0.iter_mut() {
match node.update(ctx) {
Status::Running => any_running = true,
Status::Success => continue,
Status::Failure => return Status::Failure,
}
}
if any_running {
Status::Running
} else {
Status::Success
}
}
}
#[macro_export]
macro_rules! when_any {
() => {
compile_error!("`when_any` should have at least one argument!")
};
($($x:expr),+$(,)?) => {
$crate::WhenAny::new(
Box::new([$(Box::new($x)),+]),
)
};
}
#[macro_export]
macro_rules! when_all {
() => {
compile_error!("`when_any` should have at least one argument!")
};
($($x:expr),+$(,)?) => {
$crate::WhenAll::new(
Box::new([$(Box::new($x)),+]),
)
};
}