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]
This commit is contained in:
parent
6f422c4c05
commit
5de56b3ca1
2 changed files with 16 additions and 15 deletions
|
@ -867,32 +867,33 @@ pub fn make_absolute(p: &Path) -> IoResult<Path> {
|
||||||
/// use std::path::Path;
|
/// use std::path::Path;
|
||||||
///
|
///
|
||||||
/// let root = Path::new("/");
|
/// let root = Path::new("/");
|
||||||
/// assert!(os::change_dir(&root));
|
/// assert!(os::change_dir(&root).is_ok());
|
||||||
/// println!("Successfully changed working directory to {}!", root.display());
|
/// println!("Successfully changed working directory to {}!", root.display());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn change_dir(p: &Path) -> bool {
|
pub fn change_dir(p: &Path) -> IoResult<()> {
|
||||||
return chdir(p);
|
return chdir(p);
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
fn chdir(p: &Path) -> bool {
|
fn chdir(p: &Path) -> IoResult<()> {
|
||||||
let p = match p.as_str() {
|
let mut p = p.as_str().unwrap().utf16_units().collect::<Vec<u16>>();
|
||||||
Some(s) => {
|
p.push(0);
|
||||||
let mut p = s.utf16_units().collect::<Vec<u16>>();
|
|
||||||
p.push(0);
|
|
||||||
p
|
|
||||||
}
|
|
||||||
None => return false,
|
|
||||||
};
|
|
||||||
unsafe {
|
unsafe {
|
||||||
libc::SetCurrentDirectoryW(p.as_ptr()) != (0 as libc::BOOL)
|
match libc::SetCurrentDirectoryW(p.as_ptr()) != (0 as libc::BOOL) {
|
||||||
|
true => Ok(()),
|
||||||
|
false => Err(IoError::last_error()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn chdir(p: &Path) -> bool {
|
fn chdir(p: &Path) -> IoResult<()> {
|
||||||
p.with_c_str(|buf| {
|
p.with_c_str(|buf| {
|
||||||
unsafe {
|
unsafe {
|
||||||
libc::chdir(buf) == (0 as c_int)
|
match libc::chdir(buf) == (0 as c_int) {
|
||||||
|
true => Ok(()),
|
||||||
|
false => Err(IoError::last_error()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -190,7 +190,7 @@ pub fn dont_double_panic() {
|
||||||
|
|
||||||
fn in_tmpdir(f: ||) {
|
fn in_tmpdir(f: ||) {
|
||||||
let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
|
let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
|
||||||
assert!(os::change_dir(tmpdir.path()));
|
assert!(os::change_dir(tmpdir.path()).is_ok());
|
||||||
|
|
||||||
f();
|
f();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue