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