From 007c8574a6a72efdd22d42fbca5a92c2a58da71b Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 20 Feb 2025 01:15:57 +0000 Subject: [PATCH] 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. --- crates/oxc_span/Cargo.toml | 2 +- crates/oxc_span/src/compact_str.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/oxc_span/Cargo.toml b/crates/oxc_span/Cargo.toml index 7ba6231726c6a..f0f2a77495c86 100644 --- a/crates/oxc_span/Cargo.toml +++ b/crates/oxc_span/Cargo.toml @@ -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 = [] diff --git a/crates/oxc_span/src/compact_str.rs b/crates/oxc_span/src/compact_str.rs index c783ec302d3f2..bb4337a3149c3 100644 --- a/crates/oxc_span/src/compact_str.rs +++ b/crates/oxc_span/src/compact_str.rs @@ -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>(deserializer: D) -> Result { + let compact_string = CompactString::deserialize(deserializer)?; + Ok(Self(compact_string)) + } +} + #[cfg(feature = "schemars")] impl schemars::JsonSchema for CompactStr { fn is_referenceable() -> bool {