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
22 changes: 0 additions & 22 deletions crates/oxc_allocator/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,6 @@ impl<T> Box<'_, T> {
}

impl<T: ?Sized> Box<'_, T> {
/// Create a [`Box`] from a raw pointer to a value.
///
/// The [`Box`] takes ownership of the data pointed to by `ptr`.
///
/// # SAFETY
/// Data pointed to by `ptr` must live as long as `'alloc`.
/// This requirement is met if the pointer was obtained from other data in the arena.
///
/// Data pointed to by `ptr` must *only* be used for this `Box`. i.e. it must be unique,
/// with no other aliases. You must not, for example, create 2 `Box`es from the same pointer.
///
/// `ptr` must have been created from a `*mut T` or `&mut T` (not a `*const T` / `&T`).
//
// `#[inline(always)]` because this is a no-op
#[expect(clippy::inline_always)]
#[inline(always)]
pub(crate) const unsafe fn from_non_null(ptr: NonNull<T>) -> Self {
const { Self::ASSERT_T_IS_NOT_DROP };

Self(ptr, PhantomData)
}

/// Consume a [`Box`] and return a [`NonNull`] pointer to its contents.
//
// `#[inline(always)]` because this is a no-op
Expand Down
52 changes: 1 addition & 51 deletions crates/oxc_allocator/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{
hash::{Hash, Hasher},
mem::ManuallyDrop,
ops,
ptr::NonNull,
slice::SliceIndex,
};

Expand Down Expand Up @@ -167,40 +166,6 @@ impl<'alloc, T> Vec<'alloc, T> {
let vec = unsafe { InnerVec::from_raw_parts_in(ptr, N, N, allocator.bump()) };
Self(ManuallyDrop::new(vec))
}

/// Converts the vector into [`Box<[T]>`][owned slice].
///
/// Any excess capacity the vector has will not be included in the slice.
/// The excess memory will be leaked in the arena (i.e. not reused by another allocation).
///
/// # Examples
/// ```
/// use oxc_allocator::{Allocator, Vec};
///
/// let allocator = Allocator::default();
/// let mut v = Vec::with_capacity_in(10, &allocator);
/// v.extend([1, 2, 3]);
/// let b = v.into_boxed_slice();
///
/// assert_eq!(&*b, &[1, 2, 3]);
/// assert_eq!(b.len(), 3);
/// ```
///
/// [owned slice]: Box
#[inline]
pub fn into_boxed_slice(self) -> Box<'alloc, [T]> {
let inner = ManuallyDrop::into_inner(self.0);
let slice = inner.leak();
let ptr = NonNull::from(slice);
// SAFETY: `ptr` points to a valid slice `[T]`.
// `allocator_api2::vec::Vec::leak` consumes the inner `Vec` without dropping it.
// Lifetime of returned `Box<'alloc, [T]>` is same as lifetime of consumed `Vec<'alloc, T>`,
// so data in the `Box` must be valid for its lifetime.
// `Vec` uniquely owned the data, and we have consumed the `Vec`, so the new `Box` has
// unique ownership of the data (no aliasing).
// `ptr` was created from a `&mut [T]`.
unsafe { Box::from_non_null(ptr) }
}
}

impl<'alloc, T> ops::Deref for Vec<'alloc, T> {
Expand Down Expand Up @@ -305,7 +270,7 @@ impl<T: Debug> Debug for Vec<'_, T> {
#[cfg(test)]
mod test {
use super::Vec;
use crate::{Allocator, Box};
use crate::Allocator;

#[test]
fn vec_with_capacity() {
Expand Down Expand Up @@ -352,19 +317,4 @@ mod test {
program
}
}

#[test]
fn vec_to_boxed_slice() {
let allocator = Allocator::default();
let mut v = Vec::with_capacity_in(10, &allocator);
v.extend([1, 2, 3]);

let b = v.into_boxed_slice();
// Check return value is an `oxc_allocator::Box`, not an `allocator_api2::boxed::Box`
let b: Box<[u8]> = b;

assert_eq!(&*b, &[1, 2, 3]);
// Check length of slice is equal to what `v.len()` was, not `v.capacity()`
assert_eq!(b.len(), 3);
}
}
Loading