diff --git a/crates/oxc_data_structures/src/stack/standard.rs b/crates/oxc_data_structures/src/stack/standard.rs index 4ba98af48456b..f77753e8418b6 100644 --- a/crates/oxc_data_structures/src/stack/standard.rs +++ b/crates/oxc_data_structures/src/stack/standard.rs @@ -334,6 +334,25 @@ impl Stack { unsafe { self.cursor.read() } } + /// Clear the stack, removing all values. + /// + /// Note that this method has no effect on the allocated capacity + /// of the stack. + #[inline] + pub fn clear(&mut self) { + if self.is_empty() { + return; + } + + debug_assert!(self.end > self.start); + + // SAFETY: Checked above that stack is not empty, so stack is allocated. + // Stack contains `self.len()` initialized entries, starting at `self.start` + unsafe { self.drop_contents() }; + // Move cursor back to start. This is equivalent to setting len to 0 + self.cursor = self.start; + } + /// Get number of entries on stack. #[inline] pub fn len(&self) -> usize { @@ -588,6 +607,20 @@ mod tests { assert_len_cap_last!(stack, 2, 4, Some(&22)); } + #[test] + fn clear() { + let mut stack = Stack::::new(); + assert_len_cap_last!(stack, 0, 0, None); + + stack.clear(); + assert_len_cap_last!(stack, 0, 0, None); + + stack.push(10); + assert_len_cap_last!(stack, 1, 4, Some(&10)); + stack.clear(); + assert_len_cap_last!(stack, 0, 4, None); + } + #[test] #[expect(clippy::items_after_statements)] fn drop() {