diff --git a/crates/oxc_allocator/src/boxed.rs b/crates/oxc_allocator/src/boxed.rs index 02d6d41b443d2..e78d7191b7774 100644 --- a/crates/oxc_allocator/src/boxed.rs +++ b/crates/oxc_allocator/src/boxed.rs @@ -31,35 +31,6 @@ use crate::Allocator; /// with a [`Drop`] type. pub struct Box<'alloc, T: ?Sized>(NonNull, PhantomData<(&'alloc (), T)>); -impl Box<'_, T> { - /// Take ownership of the value stored in this [`Box`], consuming the box in - /// the process. - /// - /// # Examples - /// ``` - /// use oxc_allocator::{Allocator, Box}; - /// - /// let arena = Allocator::default(); - /// - /// // Put `5` into the arena and on the heap. - /// let boxed: Box = Box::new_in(5, &arena); - /// // Move it back to the stack. `boxed` has been consumed. - /// let i = boxed.unbox(); - /// - /// assert_eq!(i, 5); - /// ``` - #[inline] - pub fn unbox(self) -> T { - // SAFETY: - // This pointer read is safe because the reference `self.0` is - // guaranteed to be unique--not just now, but we're guaranteed it's not - // borrowed from some other reference. This in turn is because we never - // construct a `Box` with a borrowed reference, only with a fresh - // one just allocated from a Bump. - unsafe { ptr::read(self.0.as_ptr()) } - } -} - impl Box<'_, T> { /// Put a `value` into a memory arena and get back a [`Box`] with ownership /// to the allocation. @@ -97,6 +68,33 @@ impl Box<'_, T> { Self(NonNull::dangling(), PhantomData) } + + /// Take ownership of the value stored in this [`Box`], consuming the box in + /// the process. + /// + /// # Examples + /// ``` + /// use oxc_allocator::{Allocator, Box}; + /// + /// let arena = Allocator::default(); + /// + /// // Put `5` into the arena and on the heap. + /// let boxed: Box = Box::new_in(5, &arena); + /// // Move it back to the stack. `boxed` has been consumed. + /// let i = boxed.unbox(); + /// + /// assert_eq!(i, 5); + /// ``` + #[inline] + pub fn unbox(self) -> T { + // SAFETY: + // This pointer read is safe because the reference `self.0` is + // guaranteed to be unique - not just now, but we're guaranteed it's not + // borrowed from some other reference. This in turn is because we never + // construct a `Box` with a borrowed reference, only with a fresh + // one just allocated from a `Bump`. + unsafe { ptr::read(self.0.as_ptr()) } + } } impl Box<'_, T> {