Skip to content

Commit

Permalink
refactor(syntax): use NonMaxU32 for IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 25, 2024
1 parent 65b8f47 commit 046db29
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
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

0 comments on commit 046db29

Please sign in to comment.