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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Budgeting module for Runestick.
//!
//! This module contains methods which allows for limiting the execution of the
//! virtual machine to abide by the specified budget.
//!
//! By default the budget is disabled, but can be enabled by wrapping your
//! function call in [with].

use pin_project::pin_project;
use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

thread_local!(static BUDGET: Cell<usize> = Cell::new(usize::max_value()));

/// Wrap the given value with a budget.
///
/// The value can either be a function, after which you can use [Budget::call],
/// or it can be a [Future] which can be polled.
pub fn with<T>(budget: usize, value: T) -> Budget<T> {
    Budget { budget, value }
}

/// Take a ticket from the budget, indicating with `true` if the budget is
/// maintained
#[inline(never)]
pub fn take() -> bool {
    BUDGET.with(|tls| {
        let v = tls.get();

        if v == usize::max_value() {
            true
        } else if v == 0 {
            false
        } else {
            tls.set(v - 1);
            true
        }
    })
}

#[repr(transparent)]
struct BudgetGuard(usize);

impl Drop for BudgetGuard {
    fn drop(&mut self) {
        BUDGET.with(|tls| {
            tls.set(self.0);
        });
    }
}

/// A budgeted future.
#[pin_project]
pub struct Budget<T> {
    /// The current budget.
    budget: usize,
    /// The future being budgeted.
    #[pin]
    value: T,
}

impl<T, O> Budget<T>
where
    T: FnOnce() -> O,
{
    /// Call the wrapped function.
    pub fn call(self) -> O {
        BUDGET.with(|tls| {
            let _guard = BudgetGuard(tls.get());
            tls.set(self.budget);
            (self.value)()
        })
    }
}

impl<T> Future for Budget<T>
where
    T: Future,
{
    type Output = T::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        BUDGET.with(|tls| {
            let _guard = BudgetGuard(tls.get());
            tls.set(*this.budget);
            let poll = this.value.poll(cx);
            *this.budget = tls.get();
            poll
        })
    }
}