Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(syntax): use NonMaxU32 for IDs #4467

Merged
merged 1 commit into from
Jul 26, 2024
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ lazy_static = "1.4.0"
memoffset = "0.9.1"
miette = { version = "7.2.0", features = ["fancy-no-syscall"] }
mimalloc = "0.1.42"
nonmax = "0.5.5"
num-bigint = "0.4.5"
num-traits = "0.2.19"
phf = "0.11.2"
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bitflags = { workspace = true }
rustc-hash = { workspace = true }
dashmap = { workspace = true }
phf = { workspace = true, features = ["macros"] }
nonmax = { workspace = true }

ryu-js = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
Expand Down
24 changes: 15 additions & 9 deletions crates/oxc_syntax/src/reference.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
use std::num::NonZeroU32;

use bitflags::bitflags;
use nonmax::NonMaxU32;
#[cfg(feature = "serialize")]
use serde::Serialize;
use serde::{Serialize, Serializer};

use oxc_index::Idx;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct ReferenceId(NonZeroU32);
pub struct ReferenceId(NonMaxU32);

impl Idx for ReferenceId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
// NB: We can't use `NonZeroU32::new_unchecked(idx as u32 + 1)`
// because if `idx == u32::MAX`, `+ 1` would make it wrap around back to 0
Self(NonZeroU32::new(idx as u32 + 1).unwrap())
Self(NonMaxU32::new(idx as u32).unwrap())
}

fn index(self) -> usize {
self.0.get() as usize - 1
self.0.get() as usize
}
}

#[cfg(feature = "serialize")]
impl Serialize for ReferenceId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u32(self.0.get())
}
}

Expand Down
43 changes: 28 additions & 15 deletions crates/oxc_syntax/src/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
use std::num::NonZeroU32;

use bitflags::bitflags;
use nonmax::NonMaxU32;
#[cfg(feature = "serialize")]
use serde::Serialize;
use serde::{Serialize, Serializer};

use oxc_index::Idx;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct SymbolId(NonZeroU32);
pub struct SymbolId(NonMaxU32);

impl Idx for SymbolId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
// NB: We can't use `NonZeroU32::new_unchecked(idx as u32 + 1)`
// because if `idx == u32::MAX`, `+ 1` would make it wrap around back to 0
Self(NonZeroU32::new(idx as u32 + 1).unwrap())
Self(NonMaxU32::new(idx as u32).unwrap())
}

fn index(self) -> usize {
self.0.get() as usize - 1
self.0.get() as usize
}
}

#[cfg(feature = "serialize")]
impl Serialize for SymbolId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u32(self.0.get())
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct RedeclarationId(NonZeroU32);
pub struct RedeclarationId(NonMaxU32);

impl Idx for RedeclarationId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
// NB: We can't use `NonZeroU32::new_unchecked(idx as u32 + 1)`
// because if `idx == u32::MAX`, `+ 1` would make it wrap around back to 0
Self(NonZeroU32::new(idx as u32 + 1).unwrap())
Self(NonMaxU32::new(idx as u32).unwrap())
}

fn index(self) -> usize {
self.0.get() as usize - 1
self.0.get() as usize
}
}

#[cfg(feature = "serialize")]
impl Serialize for RedeclarationId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u32(self.0.get())
}
}

Expand Down
Loading