Skip to content

Commit eeed5de

Browse files
committed
refactor(allocator, formatter): use std crate over core (#15472)
Pure refactor. Oxc is not no-std, and we use `std` crate everywhere. But a few references to `core` crate existed. Improve consistency by using `std` instead.
1 parent 19086c4 commit eeed5de

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

crates/oxc_allocator/src/vec2/mod.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,23 @@
9292
clippy::undocumented_unsafe_blocks
9393
)]
9494

95-
use core::borrow::{Borrow, BorrowMut};
96-
use core::cmp::Ordering;
97-
use core::fmt;
98-
use core::hash::{self, Hash};
99-
use core::iter::FusedIterator;
100-
use core::marker::PhantomData;
101-
use core::mem;
102-
use core::ops;
103-
use core::ops::Bound::{Excluded, Included, Unbounded};
104-
use core::ops::{Index, IndexMut, RangeBounds};
105-
use core::ptr;
106-
use core::ptr::NonNull;
107-
use core::slice;
95+
use std::{
96+
borrow::{Borrow, BorrowMut},
97+
cmp::Ordering,
98+
fmt,
99+
hash::{self, Hash},
100+
hint,
101+
iter::FusedIterator,
102+
marker::PhantomData,
103+
mem,
104+
ops::{
105+
self,
106+
Bound::{Excluded, Included, Unbounded},
107+
Index, IndexMut, RangeBounds,
108+
},
109+
ptr::{self, NonNull},
110+
slice::{self, SliceIndex},
111+
};
108112

109113
// #[cfg(feature = "std")]
110114
// use std::io;
@@ -1031,9 +1035,12 @@ impl<'a, T: 'a, A: Alloc> Vec<'a, T, A> {
10311035
// We shadow the slice method of the same name to avoid going through
10321036
// `deref`, which creates an intermediate reference.
10331037
let ptr = self.buf.ptr();
1038+
// Note: We could use `assert_unchecked!` here, but that would introduce a debug assertion
1039+
// for a trivially satisfied invariant.
1040+
// This is a hot path, so that could measurably slow down debug builds.
10341041
unsafe {
10351042
if ptr.is_null() {
1036-
core::hint::unreachable_unchecked();
1043+
hint::unreachable_unchecked();
10371044
}
10381045
}
10391046
ptr
@@ -1073,9 +1080,12 @@ impl<'a, T: 'a, A: Alloc> Vec<'a, T, A> {
10731080
// We shadow the slice method of the same name to avoid going through
10741081
// `deref_mut`, which creates an intermediate reference.
10751082
let ptr = self.buf.ptr();
1083+
// Note: We could use `assert_unchecked!` here, but that would introduce a debug assertion
1084+
// for a trivially satisfied invariant.
1085+
// This is a hot path, so that could measurably slow down debug builds.
10761086
unsafe {
10771087
if ptr.is_null() {
1078-
core::hint::unreachable_unchecked();
1088+
hint::unreachable_unchecked();
10791089
}
10801090
}
10811091
ptr
@@ -2057,10 +2067,7 @@ impl<'a, T: 'a + Hash, A: Alloc> Hash for Vec<'a, T, A> {
20572067
}
20582068
}
20592069

2060-
impl<T, A: Alloc, I> Index<I> for Vec<'_, T, A>
2061-
where
2062-
I: ::core::slice::SliceIndex<[T]>,
2063-
{
2070+
impl<T, A: Alloc, I: SliceIndex<[T]>> Index<I> for Vec<'_, T, A> {
20642071
type Output = I::Output;
20652072

20662073
#[inline]
@@ -2069,10 +2076,7 @@ where
20692076
}
20702077
}
20712078

2072-
impl<T, A: Alloc, I> IndexMut<I> for Vec<'_, T, A>
2073-
where
2074-
I: ::core::slice::SliceIndex<[T]>,
2075-
{
2079+
impl<T, A: Alloc, I: SliceIndex<[T]>> IndexMut<I> for Vec<'_, T, A> {
20762080
#[inline]
20772081
fn index_mut(&mut self, index: I) -> &mut Self::Output {
20782082
IndexMut::index_mut(&mut **self, index)

crates/oxc_allocator/src/vec2/raw_vec.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
#![allow(unstable_name_collisions)]
2424
#![allow(dead_code)]
2525

26-
use core::alloc::Layout;
27-
use core::cmp;
28-
use core::ptr::{self, NonNull};
26+
use std::{
27+
alloc::Layout,
28+
cmp,
29+
ptr::{self, NonNull},
30+
};
2931

3032
use crate::alloc::Alloc;
3133

@@ -676,7 +678,7 @@ impl<'a, T> RawVec<'a, T> {
676678
use crate::boxed::Box;
677679
678680
// NOTE: not calling `cap()` here; actually using the real `cap` field!
679-
let slice = core::slice::from_raw_parts_mut(self.ptr(), self.cap);
681+
let slice = std::slice::from_raw_parts_mut(self.ptr(), self.cap);
680682
let output: Box<'a, [T]> = Box::from_raw(slice);
681683
mem::forget(self);
682684
output
@@ -839,7 +841,7 @@ impl<T, A: Alloc> RawVec<'_, T, A> {
839841
#[inline]
840842
fn alloc_guard(alloc_size: usize) -> Result<(), AllocError> {
841843
if size_of::<usize>() < 8 {
842-
if alloc_size > ::core::isize::MAX as usize {
844+
if alloc_size > isize::MAX as usize {
843845
return Err(AllocError::CapacityOverflow);
844846
}
845847
} else if alloc_size > u32::MAX as usize {

crates/oxc_formatter/src/ast_nodes/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use core::fmt;
21
use std::{
2+
fmt,
33
mem::{transmute, transmute_copy},
44
ops::Deref,
55
};

crates/oxc_formatter/src/parentheses/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl NeedsParentheses<'_> for AstNode<'_, IdentifierReference<'_>> {
125125
}
126126

127127
// Early return if the parent isn't a `TSSatisfiesExpression` or `TSAsExpression`
128-
if core::ptr::eq(self.parent, parent) {
128+
if ptr::eq(self.parent, parent) {
129129
return false;
130130
}
131131

crates/oxc_formatter/src/utils/jsx.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use std::iter::{FusedIterator, Peekable};
2-
use std::str::Chars;
1+
use std::{
2+
iter::{FusedIterator, Peekable},
3+
mem,
4+
str::Chars,
5+
};
36

47
use oxc_allocator::Vec as ArenaVec;
58
use oxc_ast::ast::*;
@@ -215,7 +218,7 @@ impl PartialEq for JsxChild<'_, '_> {
215218
match (self, other) {
216219
(Self::Word(l0), Self::Word(r0)) => l0 == r0,
217220
(Self::NonText(_), Self::NonText(_)) => false, // Never equal by structural comparison
218-
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
221+
_ => mem::discriminant(self) == mem::discriminant(other),
219222
}
220223
}
221224
}

0 commit comments

Comments
 (0)