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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#[cfg(feature = "devtools")]
use super::*;
/// Zero-initialized empty breadcrumb slot helpers.
#[cfg(feature = "devtools")]
impl InlineBreadcrumbSlot {
/// Returns a zeroed empty breadcrumb slot.
///
/// # Returns
///
/// - `Self` - Empty inline breadcrumb slot.
#[must_use]
pub(crate) const fn empty() -> Self {
Self {
seq: 0,
frame_seq: 0,
kind_len: 0,
kind_buf: [0; PANIC_MAX_KIND_BYTES],
detail_len: 0,
detail_buf: [0; PANIC_MAX_DETAIL_BYTES],
}
}
/// Writes `kind` and `detail` into this slot in-place without any heap allocation.
///
/// # Arguments
///
/// - `&Self` - Mutable breadcrumb slot.
/// - `u64` - Monotonic breadcrumb sequence number.
/// - `u64` - Active frame index.
/// - `&str` - Event kind label.
/// - `&str` - Event detail label.
pub(crate) fn write_in_place(&mut self, seq: u64, frame_seq: u64, kind: &str, detail: &str) {
let Self {
seq: slot_seq,
frame_seq: slot_frame,
kind_len,
kind_buf,
detail_len,
detail_buf,
} = self;
*slot_seq = seq;
*slot_frame = frame_seq;
*kind_len = copy_utf8_truncated(kind, kind_buf) as u8;
*detail_len = copy_utf8_truncated(detail, detail_buf) as u8;
}
/// Materializes an owned `RuntimeEventBreadcrumb` from this inline slot.
///
/// # Arguments
///
/// - `Self` - Inline breadcrumb slot copied by value.
///
/// # Returns
///
/// - `RuntimeEventBreadcrumb` - Owned serializable breadcrumb entry.
#[must_use]
pub(crate) fn to_owned_breadcrumb(self) -> RuntimeEventBreadcrumb {
let Self {
seq,
frame_seq,
kind_len,
kind_buf,
detail_len,
detail_buf,
} = self;
let kind_str: String =
from_utf8(&kind_buf[..(kind_len as usize).min(PANIC_MAX_KIND_BYTES)])
.unwrap_or("")
.to_string();
let detail_str: String =
from_utf8(&detail_buf[..(detail_len as usize).min(PANIC_MAX_DETAIL_BYTES)])
.unwrap_or("")
.to_string();
RuntimeEventBreadcrumb::new(seq, kind_str, detail_str, frame_seq)
}
}
/// Zero-allocation runtime context and ring buffer operations.
#[cfg(feature = "devtools")]
impl InlineRuntimeContextState {
/// Creates an initialized runtime context slot with canonical default route and theme labels.
///
/// # Returns
///
/// - `Self` - Fresh inline runtime context state.
#[must_use]
pub(crate) fn new_default() -> Self {
let mut route_buf: [u8; PANIC_MAX_ROUTE_BYTES] = [0; PANIC_MAX_ROUTE_BYTES];
let route_len: usize = copy_utf8_truncated(PANIC_DEFAULT_ROUTE, &mut route_buf);
let mut theme_buf: [u8; PANIC_MAX_THEME_BYTES] = [0; PANIC_MAX_THEME_BYTES];
let theme_len: usize = copy_utf8_truncated(PANIC_DEFAULT_THEME, &mut theme_buf);
Self {
route_len,
route_buf,
theme_len,
theme_buf,
overlays_len: 0,
overlays_buf: [0; PANIC_MAX_OVERLAYS_BYTES],
viewport_width: 0.0,
viewport_height: 0.0,
scale_factor: 1.0,
frame_seq: 0,
is_idle: false,
next_seq: 1,
write_cursor: 0,
count: 0,
breadcrumbs: [InlineBreadcrumbSlot::empty(); PANIC_BREADCRUMB_RING_CAPACITY],
}
}
/// Appends one breadcrumb into the fixed-capacity ring buffer with zero heap allocation.
///
/// # Arguments
///
/// - `&Self` - Mutable runtime context state.
/// - `&str` - Event kind label.
/// - `&str` - Event detail label.
pub(crate) fn push_breadcrumb(&mut self, kind: &str, detail: &str) {
let Self {
frame_seq,
next_seq,
write_cursor,
count,
breadcrumbs,
..
} = self;
let seq: u64 = *next_seq;
*next_seq = seq.saturating_add(1);
let idx: usize = *write_cursor;
breadcrumbs[idx].write_in_place(seq, *frame_seq, kind, detail);
*write_cursor = (idx + 1) % PANIC_BREADCRUMB_RING_CAPACITY;
if *count < PANIC_BREADCRUMB_RING_CAPACITY {
*count += 1;
}
}
/// Exports recent breadcrumbs in strict chronological order (oldest to newest).
///
/// # Arguments
///
/// - `&Self` - Borrowed runtime context state.
///
/// # Returns
///
/// - `Vec<RuntimeEventBreadcrumb>` - Chronological owned breadcrumb entries.
#[must_use]
pub(crate) fn snapshot_breadcrumbs(&self) -> Vec<RuntimeEventBreadcrumb> {
let Self {
write_cursor,
count,
breadcrumbs,
..
} = self;
let total: usize = *count;
let mut out: Vec<RuntimeEventBreadcrumb> = Vec::with_capacity(total);
let start: usize = if total == PANIC_BREADCRUMB_RING_CAPACITY {
*write_cursor
} else {
0
};
for offset in 0..total {
let slot_idx: usize = (start + offset) % PANIC_BREADCRUMB_RING_CAPACITY;
out.push(breadcrumbs[slot_idx].to_owned_breadcrumb());
}
out
}
/// Materializes a complete owned `PanicRuntimeContext` snapshot from this inline state.
///
/// # Arguments
///
/// - `&Self` - Borrowed runtime context state.
///
/// # Returns
///
/// - `PanicRuntimeContext` - Complete serializable runtime context snapshot.
#[must_use]
pub(crate) fn to_owned_context(&self) -> PanicRuntimeContext {
let Self {
route_len,
route_buf,
theme_len,
theme_buf,
overlays_len,
overlays_buf,
viewport_width,
viewport_height,
scale_factor,
frame_seq,
is_idle,
..
} = self;
let active_route: String = from_utf8(&route_buf[..(*route_len).min(PANIC_MAX_ROUTE_BYTES)])
.unwrap_or(PANIC_DEFAULT_ROUTE)
.to_string();
let theme: String = from_utf8(&theme_buf[..(*theme_len).min(PANIC_MAX_THEME_BYTES)])
.unwrap_or(PANIC_DEFAULT_THEME)
.to_string();
let overlays_raw: &str =
from_utf8(&overlays_buf[..(*overlays_len).min(PANIC_MAX_OVERLAYS_BYTES)]).unwrap_or("");
let active_overlays: Vec<String> = overlays_raw
.split(PANIC_OVERLAY_SEPARATOR)
.map(str::trim)
.filter(|item: &&str| !item.is_empty())
.map(str::to_string)
.collect();
let recent_breadcrumbs: Vec<RuntimeEventBreadcrumb> = self.snapshot_breadcrumbs();
PanicRuntimeContext {
active_route,
viewport_width: *viewport_width,
viewport_height: *viewport_height,
scale_factor: *scale_factor,
theme,
active_overlays,
frame_seq: *frame_seq,
is_idle: *is_idle,
recent_breadcrumbs,
}
}
}