Skip to content

Commit 63421a6

Browse files
gribozavrcopybara-github
authored andcommitted
Eliminate an unnecessary memory allocation in is_utf8_string
PiperOrigin-RevId: 657966491
1 parent 89655d0 commit 63421a6

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

googletest/src/matchers/is_encoded_string_matcher.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ where
6666
InnerMatcherT: for<'a> Matcher<&'a str>,
6767
{
6868
fn matches(&self, actual: ActualT) -> MatcherResult {
69-
String::from_utf8(actual.as_ref().to_vec())
70-
.map(|s| self.inner.matches(&s))
69+
std::str::from_utf8(actual.as_ref())
70+
.map(|s| self.inner.matches(s))
7171
.unwrap_or(MatcherResult::NoMatch)
7272
}
7373

@@ -87,9 +87,9 @@ where
8787
}
8888

8989
fn explain_match(&self, actual: ActualT) -> Description {
90-
match String::from_utf8(actual.as_ref().to_vec()) {
90+
match std::str::from_utf8(actual.as_ref()) {
9191
Ok(s) => {
92-
format!("which is a UTF-8 encoded string {}", self.inner.explain_match(&s)).into()
92+
format!("which is a UTF-8 encoded string {}", self.inner.explain_match(s)).into()
9393
}
9494
Err(e) => format!("which is not a UTF-8 encoded string: {e}").into(),
9595
}

0 commit comments

Comments
 (0)