Created
June 24, 2015 18:56
-
-
Save ben0x539/367db7cf3bdd6ee45d8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn with_current_dir<F, T>(path: &Path, f: F) -> std::io::Result<T> | |
where F: FnOnce() -> T { | |
let previous_dir = try!(current_dir()); | |
let rewind = || { | |
match set_current_dir(&previous_dir) { | |
Err(e) => { | |
let _ = set_current_dir("/"); | |
let _ = writeln!( | |
&mut stderr(), | |
"couldn't chdir back to {:?}: {:?}", | |
previous_dir, e); | |
} | |
_ => () | |
} | |
}; | |
try!(set_current_dir(path)); | |
let r: T = f.finally(rewind); | |
Ok(r) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment