Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// TODO: research if there's a better way
#![cfg(feature = "std")]
use super::{Meter, Weight};
use std::cell::RefCell;
thread_local! {
static METER: RefCell<Meter> = RefCell::new(Meter {
used_weight: 0,
depth: 0,
});
}
/// Start weight meter with base weight
pub fn start() {
METER.with(|v| {
let mut meter = v.borrow_mut();
if meter.depth == 0 {
meter.used_weight = 0;
}
meter.depth = meter.depth.saturating_add(1);
});
}
/// Increment used weight
pub fn using(weight: Weight) {
METER.with(|v| {
let mut meter = v.borrow_mut();
meter.used_weight = meter.used_weight.saturating_add(weight);
})
}
/// Finish weight meter
pub fn finish() {
METER.with(|v| {
let mut meter = v.borrow_mut();
meter.depth = meter.depth.saturating_sub(1);
})
}
/// Get used weight
pub fn used_weight() -> Weight {
METER.with(|v| v.borrow().used_weight)
}