Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return errors when swap_nonatomic fails #8

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ pub fn swap_nonatomic<A, B>(a: A, b: B) -> io::Result<()> where A: AsRef<Path>,

match fs::rename(b, a) {
Ok(_) => (),
Err(_) => {
Err(e) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not using e directly, Err(e) can be matched by error or other variable name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// let's try to recover the previous state
// if it fails, there is nothing we can do
return fs::rename(&tmp, a);
fs::rename(&tmp, a)?;
return Err(e)
},
}

// rename tmp to b
match fs::rename(&tmp, b) {
Ok(_) => Ok(()),
Err(_) => {
Err(e) => {
// let's try to recover to previous state
// if it fails, there is nothing we can do
fs::rename(a, b)?;
fs::rename(&tmp, a)
fs::rename(&tmp, a)?;
Err(e)
},
}
}
Expand Down