Skip to content

Commit

Permalink
Optimize Error::new: Avoid unnecessary heap alloc (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyXu authored Jul 20, 2023
1 parent 803cf9c commit 1c81432
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,21 @@ pub struct Error {
/// Describes the kind of error that occurred.
kind: ErrorKind,
/// More explanation of error that occurred.
message: String,
message: Cow<'static, str>,
}

impl Error {
fn new(kind: ErrorKind, message: &str) -> Error {
fn new(kind: ErrorKind, message: impl Into<Cow<'static, str>>) -> Error {
Error {
kind: kind,
message: message.to_owned(),
kind,
message: message.into(),
}
}
}

impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::new(ErrorKind::IOError, &format!("{}", e))
Error::new(ErrorKind::IOError, format!("{}", e))
}
}

Expand Down Expand Up @@ -2242,7 +2242,7 @@ impl Build {
let arch = target.split('-').nth(0).ok_or_else(|| {
Error::new(
ErrorKind::ArchitectureInvalid,
format!("Unknown architecture for {} target.", os).as_str(),
format!("Unknown architecture for {} target.", os),
)
})?;

Expand Down Expand Up @@ -2292,7 +2292,7 @@ impl Build {
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
format!("Unknown architecture for {} target.", os).as_str(),
format!("Unknown architecture for {} target.", os),
));
}
}
Expand Down Expand Up @@ -3147,7 +3147,7 @@ impl Build {
Some(s) => Ok(s),
None => Err(Error::new(
ErrorKind::EnvVarNotFound,
&format!("Environment variable {} not defined.", v.to_string()),
format!("Environment variable {} not defined.", v),
)),
}
}
Expand All @@ -3167,7 +3167,7 @@ impl Build {
Some(res) => Ok(res),
None => Err(Error::new(
ErrorKind::EnvVarNotFound,
&format!("Could not find environment variable {}.", var_base),
format!("Could not find environment variable {}.", var_base),
)),
}
}
Expand Down Expand Up @@ -3480,7 +3480,7 @@ fn wait_on_child(cmd: &Command, program: &str, child: &mut Child) -> Result<(),
Err(e) => {
return Err(Error::new(
ErrorKind::ToolExecError,
&format!(
format!(
"Failed to wait on spawned child process, command {:?} with args {:?}: {}.",
cmd, program, e
),
Expand All @@ -3494,7 +3494,7 @@ fn wait_on_child(cmd: &Command, program: &str, child: &mut Child) -> Result<(),
} else {
Err(Error::new(
ErrorKind::ToolExecError,
&format!(
format!(
"Command {:?} with args {:?} did not execute successfully (status code {}).",
cmd, program, status
),
Expand Down Expand Up @@ -3559,12 +3559,12 @@ fn spawn(cmd: &mut Command, program: &str, pipe_writer: File) -> Result<Child, E
};
Err(Error::new(
ErrorKind::ToolNotFound,
&format!("Failed to find tool. Is `{}` installed?{}", program, extra),
format!("Failed to find tool. Is `{}` installed?{}", program, extra),
))
}
Err(e) => Err(Error::new(
ErrorKind::ToolExecError,
&format!(
format!(
"Command {:?} with args {:?} failed to start: {:?}",
cmd.0, program, e
),
Expand Down

0 comments on commit 1c81432

Please sign in to comment.