Skip to content

Commit 2d50873

Browse files
committed
Auto merge of #1278 - JOE1994:cow, r=RalfJung
make 'fn convert_path_separator' to take 'Cow<>' as argument Fixed `fn convert_path_separator()` to take `Cow<>` as argument, in order to prevent unnecessary allocation when target & host path separators are equal.
2 parents 3504d52 + ed1305d commit 2d50873

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/shims/os_str.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ enum Pathconversion {
2121

2222
/// Perform path separator conversion if needed.
2323
fn convert_path_separator<'a>(
24-
os_str: &'a OsStr,
24+
os_str: Cow<'a, OsStr>,
2525
target_os: &str,
2626
direction: Pathconversion,
2727
) -> Cow<'a, OsStr> {
2828
#[cfg(windows)]
2929
return if target_os == "windows" {
3030
// Windows-on-Windows, all fine.
31-
Cow::Borrowed(os_str)
31+
os_str
3232
} else {
3333
// Unix target, Windows host.
3434
let (from, to) = match direction {
@@ -56,7 +56,7 @@ fn convert_path_separator<'a>(
5656
Cow::Owned(OsString::from_vec(converted))
5757
} else {
5858
// Unix-on-Unix, all is fine.
59-
Cow::Borrowed(os_str)
59+
os_str
6060
};
6161
}
6262

@@ -226,7 +226,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
226226
let this = self.eval_context_ref();
227227
let os_str = this.read_os_str_from_c_str(scalar)?;
228228

229-
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, Pathconversion::TargetToHost) {
229+
Ok(match convert_path_separator(Cow::Borrowed(os_str), &this.tcx.sess.target.target.target_os, Pathconversion::TargetToHost) {
230230
Cow::Borrowed(x) => Cow::Borrowed(Path::new(x)),
231231
Cow::Owned(y) => Cow::Owned(PathBuf::from(y)),
232232
})
@@ -237,7 +237,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
237237
let this = self.eval_context_ref();
238238
let os_str = this.read_os_str_from_wide_str(scalar)?;
239239

240-
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, Pathconversion::TargetToHost)))
240+
Ok(convert_path_separator(Cow::Owned(os_str), &this.tcx.sess.target.target.target_os, Pathconversion::TargetToHost).into_owned().into())
241241
}
242242

243243
/// Write a Path to the machine memory (as a null-terminated sequence of bytes),
@@ -249,7 +249,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
249249
size: u64,
250250
) -> InterpResult<'tcx, (bool, u64)> {
251251
let this = self.eval_context_mut();
252-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, Pathconversion::HostToTarget);
252+
let os_str = convert_path_separator(Cow::Borrowed(path.as_os_str()), &this.tcx.sess.target.target.target_os, Pathconversion::HostToTarget);
253253
this.write_os_str_to_c_str(&os_str, scalar, size)
254254
}
255255

@@ -262,7 +262,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
262262
size: u64,
263263
) -> InterpResult<'tcx, (bool, u64)> {
264264
let this = self.eval_context_mut();
265-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, Pathconversion::HostToTarget);
265+
let os_str = convert_path_separator(Cow::Borrowed(path.as_os_str()), &this.tcx.sess.target.target.target_os, Pathconversion::HostToTarget);
266266
this.write_os_str_to_wide_str(&os_str, scalar, size)
267267
}
268268
}

0 commit comments

Comments
 (0)