X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/188b1ec1b8528e2326791feccc8077e15bd60182..7fdd4203f19f2fa9096d0a662acf22d447e57af1:/solutions/src/counter.rs?ds=inline 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 } }