predicates/
boxed.rs

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
12use std::fmt;
13
14use crate::reflection;
15use crate::utils;
16use crate::Predicate;
17
18/// `Predicate` that wraps another `Predicate` as a trait object, allowing
19/// sized storage of predicate types.
20pub struct BoxPredicate<Item: ?Sized>(Box<dyn Predicate<Item> + Send + Sync>);
21
22impl<Item> BoxPredicate<Item>
23where
24    Item: ?Sized,
25{
26    /// Creates a new `BoxPredicate`, a wrapper around a dynamically-dispatched
27    /// `Predicate` type with useful trait impls.
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
36impl<Item> fmt::Debug for BoxPredicate<Item>
37where
38    Item: ?Sized,
39{
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.debug_struct("BoxPredicate").finish()
42    }
43}
44
45impl<Item> reflection::PredicateReflection for BoxPredicate<Item>
46where
47    Item: ?Sized,
48{
49    fn parameters<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Parameter<'a>> + 'a> {
50        self.0.parameters()
51    }
52
53    fn children<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Child<'a>> + 'a> {
54        self.0.children()
55    }
56}
57
58impl<Item> fmt::Display for BoxPredicate<Item>
59where
60    Item: ?Sized,
61{
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(f, "{}", self.0)
64    }
65}
66
67impl<Item> Predicate<Item> for BoxPredicate<Item>
68where
69    Item: ?Sized,
70{
71    fn eval(&self, variable: &Item) -> bool {
72        self.0.eval(variable)
73    }
74
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`.
81pub trait PredicateBoxExt<Item: ?Sized>
82where
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    /// ```
109    fn boxed(self) -> BoxPredicate<Item>
110    where
111        Self: Sized + Send + Sync + 'static,
112    {
113        BoxPredicate::new(self)
114    }
115}
116
117impl<P, Item> PredicateBoxExt<Item> for P where P: Predicate<Item> {}