Skip to content

Commit f74fe61

Browse files
authored
Optimize Build::get_out_dir: Return Cow<'_, Path> (#831)
1 parent 2d9941b commit f74fe61

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,15 +3121,18 @@ impl Build {
31213121
self.force_frame_pointer.unwrap_or_else(|| self.get_debug())
31223122
}
31233123

3124-
fn get_out_dir(&self) -> Result<PathBuf, Error> {
3124+
fn get_out_dir(&self) -> Result<Cow<'_, Path>, Error> {
31253125
match &self.out_dir {
3126-
Some(p) => Ok((**p).into()),
3127-
None => Ok(env::var_os("OUT_DIR").map(PathBuf::from).ok_or_else(|| {
3128-
Error::new(
3129-
ErrorKind::EnvVarNotFound,
3130-
"Environment variable OUT_DIR not defined.",
3131-
)
3132-
})?),
3126+
Some(p) => Ok(Cow::Borrowed(&**p)),
3127+
None => env::var_os("OUT_DIR")
3128+
.map(PathBuf::from)
3129+
.map(Cow::Owned)
3130+
.ok_or_else(|| {
3131+
Error::new(
3132+
ErrorKind::EnvVarNotFound,
3133+
"Environment variable OUT_DIR not defined.",
3134+
)
3135+
}),
31333136
}
31343137
}
31353138

0 commit comments

Comments
 (0)