From d96f70b49d24d5c61f8fd47b58d5ae28cb156ffe Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Fri, 29 Sep 2023 19:01:59 -0700 Subject: [PATCH] NFC: DRY in budget. --- src/budget.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/budget.rs b/src/budget.rs index 4585b9b2..266548d3 100644 --- a/src/budget.rs +++ b/src/budget.rs @@ -23,23 +23,26 @@ pub(super) struct Budget { impl Budget { #[inline] pub fn consume_signature(&mut self) -> Result<(), InternalError> { - self.signatures = self - .signatures - .checked_sub(1) - .ok_or(InternalError::MaximumSignatureChecksExceeded)?; - Ok(()) + checked_sub( + &mut self.signatures, + InternalError::MaximumSignatureChecksExceeded, + ) } #[inline] pub fn consume_build_chain_call(&mut self) -> Result<(), InternalError> { - self.build_chain_calls = self - .build_chain_calls - .checked_sub(1) - .ok_or(InternalError::MaximumPathBuildCallsExceeded)?; - Ok(()) + checked_sub( + &mut self.build_chain_calls, + InternalError::MaximumPathBuildCallsExceeded, + ) } } +fn checked_sub(value: &mut usize, underflow_error: InternalError) -> Result<(), InternalError> { + *value = value.checked_sub(1).ok_or(underflow_error)?; + Ok(()) +} + impl Default for Budget { fn default() -> Self { Self {