Skip to content

Commit e6a8c7f

Browse files
committed
Fix lifetime problems by using String.
This fixes the lifetime problems in `IsEncodedStringMatcher` at the cost of an extra allocation: the input must be copied to a `Vec<u8>`, then converted to a `String`.
1 parent 3c8fefa commit e6a8c7f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

googletest/src/matchers/is_encoded_string_matcher.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn is_utf8_string<'a, ActualT: AsRef<[u8]> + Debug + 'a, InnerMatcherT>(
4949
inner: InnerMatcherT,
5050
) -> impl Matcher<ActualT = ActualT>
5151
where
52-
InnerMatcherT: Matcher<ActualT = &'a str>,
52+
InnerMatcherT: Matcher<ActualT = String>,
5353
{
5454
IsEncodedStringMatcher { inner, phantom: Default::default() }
5555
}
@@ -62,12 +62,12 @@ struct IsEncodedStringMatcher<ActualT, InnerMatcherT> {
6262
impl<'a, ActualT: AsRef<[u8]> + Debug + 'a, InnerMatcherT> Matcher
6363
for IsEncodedStringMatcher<ActualT, InnerMatcherT>
6464
where
65-
InnerMatcherT: Matcher<ActualT = &'a str>,
65+
InnerMatcherT: Matcher<ActualT = String>,
6666
{
6767
type ActualT = ActualT;
6868

6969
fn matches(&self, actual: &Self::ActualT) -> MatcherResult {
70-
std::str::from_utf8(actual.as_ref())
70+
String::from_utf8(actual.as_ref().to_vec())
7171
.map(|s| self.inner.matches(&s))
7272
.unwrap_or(MatcherResult::NoMatch)
7373
}
@@ -86,7 +86,7 @@ where
8686
}
8787

8888
fn explain_match(&self, actual: &Self::ActualT) -> String {
89-
match std::str::from_utf8(actual.as_ref()) {
89+
match String::from_utf8(actual.as_ref().to_vec()) {
9090
Ok(s) => format!("which is a UTF-8 encoded string {}", self.inner.explain_match(&s)),
9191
Err(_) => "which is not a UTF-8 encoded string".into(),
9292
}

0 commit comments

Comments
 (0)