Skip to content

Commit

Permalink
feat(data_structures): add SparseStack::last_mut method
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 28, 2024
1 parent 0f337e5 commit 5203c82
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/oxc_data_structures/src/stack/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ impl<T> SparseStack<T> {
}
}

/// Get value of last entry on the stack.
#[inline]
pub fn last_mut(&mut self) -> Option<&mut T> {
let has_value = *self.has_values.last();
if has_value {
debug_assert!(!self.values.is_empty());
// SAFETY: Last `self.has_values` is only `true` if there's a corresponding value in `self.values`.
// This invariant is maintained in `push`, `pop`, `take_last`, `last_or_init`, and `last_mut_or_init`.
let value = unsafe { self.values.last_mut_unchecked() };
Some(value)
} else {
None
}
}

/// Take value from last entry on the stack, leaving last entry empty.
#[inline]
pub fn take_last(&mut self) -> Option<T> {
Expand Down

0 comments on commit 5203c82

Please sign in to comment.