Skip to content

Commit e58547a

Browse files
committed
Rename MatcherResult::into_bool to MatcheResult::is_match.
This also pulls the proper implementation from the `impl From<MatcherResult> for bool` block into `is_match` and has the former implementation defer to this method. The naming `is_match` is closer to other Rust idioms such as `Result::is_ok`, `Result::is_err`, etc. It makes more sense to define the method explicitly according to its intended behaviour and then add a `From` implementation as a convenience.
1 parent 4e81bc2 commit e58547a

File tree

6 files changed

+18
-23
lines changed

6 files changed

+18
-23
lines changed

googletest/src/matcher.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,17 @@ impl From<bool> for MatcherResult {
251251

252252
impl From<MatcherResult> for bool {
253253
fn from(matcher_result: MatcherResult) -> Self {
254-
match matcher_result {
255-
MatcherResult::Match => true,
256-
MatcherResult::NoMatch => false,
257-
}
254+
matcher_result.is_match()
258255
}
259256
}
260257

261258
impl MatcherResult {
262259
/// Returns `true` if `self` is [`MatcherResult::Match`], otherwise
263260
/// `false`.
264-
///
265-
/// This delegates to `Into<bool>` but coerce the return type to `bool`
266-
/// instead only calling `into()`. This is useful in `if
267-
/// !matcher_result.into()`, which requires a constraint on the result
268-
/// type of `into()` to compile.
269-
pub fn into_bool(self) -> bool {
270-
self.into()
261+
pub fn is_match(self) -> bool {
262+
match self {
263+
MatcherResult::Match => true,
264+
MatcherResult::NoMatch => false,
265+
}
271266
}
272267
}

googletest/src/matchers/all_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub mod internal {
109109
let failures = self
110110
.components
111111
.iter()
112-
.filter(|component| !component.matches(actual).into_bool())
112+
.filter(|component| !component.matches(actual).is_match())
113113
.map(|component| component.explain_match(actual))
114114
.collect::<Description>();
115115
if failures.len() == 1 {

googletest/src/matchers/each_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080

8181
fn matches(&self, actual: &ActualT) -> MatcherResult {
8282
for element in actual {
83-
if !self.inner.matches(element).into_bool() {
83+
if !self.inner.matches(element).is_match() {
8484
return MatcherResult::NoMatch;
8585
}
8686
}
@@ -90,7 +90,7 @@ where
9090
fn explain_match(&self, actual: &ActualT) -> String {
9191
let mut non_matching_elements = Vec::new();
9292
for (index, element) in actual.into_iter().enumerate() {
93-
if !self.inner.matches(element).into_bool() {
93+
if !self.inner.matches(element).is_match() {
9494
non_matching_elements.push((index, element, self.inner.explain_match(element)));
9595
}
9696
}

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub mod internal {
122122
fn matches(&self, actual: &ContainerT) -> MatcherResult {
123123
let mut zipped_iterator = zip(actual.into_iter(), self.elements.iter());
124124
for (a, e) in zipped_iterator.by_ref() {
125-
if !e.matches(a).into_bool() {
125+
if !e.matches(a).is_match() {
126126
return MatcherResult::NoMatch;
127127
}
128128
}
@@ -138,7 +138,7 @@ pub mod internal {
138138
let mut zipped_iterator = zip(actual_iterator, self.elements.iter());
139139
let mut mismatches = Vec::new();
140140
for (idx, (a, e)) in zipped_iterator.by_ref().enumerate() {
141-
if !e.matches(a).into_bool() {
141+
if !e.matches(a).is_match() {
142142
mismatches.push(format!("element #{idx} is {a:?}, {}", e.explain_match(a)));
143143
}
144144
}

googletest/src/matchers/pointwise_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub mod internal {
179179
fn matches(&self, actual: &ContainerT) -> MatcherResult {
180180
let mut zipped_iterator = zip(actual.into_iter(), self.matchers.iter());
181181
for (element, matcher) in zipped_iterator.by_ref() {
182-
if !matcher.matches(element).into_bool() {
182+
if !matcher.matches(element).is_match() {
183183
return MatcherResult::NoMatch;
184184
}
185185
}
@@ -198,7 +198,7 @@ pub mod internal {
198198
let mut zipped_iterator = zip(actual_iterator, self.matchers.iter());
199199
let mut mismatches = Vec::new();
200200
for (idx, (a, e)) in zipped_iterator.by_ref().enumerate() {
201-
if !e.matches(a).into_bool() {
201+
if !e.matches(a).is_match() {
202202
mismatches.push(format!("element #{idx} is {a:?}, {}", e.explain_match(a)));
203203
}
204204
}

googletest/src/matchers/unordered_elements_are_matcher.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,25 +656,25 @@ pub mod internal {
656656
// than `find_best_match()`.
657657
fn find_unmatchable_elements(&self) -> UnmatchableElements<N> {
658658
let unmatchable_actual =
659-
self.0.iter().map(|row| row.iter().all(|&e| !e.into_bool())).collect();
659+
self.0.iter().map(|row| row.iter().all(|&e| !e.is_match())).collect();
660660
let mut unmatchable_expected = [false; N];
661661
for (col_idx, expected) in unmatchable_expected.iter_mut().enumerate() {
662-
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| !e.into_bool());
662+
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| !e.is_match());
663663
}
664664
UnmatchableElements { unmatchable_actual, unmatchable_expected }
665665
}
666666

667667
fn find_unmatched_expected(&self) -> UnmatchableElements<N> {
668668
let mut unmatchable_expected = [false; N];
669669
for (col_idx, expected) in unmatchable_expected.iter_mut().enumerate() {
670-
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| !e.into_bool());
670+
*expected = self.0.iter().map(|row| row[col_idx]).all(|e| !e.is_match());
671671
}
672672
UnmatchableElements { unmatchable_actual: vec![false; N], unmatchable_expected }
673673
}
674674

675675
fn find_unmatched_actual(&self) -> UnmatchableElements<N> {
676676
let unmatchable_actual =
677-
self.0.iter().map(|row| row.iter().all(|e| !e.into_bool())).collect();
677+
self.0.iter().map(|row| row.iter().all(|e| !e.is_match())).collect();
678678
UnmatchableElements { unmatchable_actual, unmatchable_expected: [false; N] }
679679
}
680680

@@ -795,7 +795,7 @@ pub mod internal {
795795
if seen[expected_idx] {
796796
continue;
797797
}
798-
if !self.0[actual_idx][expected_idx].into_bool() {
798+
if !self.0[actual_idx][expected_idx].is_match() {
799799
continue;
800800
}
801801
// There is an edge between `actual_idx` and `expected_idx`.

0 commit comments

Comments
 (0)