refactor(span): manually derive Deserialize for CompactStr (#9249)

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.
This commit is contained in:
overlookmotel committed 2025-02-20 01:15:58 +00:00
1 parent 0fa92cfadb
commit 007c8574a6
2 files changed
+10 -3

No files matched your search

+1 -1
View File
@@ -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 = []
+9 -2
View File
@@ -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;
@@ -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 {
@@ -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 {