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.
This commit is contained in:
overlookmotel committed 2025-02-18 13:46:56 +00:00
1 parent 4a92839bf7
commit be271646f6
2 files changed
+10 -8

No files matched your search

+8 -3
View File
@@ -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<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.serialize(serializer)
}
}
+2 -5
View File
@@ -224,11 +224,8 @@ impl fmt::Display for CompactStr {
#[cfg(feature = "serialize")]
impl Serialize for CompactStr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_str().serialize(serializer)
}
}