Nothing to see here, move along meow
0

Configure Feed

Select the types of activity you want to include in your feed.

at main 1.1 kB View raw
1#[derive(Debug, Clone, Copy, PartialEq, Eq)] 2pub struct SchedBudget { 3 pub budget_us: u64, 4 pub period_us: u64, 5 pub remaining_us: u64, 6 pub replenish_at: Option<u64>, 7} 8 9impl SchedBudget { 10 pub const fn new(budget_us: u64, period_us: u64) -> Self { 11 Self { 12 budget_us, 13 period_us, 14 remaining_us: budget_us, 15 replenish_at: None, 16 } 17 } 18 19 pub fn consume(&mut self, elapsed_us: u64, now_us: u64) { 20 self.remaining_us = self.remaining_us.saturating_sub(elapsed_us); 21 if self.remaining_us == 0 && self.replenish_at.is_none() && self.period_us > 0 { 22 self.replenish_at = Some(now_us.saturating_add(self.period_us)); 23 } 24 } 25 26 pub fn replenish(&mut self, now_us: u64) { 27 if let Some(at) = self.replenish_at 28 && now_us >= at 29 && self.period_us > 0 30 { 31 self.remaining_us = self.budget_us; 32 self.replenish_at = Some(now_us.saturating_add(self.period_us)); 33 } 34 } 35 36 pub const fn is_exhausted(&self) -> bool { 37 self.remaining_us == 0 38 } 39}