@@ -17,7 +17,7 @@ use crate::{
17
17
matcher:: { Matcher , MatcherBase , MatcherResult } ,
18
18
} ;
19
19
use num_traits:: { Float , FloatConst } ;
20
- use std:: fmt:: Debug ;
20
+ use std:: { borrow :: Borrow , fmt:: Debug } ;
21
21
22
22
/// Matches a value equal within `max_abs_error` of `expected`.
23
23
///
@@ -168,13 +168,13 @@ impl<T: Debug> NearMatcher<T> {
168
168
}
169
169
}
170
170
171
- impl < T : Debug + Float + Copy > Matcher < T > for NearMatcher < T > {
171
+ impl < T : Borrow < F > + Debug + Copy , F : Debug + Float > Matcher < T > for NearMatcher < F > {
172
172
fn matches ( & self , actual : T ) -> MatcherResult {
173
- if self . nans_are_equal && self . expected . is_nan ( ) && actual. is_nan ( ) {
173
+ if self . nans_are_equal && self . expected . is_nan ( ) && actual. borrow ( ) . is_nan ( ) {
174
174
return MatcherResult :: Match ;
175
175
}
176
176
177
- let delta = actual - self . expected ;
177
+ let delta = * actual. borrow ( ) - self . expected ;
178
178
if delta >= -self . max_abs_error && delta <= self . max_abs_error {
179
179
MatcherResult :: Match
180
180
} else {
@@ -347,4 +347,17 @@ mod tests {
347
347
fn approx_eq_does_not_match_distant_number ( ) -> Result < ( ) > {
348
348
verify_that ! ( 0.0f64 , not( approx_eq( 1.0f64 ) ) )
349
349
}
350
+
351
+ #[ test]
352
+ fn approx_eq_supports_ref ( ) -> Result < ( ) > {
353
+ verify_that ! ( & 0.0f64 , approx_eq( 0.0f64 ) )
354
+ }
355
+
356
+ #[ test]
357
+ fn approx_eq_supports_container_matchers ( ) -> Result < ( ) > {
358
+ verify_that ! (
359
+ vec![ 1. , 2. , 3. ] ,
360
+ unordered_elements_are![ approx_eq( 2. ) , approx_eq( 3. ) , approx_eq( 1. ) ]
361
+ )
362
+ }
350
363
}
0 commit comments