]> git.lizzy.rs Git - rust.git/commitdiff
Simplify chdir implementation and minimize unsafe block
authorJosh Triplett <josh@joshtriplett.org>
Thu, 29 Apr 2021 20:11:20 +0000 (13:11 -0700)
committerJosh Triplett <josh@joshtriplett.org>
Thu, 29 Apr 2021 20:11:20 +0000 (13:11 -0700)
library/std/src/sys/unix/os.rs

index 984c08c2ad53171d5741f1ea4b923a7a9490ac32..51c3e5d175cca86a0b9c351c19a6ccf459378b1d 100644 (file)
@@ -155,12 +155,10 @@ pub fn getcwd() -> io::Result<PathBuf> {
 pub fn chdir(p: &path::Path) -> io::Result<()> {
     let p: &OsStr = p.as_ref();
     let p = CString::new(p.as_bytes())?;
-    unsafe {
-        match libc::chdir(p.as_ptr()) == (0 as c_int) {
-            true => Ok(()),
-            false => Err(io::Error::last_os_error()),
-        }
+    if unsafe { libc::chdir(p.as_ptr()) } != 0 {
+        return Err(io::Error::last_os_error());
     }
+    Ok(())
 }
 
 pub struct SplitPaths<'a> {