Skip to content
Merged
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
20 changes: 16 additions & 4 deletions crates/oxc_allocator/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ impl<T> Box<'_, T> {
/// # SAFETY
/// Safe to create, but must never be dereferenced, as does not point to a valid `T`.
/// Only purpose is for mocking types without allocating for const assertions.
#[expect(unsafe_code)]
pub const unsafe fn dangling() -> Self {
const { Self::ASSERT_T_IS_NOT_DROP };

Self(NonNull::dangling(), PhantomData)
// SAFETY: None of `from_non_null`'s invariants are satisfied, but caller promises
// never to dereference the `Box`
unsafe { Self::from_non_null(ptr::NonNull::dangling()) }
}

/// Take ownership of the value stored in this [`Box`], consuming the box in
Expand Down Expand Up @@ -110,6 +109,19 @@ impl<T: ?Sized> Box<'_, T> {
pub fn into_non_null(boxed: Self) -> NonNull<T> {
boxed.0
}

/// Create a [`Box`] from a [`NonNull`] pointer.
///
/// # SAFETY
///
/// * Pointer must point to a valid `T`.
/// * Pointer must point to within an `Allocator`.
/// * Caller must ensure that the pointer is valid for the lifetime of the `Box`.
pub const unsafe fn from_non_null(ptr: NonNull<T>) -> Self {
const { Self::ASSERT_T_IS_NOT_DROP };

Self(ptr, PhantomData)
}
}

impl<T: ?Sized> ops::Deref for Box<'_, T> {
Expand Down
Loading