[go: up one dir, main page]

slog/key/
dynamic.rs

1use alloc::borrow::Cow;
2use alloc::string::{String, ToString};
3use core::clone::Clone;
4use core::cmp::PartialEq;
5use core::convert::{AsRef, From};
6use core::fmt;
7use core::hash::{Hash, Hasher};
8use core::iter::{FromIterator, IntoIterator};
9
10/// Opaque Key is a representation of a key.
11///
12/// It is owned, and largely forms a contract for
13/// key to follow.
14#[derive(Clone, Eq)]
15pub struct Key {
16    data: Cow<'static, str>,
17}
18
19impl Key {
20    /// Returns the length of self.
21    pub fn len(&self) -> usize {
22        self.data.len()
23    }
24
25    /// Returns the length of self.
26    pub fn is_empty(&self) -> bool {
27        self.data.is_empty()
28    }
29
30    /// Take as a `str`
31    pub fn as_str(&self) -> &str {
32        self.data.as_ref()
33    }
34}
35impl AsRef<str> for Key {
36    #[inline]
37    fn as_ref(&self) -> &str {
38        self.data.as_ref()
39    }
40}
41
42impl Default for Key {
43    fn default() -> Key {
44        Key {
45            data: Cow::Borrowed(""),
46        }
47    }
48}
49
50impl Hash for Key {
51    fn hash<H: Hasher>(&self, state: &mut H) {
52        match self.data {
53            Cow::Borrowed(ref ptr) => ptr.hash(state),
54            Cow::Owned(ref ptr) => ptr.hash(state),
55        }
56    }
57}
58
59impl From<&'static str> for Key {
60    #[inline(always)]
61    fn from(data: &'static str) -> Key {
62        Key {
63            data: Cow::Borrowed(data),
64        }
65    }
66}
67
68impl From<String> for Key {
69    fn from(data: String) -> Key {
70        Key {
71            data: Cow::Owned(data),
72        }
73    }
74}
75
76impl From<Key> for String {
77    fn from(s: Key) -> String {
78        s.to_string()
79    }
80}
81
82impl FromIterator<char> for Key {
83    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Key {
84        Key {
85            data: Cow::Owned(iter.into_iter().collect::<String>()),
86        }
87    }
88}
89
90impl<'a> FromIterator<&'a char> for Key {
91    fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> Key {
92        Key {
93            data: Cow::Owned(iter.into_iter().collect::<String>()),
94        }
95    }
96}
97
98impl FromIterator<String> for Key {
99    fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Key {
100        Key {
101            data: Cow::Owned(iter.into_iter().collect::<String>()),
102        }
103    }
104}
105
106impl<'a> FromIterator<&'a String> for Key {
107    fn from_iter<I: IntoIterator<Item = &'a String>>(iter: I) -> Key {
108        Key {
109            data: Cow::Owned(
110                iter.into_iter().map(|x| x.as_str()).collect::<String>(),
111            ),
112        }
113    }
114}
115
116impl<'a> FromIterator<&'a str> for Key {
117    fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Key {
118        Key {
119            data: Cow::Owned(iter.into_iter().collect::<String>()),
120        }
121    }
122}
123
124impl<'a> FromIterator<Cow<'a, str>> for Key {
125    fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> Key {
126        Key {
127            data: Cow::Owned(iter.into_iter().collect::<String>()),
128        }
129    }
130}
131
132impl PartialEq<str> for Key {
133    fn eq(&self, other: &str) -> bool {
134        self.as_ref().eq(other)
135    }
136}
137
138impl PartialEq<&str> for Key {
139    fn eq(&self, other: &&str) -> bool {
140        self.as_ref().eq(*other)
141    }
142}
143
144impl PartialEq<String> for Key {
145    fn eq(&self, other: &String) -> bool {
146        self.as_ref().eq(other.as_str())
147    }
148}
149
150impl PartialEq<Self> for Key {
151    fn eq(&self, other: &Self) -> bool {
152        self.as_ref().eq(other.as_ref())
153    }
154}
155
156impl fmt::Display for Key {
157    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158        match self.data {
159            Cow::Borrowed(ref ptr) => write!(f, "{}", ptr),
160            Cow::Owned(ref ptr) => write!(f, "{}", ptr),
161        }
162    }
163}
164
165impl fmt::Debug for Key {
166    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167        match self.data {
168            Cow::Borrowed(ref ptr) => write!(f, "{:?}", ptr),
169            Cow::Owned(ref ptr) => write!(f, "{:?}", ptr),
170        }
171    }
172}