Skip to content

Commit

Permalink
NFC: DRY in budget.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Sep 30, 2023
1 parent 7e0731a commit d96f70b
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d96f70b

Please sign in to comment.