1 // Copyright (c) 2018 The predicates-rs Project Developers.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/license/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 //! Predicate that can wrap other dynamically-called predicates in an
10 //! easy-to-manage type.
11 
12 use std::fmt;
13 
14 use crate::reflection;
15 use crate::utils;
16 use crate::Predicate;
17 
18 /// `Predicate` that wraps another `Predicate` as a trait object, allowing
19 /// sized storage of predicate types.
20 pub struct BoxPredicate<Item: ?Sized>(Box<dyn Predicate<Item> + Send + Sync>);
21 
22 impl<Item> BoxPredicate<Item>
23 where
24     Item: ?Sized,
25 {
26     /// Creates a new `BoxPredicate`, a wrapper around a dynamically-dispatched
27     /// `Predicate` type with useful trait impls.
new<P: Predicate<Item>>(inner: P) -> BoxPredicate<Item> where P: Send + Sync + 'static,28     pub fn new<P: Predicate<Item>>(inner: P) -> BoxPredicate<Item>
29     where
30         P: Send + Sync + 'static,
31     {
32         BoxPredicate(Box::new(inner))
33     }
34 }
35 
36 impl<Item> fmt::Debug for BoxPredicate<Item>
37 where
38     Item: ?Sized,
39 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result40     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41         f.debug_struct("BoxPredicate").finish()
42     }
43 }
44 
45 impl<Item> reflection::PredicateReflection for BoxPredicate<Item>
46 where
47     Item: ?Sized,
48 {
parameters<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Parameter<'a>> + 'a>49     fn parameters<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Parameter<'a>> + 'a> {
50         self.0.parameters()
51     }
52 
children<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Child<'a>> + 'a>53     fn children<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Child<'a>> + 'a> {
54         self.0.children()
55     }
56 }
57 
58 impl<Item> fmt::Display for BoxPredicate<Item>
59 where
60     Item: ?Sized,
61 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result62     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63         self.0.fmt(f)
64     }
65 }
66 
67 impl<Item> Predicate<Item> for BoxPredicate<Item>
68 where
69     Item: ?Sized,
70 {
eval(&self, variable: &Item) -> bool71     fn eval(&self, variable: &Item) -> bool {
72         self.0.eval(variable)
73     }
74 
find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>>75     fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
76         utils::default_find_case(self, expected, variable)
77     }
78 }
79 
80 /// `Predicate` extension for boxing a `Predicate`.
81 pub trait PredicateBoxExt<Item: ?Sized>
82 where
83     Self: Predicate<Item>,
84 {
85     /// Returns a `BoxPredicate` wrapper around this `Predicate` type.
86     ///
87     /// Returns a `BoxPredicate` wrapper around this `Predicate type. The
88     /// `BoxPredicate` type has a number of useful properties:
89     ///
90     ///   - It stores the inner predicate as a trait object, so the type of
91     ///     `BoxPredicate` will always be the same even if steps are added or
92     ///     removed from the predicate.
93     ///   - It is a common type, allowing it to be stored in vectors or other
94     ///     collection types.
95     ///   - It implements `Debug` and `Display`.
96     ///
97     /// # Examples
98     ///
99     /// ```
100     /// use predicates::prelude::*;
101     ///
102     /// let predicates = vec![
103     ///     predicate::always().boxed(),
104     ///     predicate::never().boxed(),
105     /// ];
106     /// assert_eq!(true, predicates[0].eval(&4));
107     /// assert_eq!(false, predicates[1].eval(&4));
108     /// ```
boxed(self) -> BoxPredicate<Item> where Self: Sized + Send + Sync + 'static,109     fn boxed(self) -> BoxPredicate<Item>
110     where
111         Self: Sized + Send + Sync + 'static,
112     {
113         BoxPredicate::new(self)
114     }
115 }
116 
117 impl<P, Item> PredicateBoxExt<Item> for P where P: Predicate<Item> {}
118