From 6032a677095a8b28e0a0778f996510c8abea3200 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Fri, 21 Jul 2023 13:45:16 +1000 Subject: [PATCH] Optimize `Build::get_out_dir`: Return `Cow<'_, Path>` to avoid heap allocation if possible. Signed-off-by: Jiahao XU --- src/lib.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0f7fccaa..1d104f29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3121,15 +3121,18 @@ impl Build { self.force_frame_pointer.unwrap_or_else(|| self.get_debug()) } - fn get_out_dir(&self) -> Result { + fn get_out_dir(&self) -> Result, 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.", + ) + }), } }