Skip to main content

bootstrap/core/builder/
step_stack.rs

1use std::cell::RefCell;
2
3thread_local! {
4    static STEP_STACK: RefCell<StepStack> = const { RefCell::new(StepStack::new()) };
5}
6
7/// This type serves for recording the stack of executed steps in a thread-local variable.
8///
9/// It is used to print the currently running step stack in places where we do not have easy
10/// access to the currently used builder, e.g. in exit! macros or the panic handler.
11pub struct StepStack {
12    stack: Vec<StepRecord>,
13}
14
15pub struct StepRecord {
16    pub info: String,
17    pub location: String,
18}
19
20impl StepStack {
21    /// Return the currently active step stack for this thread.
22    pub fn with_current<F>(func: F)
23    where
24        F: FnOnce(&mut StepStack),
25    {
26        STEP_STACK.with(|stack| func(&mut stack.borrow_mut()));
27    }
28
29    const fn new() -> Self {
30        Self { stack: Vec::new() }
31    }
32
33    pub fn get_active_steps(&self) -> impl Iterator<Item = &StepRecord> {
34        self.stack.iter()
35    }
36
37    pub fn clear(&mut self) {
38        self.stack.clear();
39    }
40
41    pub fn push(&mut self, record: StepRecord) {
42        self.stack.push(record);
43    }
44
45    pub fn pop(&mut self) {
46        self.stack.pop();
47    }
48}