1
- // Copyright (c) 2018 The predicates-rs Project Developers.
1
+ // Copyright (c) 2018, 2022 The predicates-rs Project Developers.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
4
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
@@ -35,26 +35,24 @@ impl fmt::Display for EqOps {
35
35
///
36
36
/// This is created by the `predicate::{eq, ne}` functions.
37
37
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
38
- pub struct EqPredicate < T >
39
- where
40
- T : fmt:: Debug + PartialEq ,
41
- {
38
+ pub struct EqPredicate < T > {
42
39
constant : T ,
43
40
op : EqOps ,
44
41
}
45
42
46
- impl < T > Predicate < T > for EqPredicate < T >
43
+ impl < P , T > Predicate < P > for EqPredicate < T >
47
44
where
48
- T : fmt:: Debug + PartialEq ,
45
+ T : std:: borrow:: Borrow < P > + fmt:: Debug ,
46
+ P : fmt:: Debug + PartialEq + ?Sized ,
49
47
{
50
- fn eval ( & self , variable : & T ) -> bool {
48
+ fn eval ( & self , variable : & P ) -> bool {
51
49
match self . op {
52
- EqOps :: Equal => variable. eq ( & self . constant ) ,
53
- EqOps :: NotEqual => variable. ne ( & self . constant ) ,
50
+ EqOps :: Equal => variable. eq ( self . constant . borrow ( ) ) ,
51
+ EqOps :: NotEqual => variable. ne ( self . constant . borrow ( ) ) ,
54
52
}
55
53
}
56
54
57
- fn find_case < ' a > ( & ' a self , expected : bool , variable : & T ) -> Option < reflection:: Case < ' a > > {
55
+ fn find_case < ' a > ( & ' a self , expected : bool , variable : & P ) -> Option < reflection:: Case < ' a > > {
58
56
utils:: default_find_case ( self , expected, variable) . map ( |case| {
59
57
case. add_product ( reflection:: Product :: new (
60
58
"var" ,
@@ -64,32 +62,11 @@ where
64
62
}
65
63
}
66
64
67
- impl < ' a , T > Predicate < T > for EqPredicate < & ' a T >
68
- where
69
- T : fmt:: Debug + PartialEq + ?Sized ,
70
- {
71
- fn eval ( & self , variable : & T ) -> bool {
72
- match self . op {
73
- EqOps :: Equal => variable. eq ( self . constant ) ,
74
- EqOps :: NotEqual => variable. ne ( self . constant ) ,
75
- }
76
- }
77
-
78
- fn find_case < ' b > ( & ' b self , expected : bool , variable : & T ) -> Option < reflection:: Case < ' b > > {
79
- utils:: default_find_case ( self , expected, variable) . map ( |case| {
80
- case. add_product ( reflection:: Product :: new (
81
- "var" ,
82
- utils:: DebugAdapter :: new ( variable) . to_string ( ) ,
83
- ) )
84
- } )
85
- }
86
- }
87
-
88
- impl < T > reflection:: PredicateReflection for EqPredicate < T > where T : fmt:: Debug + PartialEq { }
65
+ impl < T > reflection:: PredicateReflection for EqPredicate < T > where T : fmt:: Debug { }
89
66
90
67
impl < T > fmt:: Display for EqPredicate < T >
91
68
where
92
- T : fmt:: Debug + PartialEq ,
69
+ T : fmt:: Debug ,
93
70
{
94
71
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
95
72
let palette = crate :: Palette :: current ( ) ;
@@ -120,6 +97,10 @@ where
120
97
/// let predicate_fn = predicate::eq("Hello");
121
98
/// assert_eq!(true, predicate_fn.eval("Hello"));
122
99
/// assert_eq!(false, predicate_fn.eval("Goodbye"));
100
+ ///
101
+ /// let predicate_fn = predicate::eq(String::from("Hello"));
102
+ /// assert_eq!(true, predicate_fn.eval("Hello"));
103
+ /// assert_eq!(false, predicate_fn.eval("Goodbye"));
123
104
/// ```
124
105
pub fn eq < T > ( constant : T ) -> EqPredicate < T >
125
106
where
@@ -178,28 +159,26 @@ impl fmt::Display for OrdOps {
178
159
///
179
160
/// This is created by the `predicate::{gt, ge, lt, le}` functions.
180
161
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
181
- pub struct OrdPredicate < T >
182
- where
183
- T : fmt:: Debug + PartialOrd ,
184
- {
162
+ pub struct OrdPredicate < T > {
185
163
constant : T ,
186
164
op : OrdOps ,
187
165
}
188
166
189
- impl < T > Predicate < T > for OrdPredicate < T >
167
+ impl < P , T > Predicate < P > for OrdPredicate < T >
190
168
where
191
- T : fmt:: Debug + PartialOrd ,
169
+ T : std:: borrow:: Borrow < P > + fmt:: Debug ,
170
+ P : fmt:: Debug + PartialOrd + ?Sized ,
192
171
{
193
- fn eval ( & self , variable : & T ) -> bool {
172
+ fn eval ( & self , variable : & P ) -> bool {
194
173
match self . op {
195
- OrdOps :: LessThan => variable. lt ( & self . constant ) ,
196
- OrdOps :: LessThanOrEqual => variable. le ( & self . constant ) ,
197
- OrdOps :: GreaterThanOrEqual => variable. ge ( & self . constant ) ,
198
- OrdOps :: GreaterThan => variable. gt ( & self . constant ) ,
174
+ OrdOps :: LessThan => variable. lt ( self . constant . borrow ( ) ) ,
175
+ OrdOps :: LessThanOrEqual => variable. le ( self . constant . borrow ( ) ) ,
176
+ OrdOps :: GreaterThanOrEqual => variable. ge ( self . constant . borrow ( ) ) ,
177
+ OrdOps :: GreaterThan => variable. gt ( self . constant . borrow ( ) ) ,
199
178
}
200
179
}
201
180
202
- fn find_case < ' a > ( & ' a self , expected : bool , variable : & T ) -> Option < reflection:: Case < ' a > > {
181
+ fn find_case < ' a > ( & ' a self , expected : bool , variable : & P ) -> Option < reflection:: Case < ' a > > {
203
182
utils:: default_find_case ( self , expected, variable) . map ( |case| {
204
183
case. add_product ( reflection:: Product :: new (
205
184
"var" ,
@@ -209,34 +188,11 @@ where
209
188
}
210
189
}
211
190
212
- impl < ' a , T > Predicate < T > for OrdPredicate < & ' a T >
213
- where
214
- T : fmt:: Debug + PartialOrd + ?Sized ,
215
- {
216
- fn eval ( & self , variable : & T ) -> bool {
217
- match self . op {
218
- OrdOps :: LessThan => variable. lt ( self . constant ) ,
219
- OrdOps :: LessThanOrEqual => variable. le ( self . constant ) ,
220
- OrdOps :: GreaterThanOrEqual => variable. ge ( self . constant ) ,
221
- OrdOps :: GreaterThan => variable. gt ( self . constant ) ,
222
- }
223
- }
224
-
225
- fn find_case < ' b > ( & ' b self , expected : bool , variable : & T ) -> Option < reflection:: Case < ' b > > {
226
- utils:: default_find_case ( self , expected, variable) . map ( |case| {
227
- case. add_product ( reflection:: Product :: new (
228
- "var" ,
229
- utils:: DebugAdapter :: new ( variable) . to_string ( ) ,
230
- ) )
231
- } )
232
- }
233
- }
234
-
235
- impl < T > reflection:: PredicateReflection for OrdPredicate < T > where T : fmt:: Debug + PartialOrd { }
191
+ impl < T > reflection:: PredicateReflection for OrdPredicate < T > where T : fmt:: Debug { }
236
192
237
193
impl < T > fmt:: Display for OrdPredicate < T >
238
194
where
239
- T : fmt:: Debug + PartialOrd ,
195
+ T : fmt:: Debug ,
240
196
{
241
197
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
242
198
let palette = crate :: Palette :: current ( ) ;
@@ -267,6 +223,10 @@ where
267
223
/// let predicate_fn = predicate::lt("b");
268
224
/// assert_eq!(true, predicate_fn.eval("a"));
269
225
/// assert_eq!(false, predicate_fn.eval("c"));
226
+ ///
227
+ /// let predicate_fn = predicate::lt(String::from("b"));
228
+ /// assert_eq!(true, predicate_fn.eval("a"));
229
+ /// assert_eq!(false, predicate_fn.eval("c"));
270
230
/// ```
271
231
pub fn lt < T > ( constant : T ) -> OrdPredicate < T >
272
232
where
0 commit comments