Skip to content

Commit

Permalink
refactor(span): manually derive Deserialize for CompactStr (#9249)
Browse files Browse the repository at this point in the history
The `Deserialize` impl for `CompactStr` is trivial, so write it manually, rather than using `serde`'s heavy proc macro machinery.

This allows disabling the `derive` feature for `serde` in `oxc_span` crate. Many other crates  depend on `oxc_span`, so this chips away at compile times.
  • Loading branch information
overlookmotel committed Feb 20, 2025
1 parent 0fa92cf commit 007c857
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_span/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ compact_str = { workspace = true }
miette = { workspace = true }

schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
serde = { workspace = true, optional = true }

[features]
default = []
Expand Down
11 changes: 9 additions & 2 deletions crates/oxc_span/src/compact_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use compact_str::CompactString;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::Span;

Expand All @@ -21,7 +21,6 @@ pub const MAX_INLINE_LEN: usize = 16;
/// Currently implemented as just a wrapper around [`compact_str::CompactString`],
/// but will be reduced in size with a custom implementation later.
#[derive(Clone, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serialize", derive(serde::Deserialize))]
pub struct CompactStr(CompactString);

impl CompactStr {
Expand Down Expand Up @@ -229,6 +228,14 @@ impl Serialize for CompactStr {
}
}

#[cfg(feature = "serialize")]
impl<'de> Deserialize<'de> for CompactStr {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let compact_string = CompactString::deserialize(deserializer)?;
Ok(Self(compact_string))
}
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for CompactStr {
fn is_referenceable() -> bool {
Expand Down

0 comments on commit 007c857

Please sign in to comment.