use crate::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Budget {
pub max_steps: u64,
pub max_cells: usize,
}
impl Budget {
pub const UNLIMITED: Self = Self {
max_steps: u64::MAX,
max_cells: usize::MAX,
};
#[cfg_attr(
not(feature = "alloc"),
expect(dead_code, reason = "used by alloc algorithms")
)]
pub(crate) const fn meter(&self) -> Meter {
Meter {
remaining: self.max_steps,
}
}
#[cfg_attr(
not(feature = "alloc"),
expect(dead_code, reason = "used by alloc algorithms")
)]
pub(crate) const fn check_cells(&self, cells: usize) -> Result<(), Error> {
if cells > self.max_cells {
return Err(Error::MemoryBudgetExceeded);
}
Ok(())
}
}
#[derive(Debug)]
#[cfg_attr(
not(feature = "alloc"),
expect(dead_code, reason = "used by alloc algorithms")
)]
pub(crate) struct Meter {
remaining: u64,
}
impl Meter {
#[cfg_attr(
not(feature = "alloc"),
expect(dead_code, reason = "used by alloc algorithms")
)]
pub(crate) fn spend(&mut self, steps: u64) -> Result<(), Error> {
self.remaining = self
.remaining
.checked_sub(steps)
.ok_or(Error::BudgetExhausted)?;
Ok(())
}
}