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
222
223
224
225
226
227
228
229
230
231
232
233
//! Resource-tracked builder for `String` values.
//!
//! `StringBuilder` is the canonical way to build a Python-visible string whose
//! final size is *not* already bounded by an already-tracked input. Operations
//! that grow a `String` in a loop — padding methods (`ljust`, `center`, …),
//! tab expansion, string repetition, container `repr()`, etc. — must use
//! `StringBuilder` rather than `String::with_capacity(...).push(...)`, because
//! the intermediate `String` lives on the Rust heap *outside* the
//! [`ResourceTracker`]. Without a builder, a malicious script can amplify a
//! small tracked input into a multi-gigabyte intermediate before the final
//! [`allocate_string`](crate::types::str::allocate_string) ever consults the
//! tracker — bypassing the configured memory limit and OOMing the host.
//!
//! # Active reservation, not preview
//!
//! Each growth actively *reserves* bytes with the tracker via
//! [`ResourceTracker::on_grow`]. This matters because Monty allows nested
//! operations: a [`str.join`](crate::types::str) over arbitrary objects can
//! invoke user-defined `__str__`/`__repr__` methods, which may themselves
//! build strings. A preview-only check (`check_estimated_size`) would let the
//! inner build pass against the *committed* memory state while ignoring the
//! outer builder's in-progress buffer — so the two could collectively exceed
//! the configured memory limit. By reserving instead, the outer builder's
//! bytes are visible to every nested operation, and the limit applies
//! cumulatively. Reservations are released on drop (cleanup on `?` /
//! early-return paths) or in [`finish`](StringBuilder::finish), which folds
//! the handoff to [`allocate_string`](crate::types::str::allocate_string)
//! into a single method so the final size is re-added via `on_allocate`
//! without double-counting and without exposing the brief release window to
//! callers.
//!
//! # Growth policy
//!
//! Capacity doubles on each growth (matching `Vec`'s policy), so an `n`-byte
//! build incurs `O(log n)` tracker calls rather than `O(n)`. Use
//! [`with_capacity`](StringBuilder::with_capacity) when an upper bound is
//! known up front (e.g. padding to a width) — a single reservation covers
//! every subsequent push. Use [`new`](StringBuilder::new) when the size is
//! not bounded up front.
//!
//! # Two APIs: direct push and `fmt::Write`
//!
//! Callers that build strings imperatively use [`push`](StringBuilder::push)
//! and [`push_str`](StringBuilder::push_str), which return [`ResourceError`]
//! directly. Callers that need to plug into `fmt::Write`-based machinery
//! (`write!`, `format_args!`, the existing `py_repr_fmt` recursion) use the
//! builder's [`fmt::Write`] impl, which captures any [`ResourceError`] into
//! an internal slot since `fmt::Error` is payload-free. The stored error is
//! surfaced automatically by [`finish`](StringBuilder::finish), so the
//! tracker error reaches the caller even when the intermediate
//! `fmt::Error` is swallowed by a downstream formatter.
use ;
use crate::;
/// Resource-tracked builder for a `String`.
///
/// Holds an inner `String`, a tracker reference, and the byte count currently
/// reserved with the tracker. Growth calls [`ResourceTracker::on_grow`] to
/// reserve additional bytes (which fails fast if the memory limit would be
/// exceeded), and [`Drop`] / [`finish`](Self::finish) release the reservation
/// via [`ResourceTracker::on_free`].
///
/// Typical use:
///
/// ```ignore
/// let mut builder = StringBuilder::with_capacity(cap, vm.heap.tracker())?;
/// builder.push_str(prefix)?;
/// for _ in 0..pad { builder.push(fill)?; }
/// builder.finish(vm.heap)
/// ```
/// `fmt::Write` impl so `write!(builder, ...)` and `format_args!` work
/// against any tracker-protected builder. A tracker rejection is converted
/// into the payload-free [`fmt::Error`] and stashed in `pending_error`;
/// short-circuits subsequent writes so a partially-built string doesn't keep
/// accruing reservations after the limit has been hit.