// Copyright (c) 2018 The predicates-rs Project Developers. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! Predicate that can wrap other dynamically-called predicates in an //! easy-to-manage type. use std::fmt; use crate::reflection; use crate::Predicate; /// `Predicate` that wraps another `Predicate` as a trait object, allowing /// sized storage of predicate types. pub struct BoxPredicate(Box + Send + Sync>); impl BoxPredicate where Item: ?Sized, { /// Creates a new `BoxPredicate`, a wrapper around a dynamically-dispatched /// `Predicate` type with useful trait impls. pub fn new

(inner: P) -> BoxPredicate where P: Predicate + Send + Sync + 'static, { BoxPredicate(Box::new(inner)) } } impl fmt::Debug for BoxPredicate where Item: ?Sized, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("BoxPredicate").finish() } } impl reflection::PredicateReflection for BoxPredicate where Item: ?Sized, { fn parameters<'a>(&'a self) -> Box> + 'a> { self.0.parameters() } fn children<'a>(&'a self) -> Box> + 'a> { self.0.children() } } impl fmt::Display for BoxPredicate where Item: ?Sized, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl Predicate for BoxPredicate where Item: ?Sized, { fn eval(&self, variable: &Item) -> bool { self.0.eval(variable) } fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option> { self.0.find_case(expected, variable) } } /// `Predicate` extension for boxing a `Predicate`. pub trait PredicateBoxExt where Self: Predicate, { /// Returns a `BoxPredicate` wrapper around this `Predicate` type. /// /// Returns a `BoxPredicate` wrapper around this `Predicate` type. The /// `BoxPredicate` type has a number of useful properties: /// /// - It stores the inner predicate as a trait object, so the type of /// `BoxPredicate` will always be the same even if steps are added or /// removed from the predicate. /// - It is a common type, allowing it to be stored in vectors or other /// collection types. /// - It implements `Debug` and `Display`. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let predicates = vec![ /// predicate::always().boxed(), /// predicate::never().boxed(), /// ]; /// assert_eq!(true, predicates[0].eval(&4)); /// assert_eq!(false, predicates[1].eval(&4)); /// ``` fn boxed(self) -> BoxPredicate where Self: Sized + Send + Sync + 'static, { BoxPredicate::new(self) } } impl PredicateBoxExt for P where P: Predicate {} #[cfg(test)] mod test { use crate::prelude::*; #[test] fn unsized_boxed() { let p = predicate::always().boxed(); p.eval("4"); } #[test] fn boxed_find_case() { let p1 = predicate::gt(5); let p2 = p1.boxed(); match (p1.find_case(false, &4), p2.find_case(false, &4)) { (Some(c1), Some(c2)) => { assert_eq!(format!("{c1:?}"), format!("{c2:?}")); } _ => { panic!(); } } } }