Skip to content

Commit 5de56b3

Browse files
committed
Make os::change_dir() return IoResult<()>
os::change_dir() returns bool, without a meaningful error message. Change it to return IoResult<()> to indicate what IoError caused the failure. Fixes #16315. [breaking-change]
1 parent 6f422c4 commit 5de56b3

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/libstd/os.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -867,32 +867,33 @@ pub fn make_absolute(p: &Path) -> IoResult<Path> {
867867
/// use std::path::Path;
868868
///
869869
/// let root = Path::new("/");
870-
/// assert!(os::change_dir(&root));
870+
/// assert!(os::change_dir(&root).is_ok());
871871
/// println!("Successfully changed working directory to {}!", root.display());
872872
/// ```
873-
pub fn change_dir(p: &Path) -> bool {
873+
pub fn change_dir(p: &Path) -> IoResult<()> {
874874
return chdir(p);
875875

876876
#[cfg(windows)]
877-
fn chdir(p: &Path) -> bool {
878-
let p = match p.as_str() {
879-
Some(s) => {
880-
let mut p = s.utf16_units().collect::<Vec<u16>>();
881-
p.push(0);
882-
p
883-
}
884-
None => return false,
885-
};
877+
fn chdir(p: &Path) -> IoResult<()> {
878+
let mut p = p.as_str().unwrap().utf16_units().collect::<Vec<u16>>();
879+
p.push(0);
880+
886881
unsafe {
887-
libc::SetCurrentDirectoryW(p.as_ptr()) != (0 as libc::BOOL)
882+
match libc::SetCurrentDirectoryW(p.as_ptr()) != (0 as libc::BOOL) {
883+
true => Ok(()),
884+
false => Err(IoError::last_error()),
885+
}
888886
}
889887
}
890888

891889
#[cfg(unix)]
892-
fn chdir(p: &Path) -> bool {
890+
fn chdir(p: &Path) -> IoResult<()> {
893891
p.with_c_str(|buf| {
894892
unsafe {
895-
libc::chdir(buf) == (0 as c_int)
893+
match libc::chdir(buf) == (0 as c_int) {
894+
true => Ok(()),
895+
false => Err(IoError::last_error()),
896+
}
896897
}
897898
})
898899
}

src/test/run-pass/tempfile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub fn dont_double_panic() {
190190

191191
fn in_tmpdir(f: ||) {
192192
let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
193-
assert!(os::change_dir(tmpdir.path()));
193+
assert!(os::change_dir(tmpdir.path()).is_ok());
194194

195195
f();
196196
}

0 commit comments

Comments
 (0)