From be271646f6ab3353fe7cbe97d4053b9298f4124b Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:46:56 +0000 Subject: [PATCH] refactor(span): implement `Serialize` manually for `Atom` (#9196) The `Serialize` impl for `Atom` is trivial, so there's no need to use `#[derive(Serialize)]` on it - just implement it by hand. This is another step towards removing `serde-derive` from `oxc_span` and `oxc_ast`, which when complete will greatly improve compile times. Also shorten the code for `impl Serialize for CompactStr`. That change is pure style refactor. --- crates/oxc_span/src/atom.rs | 11 ++++++++--- crates/oxc_span/src/compact_str.rs | 7 ++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/oxc_span/src/atom.rs b/crates/oxc_span/src/atom.rs index c01218bafc4cf..afaef900fa437 100644 --- a/crates/oxc_span/src/atom.rs +++ b/crates/oxc_span/src/atom.rs @@ -6,7 +6,7 @@ use std::{ use oxc_allocator::{Allocator, CloneIn, FromIn}; #[cfg(feature = "serialize")] -use serde::Serialize; +use serde::{Serialize, Serializer}; use crate::{CompactStr, ContentEq}; @@ -15,8 +15,6 @@ use crate::{CompactStr, ContentEq}; /// Use [CompactStr] with [Atom::to_compact_str] or [Atom::into_compact_str] for /// the lifetimeless form. #[derive(Clone, Copy, Eq)] -#[cfg_attr(feature = "serialize", derive(Serialize))] -#[cfg_attr(feature = "serialize", serde(transparent))] pub struct Atom<'a>(&'a str); impl Atom<'static> { @@ -207,3 +205,10 @@ impl fmt::Display for Atom<'_> { fmt::Display::fmt(self.as_str(), f) } } + +#[cfg(feature = "serialize")] +impl Serialize for Atom<'_> { + fn serialize(&self, serializer: S) -> Result { + self.0.serialize(serializer) + } +} diff --git a/crates/oxc_span/src/compact_str.rs b/crates/oxc_span/src/compact_str.rs index 10752a2b7f30f..c783ec302d3f2 100644 --- a/crates/oxc_span/src/compact_str.rs +++ b/crates/oxc_span/src/compact_str.rs @@ -224,11 +224,8 @@ impl fmt::Display for CompactStr { #[cfg(feature = "serialize")] impl Serialize for CompactStr { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(self.as_str()) + fn serialize(&self, serializer: S) -> Result { + self.as_str().serialize(serializer) } }