@@ -20,18 +20,49 @@ use Predicate;
20
20
/// Note that this implementation places the fewest restrictions on the
21
21
/// underlying `Item` type at the expense of having the least performant
22
22
/// implementation (linear search). If the type to be searched is `Hash + Eq`,
23
- /// it is much more efficient to use `HashableContainsPredicate ` and
24
- /// `contains_hashable `. The implementation-specific predicates will be
23
+ /// it is much more efficient to use `HashableInPredicate ` and
24
+ /// `in_hash `. The implementation-specific predicates will be
25
25
/// deprecated when Rust supports trait specialization.
26
26
#[ derive( Debug ) ]
27
- pub struct ContainsPredicate < T >
27
+ pub struct InPredicate < T >
28
28
where
29
29
T : PartialEq ,
30
30
{
31
31
inner : Vec < T > ,
32
32
}
33
33
34
- impl < T > Predicate < T > for ContainsPredicate < T >
34
+ impl < T > InPredicate < T >
35
+ where
36
+ T : Ord ,
37
+ {
38
+ /// Creates a new predicate that will return `true` when the given `variable` is
39
+ /// contained with the set of items provided.
40
+ ///
41
+ /// Note that this implementation requires `Item` to be `Ord`. The
42
+ /// `InPredicate` uses a less efficient search algorithm but only
43
+ /// requires `Item` implement `PartialEq`. The implementation-specific
44
+ /// predicates will be deprecated when Rust supports trait specialization.
45
+ ///
46
+ /// # Examples
47
+ ///
48
+ /// ```
49
+ /// use predicates::prelude::*;
50
+ ///
51
+ /// let predicate_fn = predicate::in_iter(vec![1, 3, 5]).sort();
52
+ /// assert_eq!(true, predicate_fn.eval(&1));
53
+ /// assert_eq!(false, predicate_fn.eval(&2));
54
+ /// assert_eq!(true, predicate_fn.eval(&3));
55
+ /// assert_eq!(false, predicate_fn.eval(&4));
56
+ /// assert_eq!(true, predicate_fn.eval(&5));
57
+ /// ```
58
+ pub fn sort ( self ) -> OrdInPredicate < T > {
59
+ let mut items = self . inner ;
60
+ items. sort ( ) ;
61
+ OrdInPredicate { inner : items }
62
+ }
63
+ }
64
+
65
+ impl < T > Predicate < T > for InPredicate < T >
35
66
where
36
67
T : PartialEq ,
37
68
{
@@ -46,28 +77,28 @@ where
46
77
/// Note that this implementation places the fewest restrictions on the
47
78
/// underlying `Item` type at the expense of having the least performant
48
79
/// implementation (linear search). If the type to be searched is `Hash + Eq`,
49
- /// it is much more efficient to use `HashableContainsPredicate ` and
50
- /// `contains_hashable `. The implementation-specific predicates will be
80
+ /// it is much more efficient to use `HashableInPredicate ` and
81
+ /// `in_hash `. The implementation-specific predicates will be
51
82
/// deprecated when Rust supports trait specialization.
52
83
///
53
84
/// # Examples
54
85
///
55
86
/// ```
56
87
/// use predicates::prelude::*;
57
88
///
58
- /// let predicate_fn = predicate::contains (vec![1, 3, 5]);
89
+ /// let predicate_fn = predicate::in_iter (vec![1, 3, 5]);
59
90
/// assert_eq!(true, predicate_fn.eval(&1));
60
91
/// assert_eq!(false, predicate_fn.eval(&2));
61
92
/// assert_eq!(true, predicate_fn.eval(&3));
62
93
/// assert_eq!(false, predicate_fn.eval(&4));
63
94
/// assert_eq!(true, predicate_fn.eval(&5));
64
95
/// ```
65
- pub fn contains < I , T > ( iter : I ) -> ContainsPredicate < T >
96
+ pub fn in_iter < I , T > ( iter : I ) -> InPredicate < T >
66
97
where
67
98
T : PartialEq ,
68
99
I : IntoIterator < Item = T > ,
69
100
{
70
- ContainsPredicate {
101
+ InPredicate {
71
102
inner : Vec :: from_iter ( iter) ,
72
103
}
73
104
}
@@ -76,20 +107,20 @@ where
76
107
/// set, otherwise returns `false`.
77
108
///
78
109
/// Note that this implementation requires `Item` to be `Ord`. The
79
- /// `ContainsPredicate ` uses a less efficient search algorithm but only
110
+ /// `InPredicate ` uses a less efficient search algorithm but only
80
111
/// requires `Item` implement `PartialEq`. The implementation-specific
81
112
/// predicates will be deprecated when Rust supports trait specialization.
82
113
///
83
- /// This is created by the `predicate::contains_ord ` function.
114
+ /// This is created by the `predicate::in_iter(...).sort ` function.
84
115
#[ derive( Debug ) ]
85
- pub struct OrdContainsPredicate < T >
116
+ pub struct OrdInPredicate < T >
86
117
where
87
118
T : Ord ,
88
119
{
89
120
inner : Vec < T > ,
90
121
}
91
122
92
- impl < T > Predicate < T > for OrdContainsPredicate < T >
123
+ impl < T > Predicate < T > for OrdInPredicate < T >
93
124
where
94
125
T : Ord ,
95
126
{
@@ -98,54 +129,24 @@ where
98
129
}
99
130
}
100
131
101
- /// Creates a new predicate that will return `true` when the given `variable` is
102
- /// contained with the set of items provided.
103
- ///
104
- /// Note that this implementation requires `Item` to be `Ord`. The
105
- /// `ContainsPredicate` uses a less efficient search algorithm but only
106
- /// requires `Item` implement `PartialEq`. The implementation-specific
107
- /// predicates will be deprecated when Rust supports trait specialization.
108
- ///
109
- /// # Examples
110
- ///
111
- /// ```
112
- /// use predicates::prelude::*;
113
- ///
114
- /// let predicate_fn = predicate::contains_ord(vec![1, 3, 5]);
115
- /// assert_eq!(true, predicate_fn.eval(&1));
116
- /// assert_eq!(false, predicate_fn.eval(&2));
117
- /// assert_eq!(true, predicate_fn.eval(&3));
118
- /// assert_eq!(false, predicate_fn.eval(&4));
119
- /// assert_eq!(true, predicate_fn.eval(&5));
120
- /// ```
121
- pub fn contains_ord < I , T > ( iter : I ) -> OrdContainsPredicate < T >
122
- where
123
- T : Ord ,
124
- I : IntoIterator < Item = T > ,
125
- {
126
- let mut items = Vec :: from_iter ( iter) ;
127
- items. sort ( ) ;
128
- OrdContainsPredicate { inner : items }
129
- }
130
-
131
132
/// Predicate that returns `true` if `variable` is a member of the pre-defined
132
133
/// `HashSet`, otherwise returns `false`.
133
134
///
134
135
/// Note that this implementation requires `Item` to be `Hash + Eq`. The
135
- /// `ContainsPredicate ` uses a less efficient search algorithm but only
136
+ /// `InPredicate ` uses a less efficient search algorithm but only
136
137
/// requires `Item` implement `PartialEq`. The implementation-specific
137
138
/// predicates will be deprecated when Rust supports trait specialization.
138
139
///
139
- /// This is created by the `predicate::contains_hashable ` function.
140
+ /// This is created by the `predicate::in_hash ` function.
140
141
#[ derive( Debug ) ]
141
- pub struct HashableContainsPredicate < T >
142
+ pub struct HashableInPredicate < T >
142
143
where
143
144
T : Hash + Eq ,
144
145
{
145
146
inner : HashSet < T > ,
146
147
}
147
148
148
- impl < T > Predicate < T > for HashableContainsPredicate < T >
149
+ impl < T > Predicate < T > for HashableInPredicate < T >
149
150
where
150
151
T : Hash + Eq ,
151
152
{
@@ -158,7 +159,7 @@ where
158
159
/// contained with the set of items provided.
159
160
///
160
161
/// Note that this implementation requires `Item` to be `Hash + Eq`. The
161
- /// `ContainsPredicate ` uses a less efficient search algorithm but only
162
+ /// `InPredicate ` uses a less efficient search algorithm but only
162
163
/// requires `Item` implement `PartialEq`. The implementation-specific
163
164
/// predicates will be deprecated when Rust supports trait specialization.
164
165
///
@@ -167,19 +168,19 @@ where
167
168
/// ```
168
169
/// use predicates::prelude::*;
169
170
///
170
- /// let predicate_fn = predicate::contains_hashable (vec![1, 3, 5]);
171
+ /// let predicate_fn = predicate::in_hash (vec![1, 3, 5]);
171
172
/// assert_eq!(true, predicate_fn.eval(&1));
172
173
/// assert_eq!(false, predicate_fn.eval(&2));
173
174
/// assert_eq!(true, predicate_fn.eval(&3));
174
175
/// assert_eq!(false, predicate_fn.eval(&4));
175
176
/// assert_eq!(true, predicate_fn.eval(&5));
176
177
/// ```
177
- pub fn contains_hashable < I , T > ( iter : I ) -> HashableContainsPredicate < T >
178
+ pub fn in_hash < I , T > ( iter : I ) -> HashableInPredicate < T >
178
179
where
179
180
T : Hash + Eq ,
180
181
I : IntoIterator < Item = T > ,
181
182
{
182
- HashableContainsPredicate {
183
+ HashableInPredicate {
183
184
inner : HashSet :: from_iter ( iter) ,
184
185
}
185
186
}
0 commit comments