From 29bae01c222dec35d4718d483cd2fbc788588bc6 Mon Sep 17 00:00:00 2001 From: abysssol Date: Tue, 30 Jul 2024 10:42:46 -0400 Subject: [PATCH] replace `String::from_utf8()` with `std::str::from_utf8()` allocation should be avoided when possible since it's relatively slow --- ofborg/src/tasks/build.rs | 6 +++--- ofborg/src/tasks/evaluate.rs | 4 ++-- ofborg/src/tasks/evaluationfilter.rs | 2 +- ofborg/src/tasks/githubcommentfilter.rs | 2 +- ofborg/src/tasks/statscollector.rs | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ofborg/src/tasks/build.rs b/ofborg/src/tasks/build.rs index 5ff84e47..9cc659f4 100644 --- a/ofborg/src/tasks/build.rs +++ b/ofborg/src/tasks/build.rs @@ -269,7 +269,7 @@ impl notifyworker::SimpleNotifyWorker for BuildWorker { match buildjob::from(body) { Ok(job) => Ok(job), Err(err) => { - error!("{:?}", String::from_utf8(body.to_vec())); + error!("{:?}", std::str::from_utf8(body).unwrap_or("")); panic!("{err:?}"); } } @@ -453,8 +453,8 @@ mod tests { actions .position(|job| match job { worker::Action::Publish(ref body) => { - let content = String::from_utf8(body.content.clone()).unwrap(); - let text = strip_escaped_ansi(&content); + let content = std::str::from_utf8(&body.content).unwrap(); + let text = strip_escaped_ansi(content); eprintln!("{text}"); if text.contains(text_to_match) { println!(" ok"); diff --git a/ofborg/src/tasks/evaluate.rs b/ofborg/src/tasks/evaluate.rs index c91e257e..f0d0f65e 100644 --- a/ofborg/src/tasks/evaluate.rs +++ b/ofborg/src/tasks/evaluate.rs @@ -68,8 +68,8 @@ impl worker::SimpleWorker for EvaluationWorker Err(err) => { self.events.notify(Event::JobDecodeFailure); error!( - "Failed to decode message: {:?}, Err: {err:?}", - String::from_utf8(body.to_vec()) + "Failed to decode message: {}, Err: {err:?}", + std::str::from_utf8(body).unwrap_or("") ); Err("Failed to decode message".to_owned()) } diff --git a/ofborg/src/tasks/evaluationfilter.rs b/ofborg/src/tasks/evaluationfilter.rs index 8f2858c8..781e69a4 100644 --- a/ofborg/src/tasks/evaluationfilter.rs +++ b/ofborg/src/tasks/evaluationfilter.rs @@ -23,7 +23,7 @@ impl worker::SimpleWorker for EvaluationFilterWorker { Ok(event) => Ok(event), Err(err) => Err(format!( "Failed to deserialize job {err:?}: {:?}", - String::from_utf8(body.to_vec()) + std::str::from_utf8(body).unwrap_or("") )), } } diff --git a/ofborg/src/tasks/githubcommentfilter.rs b/ofborg/src/tasks/githubcommentfilter.rs index 436ea7d9..8435a293 100644 --- a/ofborg/src/tasks/githubcommentfilter.rs +++ b/ofborg/src/tasks/githubcommentfilter.rs @@ -27,7 +27,7 @@ impl worker::SimpleWorker for GitHubCommentWorker { Err(err) => { error!( "Failed to deserialize IsssueComment: {:?}", - String::from_utf8(body.to_vec()) + std::str::from_utf8(body).unwrap_or("") ); panic!("{err:?}"); } diff --git a/ofborg/src/tasks/statscollector.rs b/ofborg/src/tasks/statscollector.rs index 3303049e..0372e9a6 100644 --- a/ofborg/src/tasks/statscollector.rs +++ b/ofborg/src/tasks/statscollector.rs @@ -39,7 +39,7 @@ impl worker::SimpleWorker for StatCollectorWorker self.events.notify(stats::Event::StatCollectorBogusEvent); error!( "Failed to decode message: {:?}, Err: {err:?}", - String::from_utf8(body.to_vec()) + std::str::from_utf8(body).unwrap_or("") ); Err("Failed to decode message".to_owned()) }