Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion crates/oxc_data_structures/src/stack/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{
alloc::{self, Layout},
mem::{align_of, size_of},
ptr,
ptr, slice,
};

use assert_unchecked::assert_unchecked;
Expand Down Expand Up @@ -186,6 +186,20 @@ pub trait StackCommon<T>: StackCapacity<T> {
self.end().byte_offset_from(self.start()) as usize
}
}

/// Get contents of stack as a slice `&[T]`.
#[inline]
fn as_slice(&self) -> &[T] {
// SAFETY: Stack always contains `self.len()` entries, starting at `self.start()`
unsafe { slice::from_raw_parts(self.start().as_ptr(), self.len()) }
}

/// Get contents of stack as a mutable slice `&mut [T]`.
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] {
// SAFETY: Stack always contains `self.len()` entries, starting at `self.start()`
unsafe { slice::from_raw_parts_mut(self.start().as_ptr(), self.len()) }
}
}

/// Make allocation of with provided layout.
Expand Down
33 changes: 32 additions & 1 deletion crates/oxc_data_structures/src/stack/non_empty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![expect(clippy::unnecessary_safety_comment)]

use std::mem::size_of;
use std::{
mem::size_of,
ops::{Deref, DerefMut},
};

use super::{NonNull, StackCapacity, StackCommon};

Expand Down Expand Up @@ -301,6 +304,18 @@ impl<T> NonEmptyStack<T> {
pub fn capacity(&self) -> usize {
<Self as StackCommon<T>>::capacity(self)
}

/// Get contents of stack as a slice `&[T]`.
#[inline]
pub fn as_slice(&self) -> &[T] {
<Self as StackCommon<T>>::as_slice(self)
}

/// Get contents of stack as a mutable slice `&mut [T]`.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
<Self as StackCommon<T>>::as_mut_slice(self)
}
}

impl<T> Drop for NonEmptyStack<T> {
Expand All @@ -316,6 +331,22 @@ impl<T> Drop for NonEmptyStack<T> {
}
}

impl<T> Deref for NonEmptyStack<T> {
type Target = [T];

#[inline]
fn deref(&self) -> &[T] {
self.as_slice()
}
}

impl<T> DerefMut for NonEmptyStack<T> {
#[inline]
fn deref_mut(&mut self) -> &mut [T] {
self.as_mut_slice()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
33 changes: 32 additions & 1 deletion crates/oxc_data_structures/src/stack/standard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![expect(clippy::unnecessary_safety_comment)]

use std::mem::size_of;
use std::{
mem::size_of,
ops::{Deref, DerefMut},
};

use super::{NonNull, StackCapacity, StackCommon};

Expand Down Expand Up @@ -324,6 +327,18 @@ impl<T> Stack<T> {
pub fn capacity(&self) -> usize {
<Self as StackCommon<T>>::capacity(self)
}

/// Get contents of stack as a slice `&[T]`.
#[inline]
pub fn as_slice(&self) -> &[T] {
<Self as StackCommon<T>>::as_slice(self)
}

/// Get contents of stack as a mutable slice `&mut [T]`.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
<Self as StackCommon<T>>::as_mut_slice(self)
}
}

impl<T> Drop for Stack<T> {
Expand All @@ -345,6 +360,22 @@ impl<T> Drop for Stack<T> {
}
}

impl<T> Deref for Stack<T> {
type Target = [T];

#[inline]
fn deref(&self) -> &[T] {
self.as_slice()
}
}

impl<T> DerefMut for Stack<T> {
#[inline]
fn deref_mut(&mut self) -> &mut [T] {
self.as_mut_slice()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down