Skip to main content

profile_inspect/ir/
stack.rs

1use super::FrameId;
2use serde::Serialize;
3
4/// Unique identifier for a stack
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
6pub struct StackId(pub u32);
7
8/// A call stack represented as a sequence of frames
9///
10/// Frames are ordered from root (caller) to leaf (callee).
11/// The last frame is the function that was actually executing.
12#[derive(Debug, Clone, Serialize)]
13pub struct Stack {
14    /// Unique identifier for this stack
15    pub id: StackId,
16
17    /// Frame IDs from root to leaf
18    pub frames: Vec<FrameId>,
19}
20
21impl Stack {
22    /// Create a new stack with the given ID and frames
23    pub fn new(id: StackId, frames: Vec<FrameId>) -> Self {
24        Self { id, frames }
25    }
26
27    /// Get the leaf (executing) frame, if any
28    pub fn leaf(&self) -> Option<FrameId> {
29        self.frames.last().copied()
30    }
31
32    /// Get the root frame, if any
33    pub fn root(&self) -> Option<FrameId> {
34        self.frames.first().copied()
35    }
36
37    /// Check if this stack contains the given frame
38    pub fn contains(&self, frame: FrameId) -> bool {
39        self.frames.contains(&frame)
40    }
41
42    /// Get the depth of the stack (number of frames)
43    pub fn depth(&self) -> usize {
44        self.frames.len()
45    }
46
47    /// Check if the stack is empty
48    pub fn is_empty(&self) -> bool {
49        self.frames.is_empty()
50    }
51}