Skip to content

Commit

Permalink
Optimize Build::get_out_dir: Return Cow<'_, Path>
Browse files Browse the repository at this point in the history
to avoid heap allocation if possible.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Jul 21, 2023
1 parent c83f5ee commit 6032a67
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3121,15 +3121,18 @@ impl Build {
self.force_frame_pointer.unwrap_or_else(|| self.get_debug())
}

fn get_out_dir(&self) -> Result<PathBuf, Error> {
fn get_out_dir(&self) -> Result<Cow<'_, Path>, Error> {
match &self.out_dir {
Some(p) => Ok((**p).into()),
None => Ok(env::var_os("OUT_DIR").map(PathBuf::from).ok_or_else(|| {
Error::new(
ErrorKind::EnvVarNotFound,
"Environment variable OUT_DIR not defined.",
)
})?),
Some(p) => Ok(Cow::Borrowed(&**p)),
None => env::var_os("OUT_DIR")
.map(PathBuf::from)
.map(Cow::Owned)
.ok_or_else(|| {
Error::new(
ErrorKind::EnvVarNotFound,
"Environment variable OUT_DIR not defined.",
)
}),
}
}

Expand Down

0 comments on commit 6032a67

Please sign in to comment.