Skip to content

Commit 7e0cc83

Browse files
committed
fix 'magic boolean' to enum
1 parent fe9ecb5 commit 7e0cc83

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/shims/os_str.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
179179
let this = self.eval_context_ref();
180180
let os_str: &'a OsStr = this.read_os_str_from_c_str(scalar)?;
181181

182-
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, false) {
182+
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, PathConversionDirection::TargetToHost) {
183183
Cow::Borrowed(x) => Cow::Borrowed(Path::new(x)),
184184
Cow::Owned(y) => Cow::Owned(PathBuf::from(y)),
185185
})
@@ -190,7 +190,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
190190
let this = self.eval_context_ref();
191191
let os_str: OsString = this.read_os_str_from_wide_str(scalar)?;
192192

193-
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, false)))
193+
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, PathConversionDirection::TargetToHost)))
194194
}
195195

196196
/// 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
202202
size: u64,
203203
) -> InterpResult<'tcx, (bool, u64)> {
204204
let this = self.eval_context_mut();
205-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, true);
205+
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, PathConversionDirection::HostToTarget);
206206
this.write_os_str_to_c_str(&os_str, scalar, size)
207207
}
208208

@@ -215,26 +215,32 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
215215
size: u64,
216216
) -> InterpResult<'tcx, (bool, u64)> {
217217
let this = self.eval_context_mut();
218-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, true);
218+
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, PathConversionDirection::HostToTarget);
219219
this.write_os_str_to_wide_str(&os_str, scalar, size)
220220
}
221221
}
222222

223+
enum PathConversionDirection {
224+
HostToTarget,
225+
TargetToHost,
226+
}
227+
223228
/// Perform path separator conversion if needed.
224-
/// if direction == true, Convert from 'host' to 'target'.
225-
/// if direction == false, Convert from 'target' to 'host'.
226229
fn convert_path_separator<'a>(
227230
os_str: &'a OsStr,
228231
target_os: &str,
229-
direction: bool,
232+
direction: PathConversionDirection,
230233
) -> Cow<'a, OsStr> {
231234
#[cfg(windows)]
232235
return if target_os == "windows" {
233236
// Windows-on-Windows, all fine.
234237
Cow::Borrowed(os_str)
235238
} else {
236239
// Unix target, Windows host.
237-
let (from, to) = if direction { ('\\', '/') } else { ('/', '\\') };
240+
let (from, to) = match direction {
241+
PathConversionDirection::HostToTarget => ('\\', '/'),
242+
PathConversionDirection::TargetToHost => ('/', '\\'),
243+
};
238244
let converted = os_str
239245
.encode_wide()
240246
.map(|wchar| if wchar == from as u16 { to as u16 } else { wchar })
@@ -244,7 +250,10 @@ fn convert_path_separator<'a>(
244250
#[cfg(unix)]
245251
return if target_os == "windows" {
246252
// Windows target, Unix host.
247-
let (from, to) = if direction { ('/', '\\') } else { ('\\', '/') };
253+
let (from, to) = match direction {
254+
PathConversionDirection::HostToTarget => ('/', '\\'),
255+
PathConversionDirection::TargetToHost => ('\\', '/'),
256+
};
248257
let converted = os_str
249258
.as_bytes()
250259
.iter()

0 commit comments

Comments
 (0)