Skip to content

Commit 139b930

Browse files
committed
perf: Remove unnecessary string allocations in command output handling
Replace `.to_string()` calls with `.into_owned()` to avoid double allocation when processing command stdout/stderr. This eliminates redundant String creation when `Cow<str>` is already borrowed from valid UTF-8 output.
1 parent fd7d9de commit 139b930

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/bundler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
33

44
/// A simple wrapper around the `bundle` command.
55
pub struct Bundler {
6-
pub working_dir: PathBuf,
6+
working_dir: PathBuf,
77
command_executor: Box<dyn CommandExecutor>,
88
}
99

@@ -58,15 +58,15 @@ impl Bundler {
5858
self.command_executor
5959
.execute("bundle", &full_args, &command_envs)
6060
.and_then(|output| match output.status {
61-
Some(0) => Ok(String::from_utf8_lossy(&output.stdout).to_string()),
61+
Some(0) => Ok(String::from_utf8_lossy(&output.stdout).into_owned()),
6262
Some(status) => {
63-
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
63+
let stderr = String::from_utf8_lossy(&output.stderr);
6464
Err(format!(
6565
"'bundle' command failed (status: {status})\nError: {stderr}",
6666
))
6767
}
6868
None => {
69-
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
69+
let stderr = String::from_utf8_lossy(&output.stderr);
7070
Err(format!("Failed to execute 'bundle' command: {stderr}"))
7171
}
7272
})

src/gemset.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ impl Gemset {
113113
self.command_executor
114114
.execute("gem", &full_args, command_envs)
115115
.and_then(|output| match output.status {
116-
Some(0) => Ok(String::from_utf8_lossy(&output.stdout).to_string()),
116+
Some(0) => Ok(String::from_utf8_lossy(&output.stdout).into_owned()),
117117
Some(status) => {
118-
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
118+
let stderr = String::from_utf8_lossy(&output.stderr);
119119
Err(format!(
120120
"Gem command failed (status: {status})\nError: {stderr}",
121121
))
122122
}
123123
None => {
124-
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
124+
let stderr = String::from_utf8_lossy(&output.stderr);
125125
Err(format!("Failed to execute gem command: {stderr}"))
126126
}
127127
})

0 commit comments

Comments
 (0)