Skip to main content

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        let hash64 = self.get_hash();
19        (hash64 >> 32) as u32 ^ hash64 as u32
20    }
21}
22
23impl<'a, Static: StaticAtomSet> From<&'a Atom<Static>> for Atom<Static> {
24    fn from(atom: &'a Self) -> Self {
25        atom.clone()
26    }
27}
28
29impl<Static: StaticAtomSet> PartialEq<str> for Atom<Static> {
30    fn eq(&self, other: &str) -> bool {
31        &self[..] == other
32    }
33}
34
35impl<Static: StaticAtomSet> PartialEq<Atom<Static>> for str {
36    fn eq(&self, other: &Atom<Static>) -> bool {
37        self == &other[..]
38    }
39}
40
41impl<Static: StaticAtomSet> PartialEq<String> for Atom<Static> {
42    fn eq(&self, other: &String) -> bool {
43        self[..] == other[..]
44    }
45}
46
47impl<Static: StaticAtomSet> From<&'_ str> for Atom<Static> {
48    #[inline]
49    fn from(string_to_add: &str) -> Self {
50        Atom::from(Cow::Borrowed(string_to_add))
51    }
52}
53
54impl<Static: StaticAtomSet> From<String> for Atom<Static> {
55    #[inline]
56    fn from(string_to_add: String) -> Self {
57        Atom::from(Cow::Owned(string_to_add))
58    }
59}
60
61impl<Static: StaticAtomSet> fmt::Display for Atom<Static> {
62    #[inline]
63    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64        <str as fmt::Display>::fmt(self, f)
65    }
66}
67
68impl<Static: StaticAtomSet> AsRef<str> for Atom<Static> {
69    #[inline]
70    fn as_ref(&self) -> &str {
71        self.as_str()
72    }
73}
74
75impl<Static: StaticAtomSet> AsRef<[u8]> for Atom<Static> {
76    #[inline]
77    fn as_ref(&self) -> &[u8] {
78        self.as_bytes()
79    }
80}
81
82#[cfg(feature = "serde_support")]
83impl<Static: StaticAtomSet> Serialize for Atom<Static> {
84    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
85    where
86        S: Serializer,
87    {
88        let string: &str = self.as_ref();
89        string.serialize(serializer)
90    }
91}
92
93#[cfg(feature = "serde_support")]
94impl<'a, Static: StaticAtomSet> Deserialize<'a> for Atom<Static> {
95    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
96    where
97        D: Deserializer<'a>,
98    {
99        use serde::de;
100        use std::marker::PhantomData;
101
102        struct AtomVisitor<Static: StaticAtomSet>(PhantomData<Static>);
103
104        impl<'de, Static: StaticAtomSet> de::Visitor<'de> for AtomVisitor<Static> {
105            type Value = Atom<Static>;
106
107            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
108                write!(formatter, "an Atom")
109            }
110
111            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
112            where
113                E: de::Error,
114            {
115                Ok(Atom::from(v))
116            }
117
118            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
119            where
120                E: de::Error,
121            {
122                Ok(Atom::from(v))
123            }
124        }
125
126        deserializer.deserialize_str(AtomVisitor(PhantomData))
127    }
128}