Bug 1915021: Part 2 - Derive serde::{Serialize, Deserialize} for AnchorFunction. r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D228240
This commit is contained in:
David Shin 2024-11-13 19:25:42 +00:00
parent 33b28d4e84
commit 875e74700d
3 changed files with 44 additions and 0 deletions

View File

@ -29,6 +29,7 @@ use std::ops::Deref;
use std::{slice, str};
use style_traits::SpecifiedValueInfo;
use to_shmem::{SharedMemoryBuilder, ToShmem};
use serde::{Deserialize, Serialize};
#[macro_use]
#[allow(improper_ctypes, non_camel_case_types, missing_docs)]
@ -53,6 +54,41 @@ pub use self::namespace::{Namespace, WeakNamespace};
#[repr(C)]
pub struct Atom(NonZeroUsize);
impl Serialize for Atom {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer
{
// TODO(dshin, Bug 1929015): Optimization for static atoms is possible.
self.deref().with_str(|s| serializer.serialize_str(s))
}
}
struct AtomStrVisitor;
impl<'de> serde::de::Visitor<'de> for AtomStrVisitor {
type Value = Atom;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "A string to atomize")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Atom::from(s))
}
}
impl<'de> Deserialize<'de> for Atom {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_str(AtomStrVisitor)
}
}
/// An atom *without* a strong reference.
///
/// Only usable as `&'a WeakAtom`,

View File

@ -335,6 +335,8 @@ pub use self::GenericInset as Inset;
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
)]
#[repr(C)]
pub struct GenericAnchorFunction<Percentage, LengthPercentage> {
@ -390,6 +392,8 @@ where
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
)]
#[repr(u8)]
pub enum AnchorSideKeyword {
@ -436,6 +440,8 @@ pub enum AnchorSideKeyword {
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
)]
#[repr(C)]
pub enum AnchorSide<P> {

View File

@ -597,6 +597,8 @@ impl ToCss for CustomIdent {
ToComputedValue,
ToResolvedValue,
ToShmem,
Serialize,
Deserialize,
)]
pub struct DashedIdent(pub Atom);