From c1970846380efd7756e434dd0c47d419f6640ca9 Mon Sep 17 00:00:00 2001 From: Nicolas Gotchac Date: Fri, 7 Sep 2018 10:15:58 +0200 Subject: [PATCH 1/2] Return errors when `swap_nonatomic` fails --- src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 81ad989..d1549e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,21 +40,23 @@ pub fn swap_nonatomic(a: A, b: B) -> io::Result<()> where A: AsRef, match fs::rename(b, a) { Ok(_) => (), - Err(_) => { + Err(e) => { // 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) }, } } From ad6b44ff6661205f7c24dd01d0dfe4e21417b235 Mon Sep 17 00:00:00 2001 From: Nicolas Gotchac Date: Fri, 7 Sep 2018 15:50:12 +0200 Subject: [PATCH 2/2] PR Grumble --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d1549e9..204a683 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,23 +40,23 @@ pub fn swap_nonatomic(a: A, b: B) -> io::Result<()> where A: AsRef, match fs::rename(b, a) { Ok(_) => (), - Err(e) => { + error => { // let's try to recover the previous state // if it fails, there is nothing we can do fs::rename(&tmp, a)?; - return Err(e) + return error }, } // rename tmp to b match fs::rename(&tmp, b) { Ok(_) => Ok(()), - Err(e) => { + error => { // 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)?; - Err(e) + error }, } }