use std::fmt;
use reflection;
use utils;
use Predicate;
pub struct BoxPredicate<Item: ?Sized>(Box<Predicate<Item> + Send + Sync>);
impl<Item> BoxPredicate<Item>
where
Item: ?Sized,
{
pub fn new<P: Predicate<Item>>(inner: P) -> BoxPredicate<Item>
where
P: Send + Sync + 'static,
{
BoxPredicate(Box::new(inner))
}
}
impl<Item> fmt::Debug for BoxPredicate<Item>
where
Item: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("BoxPredicate").finish()
}
}
impl<Item> reflection::PredicateReflection for BoxPredicate<Item>
where
Item: ?Sized,
{
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
self.0.parameters()
}
fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
self.0.children()
}
}
impl<Item> fmt::Display for BoxPredicate<Item>
where
Item: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<Item> Predicate<Item> for BoxPredicate<Item>
where
Item: ?Sized,
{
fn eval(&self, variable: &Item) -> bool {
self.0.eval(variable)
}
fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
utils::default_find_case(self, expected, variable)
}
}
pub trait PredicateBoxExt<Item: ?Sized>
where
Self: Predicate<Item>,
{
fn boxed(self) -> BoxPredicate<Item>
where
Self: Sized + Send + Sync + 'static,
{
BoxPredicate::new(self)
}
}
impl<P, Item> PredicateBoxExt<Item> for P
where
P: Predicate<Item>,
{
}