NFC: DRY in budget.

This commit is contained in:
Brian Smith 2023-09-29 19:01:59 -07:00
parent 7e0731a016
commit d96f70b49d
1 changed files with 13 additions and 10 deletions

View File

@ -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 {