fix 'magic boolean' to enum

This commit is contained in:
JOE1994 2020-03-29 10:21:02 -04:00
parent fe9ecb50d1
commit 7e0cc8307e

View File

@ -179,7 +179,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_ref();
let os_str: &'a OsStr = this.read_os_str_from_c_str(scalar)?;
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, false) {
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, PathConversionDirection::TargetToHost) {
Cow::Borrowed(x) => Cow::Borrowed(Path::new(x)),
Cow::Owned(y) => Cow::Owned(PathBuf::from(y)),
})
@ -190,7 +190,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_ref();
let os_str: OsString = this.read_os_str_from_wide_str(scalar)?;
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, false)))
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, PathConversionDirection::TargetToHost)))
}
/// Write a Path to the machine memory (as a null-terminated sequence of bytes),
@ -202,7 +202,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
size: u64,
) -> InterpResult<'tcx, (bool, u64)> {
let this = self.eval_context_mut();
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, true);
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, PathConversionDirection::HostToTarget);
this.write_os_str_to_c_str(&os_str, scalar, size)
}
@ -215,18 +215,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
size: u64,
) -> InterpResult<'tcx, (bool, u64)> {
let this = self.eval_context_mut();
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, true);
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, PathConversionDirection::HostToTarget);
this.write_os_str_to_wide_str(&os_str, scalar, size)
}
}
enum PathConversionDirection {
HostToTarget,
TargetToHost,
}
/// Perform path separator conversion if needed.
/// if direction == true, Convert from 'host' to 'target'.
/// if direction == false, Convert from 'target' to 'host'.
fn convert_path_separator<'a>(
os_str: &'a OsStr,
target_os: &str,
direction: bool,
direction: PathConversionDirection,
) -> Cow<'a, OsStr> {
#[cfg(windows)]
return if target_os == "windows" {
@ -234,7 +237,10 @@ fn convert_path_separator<'a>(
Cow::Borrowed(os_str)
} else {
// Unix target, Windows host.
let (from, to) = if direction { ('\\', '/') } else { ('/', '\\') };
let (from, to) = match direction {
PathConversionDirection::HostToTarget => ('\\', '/'),
PathConversionDirection::TargetToHost => ('/', '\\'),
};
let converted = os_str
.encode_wide()
.map(|wchar| if wchar == from as u16 { to as u16 } else { wchar })
@ -244,7 +250,10 @@ fn convert_path_separator<'a>(
#[cfg(unix)]
return if target_os == "windows" {
// Windows target, Unix host.
let (from, to) = if direction { ('/', '\\') } else { ('\\', '/') };
let (from, to) = match direction {
PathConversionDirection::HostToTarget => ('/', '\\'),
PathConversionDirection::TargetToHost => ('\\', '/'),
};
let converted = os_str
.as_bytes()
.iter()