#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FlowWindow {
size: i64,
limit: i64,
}
impl FlowWindow {
#[inline]
pub const fn new(initial: i64, limit: i64) -> Self {
Self {
size: initial,
limit,
}
}
#[inline]
pub const fn available(&self) -> i64 {
self.size
}
#[inline]
pub fn consume(&mut self, n: i64) {
self.size = self.size.saturating_sub(n);
}
pub fn increase(&mut self, n: u32) -> bool {
let next = self.size.saturating_add(n as i64);
if next > self.limit {
return false;
}
self.size = next;
true
}
#[inline]
pub fn release(&mut self, n: i64) {
self.size = self.size.saturating_add(n).min(self.limit);
}
}