X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/188b1ec1b8528e2326791feccc8077e15bd60182..e3b3bde6ba4c44e6a53deb791bea681014b3ecab:/solutions/src/counter.rs?ds=sidebyside diff --git a/solutions/src/counter.rs b/solutions/src/counter.rs index afea9d0..319058e 100644 --- a/solutions/src/counter.rs +++ b/solutions/src/counter.rs @@ -11,19 +11,19 @@ impl ConcurrentCounter { } pub fn increment(&self, by: usize) { - let mut counter = self.0.write().unwrap(); + let mut counter = self.0.write().unwrap_or_else(|e| e.into_inner()); *counter = *counter + by; } pub fn compare_and_inc(&self, test: usize, by: usize) { - let mut counter = self.0.write().unwrap(); + let mut counter = self.0.write().unwrap_or_else(|e| e.into_inner()); if *counter == test { *counter += by; } } pub fn get(&self) -> usize { - let counter = self.0.read().unwrap(); + let counter = self.0.read().unwrap_or_else(|e| e.into_inner()); *counter } }