Skip to content

Commit 3eb17ab

Browse files
authored
Prevent cloning on every get_xxx() call (#793)
1 parent 06c1289 commit 3eb17ab

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/lib.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#![allow(deprecated)]
5757
#![deny(missing_docs)]
5858

59+
use std::borrow::Cow;
5960
use std::collections::{hash_map, HashMap};
6061
use std::env;
6162
use std::ffi::{OsStr, OsString};
@@ -3040,24 +3041,24 @@ impl Build {
30403041
prefixes.first().map(|prefix| *prefix))
30413042
}
30423043

3043-
fn get_target(&self) -> Result<String, Error> {
3044-
match self.target {
3045-
Some(ref t) => Ok((*t).to_string()),
3046-
None => Ok(self.getenv_unwrap("TARGET")?),
3044+
fn get_target(&self) -> Result<Cow<str>, Error> {
3045+
match &self.target {
3046+
Some(t) => Ok(Cow::Borrowed(&t)),
3047+
None => Ok(Cow::Owned(self.getenv_unwrap("TARGET")?)),
30473048
}
30483049
}
30493050

3050-
fn get_host(&self) -> Result<String, Error> {
3051+
fn get_host(&self) -> Result<Cow<str>, Error> {
30513052
match &self.host {
3052-
Some(h) => Ok((**h).to_string()),
3053-
None => Ok(self.getenv_unwrap("HOST")?),
3053+
Some(h) => Ok(Cow::Borrowed(&h)),
3054+
None => Ok(Cow::Owned(self.getenv_unwrap("HOST")?)),
30543055
}
30553056
}
30563057

3057-
fn get_opt_level(&self) -> Result<String, Error> {
3058+
fn get_opt_level(&self) -> Result<Cow<str>, Error> {
30583059
match &self.opt_level {
3059-
Some(ol) => Ok((**ol).to_string()),
3060-
None => Ok(self.getenv_unwrap("OPT_LEVEL")?),
3060+
Some(ol) => Ok(Cow::Borrowed(&ol)),
3061+
None => Ok(Cow::Owned(self.getenv_unwrap("OPT_LEVEL")?)),
30613062
}
30623063
}
30633064

0 commit comments

Comments
 (0)