Skip to content

Commit 1c81432

Browse files
authored
Optimize Error::new: Avoid unnecessary heap alloc (#823)
1 parent 803cf9c commit 1c81432

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,21 @@ pub struct Error {
157157
/// Describes the kind of error that occurred.
158158
kind: ErrorKind,
159159
/// More explanation of error that occurred.
160-
message: String,
160+
message: Cow<'static, str>,
161161
}
162162

163163
impl Error {
164-
fn new(kind: ErrorKind, message: &str) -> Error {
164+
fn new(kind: ErrorKind, message: impl Into<Cow<'static, str>>) -> Error {
165165
Error {
166-
kind: kind,
167-
message: message.to_owned(),
166+
kind,
167+
message: message.into(),
168168
}
169169
}
170170
}
171171

172172
impl From<io::Error> for Error {
173173
fn from(e: io::Error) -> Error {
174-
Error::new(ErrorKind::IOError, &format!("{}", e))
174+
Error::new(ErrorKind::IOError, format!("{}", e))
175175
}
176176
}
177177

@@ -2242,7 +2242,7 @@ impl Build {
22422242
let arch = target.split('-').nth(0).ok_or_else(|| {
22432243
Error::new(
22442244
ErrorKind::ArchitectureInvalid,
2245-
format!("Unknown architecture for {} target.", os).as_str(),
2245+
format!("Unknown architecture for {} target.", os),
22462246
)
22472247
})?;
22482248

@@ -2292,7 +2292,7 @@ impl Build {
22922292
_ => {
22932293
return Err(Error::new(
22942294
ErrorKind::ArchitectureInvalid,
2295-
format!("Unknown architecture for {} target.", os).as_str(),
2295+
format!("Unknown architecture for {} target.", os),
22962296
));
22972297
}
22982298
}
@@ -3147,7 +3147,7 @@ impl Build {
31473147
Some(s) => Ok(s),
31483148
None => Err(Error::new(
31493149
ErrorKind::EnvVarNotFound,
3150-
&format!("Environment variable {} not defined.", v.to_string()),
3150+
format!("Environment variable {} not defined.", v),
31513151
)),
31523152
}
31533153
}
@@ -3167,7 +3167,7 @@ impl Build {
31673167
Some(res) => Ok(res),
31683168
None => Err(Error::new(
31693169
ErrorKind::EnvVarNotFound,
3170-
&format!("Could not find environment variable {}.", var_base),
3170+
format!("Could not find environment variable {}.", var_base),
31713171
)),
31723172
}
31733173
}
@@ -3480,7 +3480,7 @@ fn wait_on_child(cmd: &Command, program: &str, child: &mut Child) -> Result<(),
34803480
Err(e) => {
34813481
return Err(Error::new(
34823482
ErrorKind::ToolExecError,
3483-
&format!(
3483+
format!(
34843484
"Failed to wait on spawned child process, command {:?} with args {:?}: {}.",
34853485
cmd, program, e
34863486
),
@@ -3494,7 +3494,7 @@ fn wait_on_child(cmd: &Command, program: &str, child: &mut Child) -> Result<(),
34943494
} else {
34953495
Err(Error::new(
34963496
ErrorKind::ToolExecError,
3497-
&format!(
3497+
format!(
34983498
"Command {:?} with args {:?} did not execute successfully (status code {}).",
34993499
cmd, program, status
35003500
),
@@ -3559,12 +3559,12 @@ fn spawn(cmd: &mut Command, program: &str, pipe_writer: File) -> Result<Child, E
35593559
};
35603560
Err(Error::new(
35613561
ErrorKind::ToolNotFound,
3562-
&format!("Failed to find tool. Is `{}` installed?{}", program, extra),
3562+
format!("Failed to find tool. Is `{}` installed?{}", program, extra),
35633563
))
35643564
}
35653565
Err(e) => Err(Error::new(
35663566
ErrorKind::ToolExecError,
3567-
&format!(
3567+
format!(
35683568
"Command {:?} with args {:?} failed to start: {:?}",
35693569
cmd.0, program, e
35703570
),

0 commit comments

Comments
 (0)