future_queue/
global_weight.rs1#[derive(Debug)]
6pub(crate) struct GlobalWeight {
7 max: usize,
8 current: usize,
9}
10
11impl GlobalWeight {
12 pub(crate) fn new(max: usize) -> Self {
13 Self { max, current: 0 }
14 }
15
16 #[inline]
17 pub(crate) fn max(&self) -> usize {
18 self.max
19 }
20
21 #[inline]
22 pub(crate) fn current(&self) -> usize {
23 self.current
24 }
25
26 #[inline]
27 pub(crate) fn has_space_for(&self, weight: usize) -> bool {
28 let weight = weight.min(self.max);
29 self.current <= self.max - weight
30 }
31
32 pub(crate) fn add_weight(&mut self, weight: usize) {
33 let weight = weight.min(self.max);
34 self.current = self.current.checked_add(weight).unwrap_or_else(|| {
35 panic!(
36 "future_queue_grouped: added weight {} to current {}, overflowed",
37 weight, self.current,
38 )
39 });
40 }
41
42 pub(crate) fn sub_weight(&mut self, weight: usize) {
43 let weight = weight.min(self.max);
44 self.current = self.current.checked_sub(weight).unwrap_or_else(|| {
45 panic!(
46 "future_queue_grouped: subtracted weight {} from current {}, overflowed",
47 weight, self.current,
48 )
49 });
50 }
51}