// Copyright 2014 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use crate::{Atom, StaticAtomSet}; #[cfg(feature = "serde_support")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::borrow::Cow; use std::fmt; impl ::precomputed_hash::PrecomputedHash for Atom { fn precomputed_hash(&self) -> u32 { self.get_hash() } } impl<'a, Static: StaticAtomSet> From<&'a Atom> for Atom { fn from(atom: &'a Self) -> Self { atom.clone() } } impl PartialEq for Atom { fn eq(&self, other: &str) -> bool { &self[..] == other } } impl PartialEq> for str { fn eq(&self, other: &Atom) -> bool { self == &other[..] } } impl PartialEq for Atom { fn eq(&self, other: &String) -> bool { self[..] == other[..] } } impl<'a, Static: StaticAtomSet> From<&'a str> for Atom { #[inline] fn from(string_to_add: &str) -> Self { Atom::from(Cow::Borrowed(string_to_add)) } } impl From for Atom { #[inline] fn from(string_to_add: String) -> Self { Atom::from(Cow::Owned(string_to_add)) } } impl fmt::Display for Atom { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { ::fmt(self, f) } } impl AsRef for Atom { fn as_ref(&self) -> &str { self } } #[cfg(feature = "serde_support")] impl Serialize for Atom { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let string: &str = self.as_ref(); string.serialize(serializer) } } #[cfg(feature = "serde_support")] impl<'a, Static: StaticAtomSet> Deserialize<'a> for Atom { fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { use serde::de; use std::marker::PhantomData; struct AtomVisitor(PhantomData); impl<'de, Static: StaticAtomSet> de::Visitor<'de> for AtomVisitor { type Value = Atom; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "an Atom") } fn visit_str(self, v: &str) -> Result where E: de::Error, { Ok(Atom::from(v)) } fn visit_string(self, v: String) -> Result where E: de::Error, { Ok(Atom::from(v)) } } deserializer.deserialize_str(AtomVisitor(PhantomData)) } }