From 9c1844a8a73189a42be4c9fa2f9b5a975d214a5d Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sat, 11 Jan 2025 01:18:53 +0000 Subject: [PATCH] refactor(data_structures): remove `NonNull` shim (#8423) The `NonNull` shim in `oxc_data_structures` was just to emulate native APIs which only became stable in Rust 1.80.0. #8407 bumped our MSRV to 1.80.0, so now we can remove the shim and use `std::ptr::NonNull` directly. --- .../oxc_data_structures/src/stack/common.rs | 5 +- crates/oxc_data_structures/src/stack/mod.rs | 2 - .../src/stack/non_empty.rs | 3 +- .../oxc_data_structures/src/stack/non_null.rs | 109 ------------------ .../oxc_data_structures/src/stack/standard.rs | 3 +- 5 files changed, 7 insertions(+), 115 deletions(-) delete mode 100644 crates/oxc_data_structures/src/stack/non_null.rs diff --git a/crates/oxc_data_structures/src/stack/common.rs b/crates/oxc_data_structures/src/stack/common.rs index e63c27e0d7cfc..e4e4ee4470e97 100644 --- a/crates/oxc_data_structures/src/stack/common.rs +++ b/crates/oxc_data_structures/src/stack/common.rs @@ -3,12 +3,13 @@ use std::{ alloc::{self, Layout}, mem::{align_of, size_of}, - ptr, slice, + ptr::{self, NonNull}, + slice, }; use assert_unchecked::assert_unchecked; -use super::{NonNull, StackCapacity}; +use super::StackCapacity; pub trait StackCommon: StackCapacity { // Getter + setter methods defined by implementer diff --git a/crates/oxc_data_structures/src/stack/mod.rs b/crates/oxc_data_structures/src/stack/mod.rs index 9817a79fcdb9b..236184b1ea3b9 100644 --- a/crates/oxc_data_structures/src/stack/mod.rs +++ b/crates/oxc_data_structures/src/stack/mod.rs @@ -9,13 +9,11 @@ mod capacity; mod common; mod non_empty; -mod non_null; mod sparse; mod standard; use capacity::StackCapacity; use common::StackCommon; pub use non_empty::NonEmptyStack; -use non_null::NonNull; pub use sparse::SparseStack; pub use standard::Stack; diff --git a/crates/oxc_data_structures/src/stack/non_empty.rs b/crates/oxc_data_structures/src/stack/non_empty.rs index 7a8e87011e3d9..7bbe4e00f8d92 100644 --- a/crates/oxc_data_structures/src/stack/non_empty.rs +++ b/crates/oxc_data_structures/src/stack/non_empty.rs @@ -3,9 +3,10 @@ use std::{ mem::size_of, ops::{Deref, DerefMut}, + ptr::NonNull, }; -use super::{NonNull, StackCapacity, StackCommon}; +use super::{StackCapacity, StackCommon}; /// A stack which can never be empty. /// diff --git a/crates/oxc_data_structures/src/stack/non_null.rs b/crates/oxc_data_structures/src/stack/non_null.rs deleted file mode 100644 index 67015a6161694..0000000000000 --- a/crates/oxc_data_structures/src/stack/non_null.rs +++ /dev/null @@ -1,109 +0,0 @@ -#![expect(clippy::inline_always)] - -use std::{cmp::Ordering, ptr::NonNull as NativeNonNull}; - -/// Wrapper around `NonNull`, which adds methods `add`, `sub`, `offset_from`, `byte_offset_from`, -/// and `read`. -/// These methods exist on `std::ptr::NonNull`, and became stable in Rust 1.80.0, but are not yet -/// stable in our MSRV. -/// -/// These methods are much cleaner than the workarounds required in older Rust versions, -/// and make code using them easier to understand. -/// -/// Once we bump MSRV and these methods are natively supported, this type can be removed. -/// `#[expect(clippy::incompatible_msrv)]` on `_non_null_add_is_not_stable` below will trigger -/// a lint warning when that happens. -/// Then this module can be deleted, and all uses of this type can be switched to `std::ptr::NonNull`. -#[derive(Debug)] -#[repr(transparent)] -pub struct NonNull(NativeNonNull); - -impl NonNull { - #[inline(always)] - pub const unsafe fn new_unchecked(ptr: *mut T) -> Self { - Self(NativeNonNull::new_unchecked(ptr)) - } - - #[inline(always)] - pub const fn dangling() -> Self { - Self(NativeNonNull::dangling()) - } - - #[inline(always)] - pub const fn as_ptr(self) -> *mut T { - self.0.as_ptr() - } - - #[inline(always)] - pub const fn cast(self) -> NonNull { - // SAFETY: `self` is non-null, so it's still non-null after casting - unsafe { NonNull::new_unchecked(self.as_ptr().cast()) } - } - - #[inline(always)] - pub const unsafe fn add(self, count: usize) -> Self { - NonNull(NativeNonNull::new_unchecked(self.as_ptr().add(count))) - } - - #[inline(always)] - pub const unsafe fn sub(self, count: usize) -> Self { - NonNull(NativeNonNull::new_unchecked(self.as_ptr().sub(count))) - } - - #[inline(always)] - pub const unsafe fn offset_from(self, origin: Self) -> isize { - self.as_ptr().offset_from(origin.as_ptr()) - } - - #[inline(always)] - pub const unsafe fn byte_offset_from(self, origin: Self) -> isize { - self.as_ptr().byte_offset_from(origin.as_ptr()) - } - - #[inline(always)] - pub const unsafe fn as_ref<'t>(self) -> &'t T { - self.0.as_ref() - } - - #[inline(always)] - pub unsafe fn as_mut<'t>(mut self) -> &'t mut T { - self.0.as_mut() - } - - #[inline(always)] - pub unsafe fn read(self) -> T { - self.0.as_ptr().read() - } -} - -impl Copy for NonNull {} - -impl Clone for NonNull { - #[inline(always)] - fn clone(&self) -> Self { - *self - } -} - -impl Eq for NonNull {} - -impl PartialEq for NonNull { - #[inline(always)] - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } -} - -impl Ord for NonNull { - #[inline(always)] - fn cmp(&self, other: &Self) -> Ordering { - self.as_ptr().cmp(&other.as_ptr()) - } -} - -impl PartialOrd for NonNull { - #[inline(always)] - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} diff --git a/crates/oxc_data_structures/src/stack/standard.rs b/crates/oxc_data_structures/src/stack/standard.rs index 91241cef04a27..4161d95ef39e2 100644 --- a/crates/oxc_data_structures/src/stack/standard.rs +++ b/crates/oxc_data_structures/src/stack/standard.rs @@ -3,9 +3,10 @@ use std::{ mem::size_of, ops::{Deref, DerefMut}, + ptr::NonNull, }; -use super::{NonNull, StackCapacity, StackCommon}; +use super::{StackCapacity, StackCommon}; /// A simple stack. ///