@@ -22,6 +22,10 @@ use crate::matchers::__internal_unstable_do_not_depend_on_these::DisjunctionMatc
22
22
use std:: fmt:: Debug ;
23
23
24
24
/// An interface for checking an arbitrary condition on a datum.
25
+ ///
26
+ /// This trait is automatically implemented for a reference of any type
27
+ /// implementing `Matcher`. This simplifies reusing a matcher in different
28
+ /// assertions.
25
29
pub trait Matcher {
26
30
/// The type against which this matcher matches.
27
31
type ActualT : Debug + ?Sized ;
@@ -268,3 +272,39 @@ impl MatcherResult {
268
272
matches ! ( self , MatcherResult :: NoMatch )
269
273
}
270
274
}
275
+
276
+ impl < M : Matcher > Matcher for & M {
277
+ type ActualT = M :: ActualT ;
278
+
279
+ fn matches ( & self , actual : & Self :: ActualT ) -> MatcherResult {
280
+ ( * self ) . matches ( actual)
281
+ }
282
+
283
+ fn describe ( & self , matcher_result : MatcherResult ) -> Description {
284
+ ( * self ) . describe ( matcher_result)
285
+ }
286
+
287
+ fn explain_match ( & self , actual : & Self :: ActualT ) -> Description {
288
+ ( * self ) . explain_match ( actual)
289
+ }
290
+ }
291
+
292
+ #[ cfg( test) ]
293
+ mod tests {
294
+ use crate :: prelude:: * ;
295
+
296
+ #[ test]
297
+ fn ref_matchers_can_be_reused ( ) -> Result < ( ) > {
298
+ let matcher = eq ( 1 ) ;
299
+
300
+ verify_that ! ( 1 , & matcher) ?;
301
+ verify_that ! ( 1 , & matcher)
302
+ }
303
+
304
+ #[ test]
305
+ fn ref_matchers_as_inner_matcher ( ) -> Result < ( ) > {
306
+ let matcher = gt ( 1 ) ;
307
+
308
+ verify_that ! ( [ 2 , 3 , 4 , 5 ] , [ & matcher, & matcher, & matcher, & matcher] )
309
+ }
310
+ }
0 commit comments