string_cache/
trivial_impls.rs

1// Copyright 2014 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use crate::{Atom, StaticAtomSet};
11#[cfg(feature = "serde_support")]
12use serde::{Deserialize, Deserializer, Serialize, Serializer};
13use std::borrow::Cow;
14use std::fmt;
15
16impl<Static: StaticAtomSet> ::precomputed_hash::PrecomputedHash for Atom<Static> {
17    fn precomputed_hash(&self) -> u32 {
18        self.get_hash()
19    }
20}
21
22impl<'a, Static: StaticAtomSet> From<&'a Atom<Static>> for Atom<Static> {
23    fn from(atom: &'a Self) -> Self {
24        atom.clone()
25    }
26}
27
28impl<Static: StaticAtomSet> PartialEq<str> for Atom<Static> {
29    fn eq(&self, other: &str) -> bool {
30        &self[..] == other
31    }
32}
33
34impl<Static: StaticAtomSet> PartialEq<Atom<Static>> for str {
35    fn eq(&self, other: &Atom<Static>) -> bool {
36        self == &other[..]
37    }
38}
39
40impl<Static: StaticAtomSet> PartialEq<String> for Atom<Static> {
41    fn eq(&self, other: &String) -> bool {
42        self[..] == other[..]
43    }
44}
45
46impl<'a, Static: StaticAtomSet> From<&'a str> for Atom<Static> {
47    #[inline]
48    fn from(string_to_add: &str) -> Self {
49        Atom::from(Cow::Borrowed(string_to_add))
50    }
51}
52
53impl<Static: StaticAtomSet> From<String> for Atom<Static> {
54    #[inline]
55    fn from(string_to_add: String) -> Self {
56        Atom::from(Cow::Owned(string_to_add))
57    }
58}
59
60impl<Static: StaticAtomSet> fmt::Display for Atom<Static> {
61    #[inline]
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        <str as fmt::Display>::fmt(self, f)
64    }
65}
66
67impl<Static: StaticAtomSet> AsRef<str> for Atom<Static> {
68    fn as_ref(&self) -> &str {
69        self
70    }
71}
72
73#[cfg(feature = "serde_support")]
74impl<Static: StaticAtomSet> Serialize for Atom<Static> {
75    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
76    where
77        S: Serializer,
78    {
79        let string: &str = self.as_ref();
80        string.serialize(serializer)
81    }
82}
83
84#[cfg(feature = "serde_support")]
85impl<'a, Static: StaticAtomSet> Deserialize<'a> for Atom<Static> {
86    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
87    where
88        D: Deserializer<'a>,
89    {
90        use serde::de;
91        use std::marker::PhantomData;
92
93        struct AtomVisitor<Static: StaticAtomSet>(PhantomData<Static>);
94
95        impl<'de, Static: StaticAtomSet> de::Visitor<'de> for AtomVisitor<Static> {
96            type Value = Atom<Static>;
97
98            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
99                write!(formatter, "an Atom")
100            }
101
102            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
103            where
104                E: de::Error,
105            {
106                Ok(Atom::from(v))
107            }
108
109            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
110            where
111                E: de::Error,
112            {
113                Ok(Atom::from(v))
114            }
115        }
116
117        deserializer.deserialize_str(AtomVisitor(PhantomData))
118    }
119}