1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
use std::ops::{Deref, DerefMut, Index, IndexMut}; use std::hash::{Hash, Hasher}; use std::iter::FromIterator; use fnv::FnvHashMap; use serde_json::{Map, Value}; use super::{props_to_json, Prop}; const PROP_NULL: Prop = Prop::Null; #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Props(FnvHashMap<String, Prop>); unsafe impl Sync for Props {} unsafe impl Send for Props {} impl Props { #[inline(always)] pub fn new() -> Self { Props(FnvHashMap::default()) } #[inline] pub fn insert<K, V>(&mut self, key: K, value: V) where K: ToString, V: Into<Prop>, { self.0.insert(key.to_string(), value.into()); } #[inline] pub fn set<K, V>(&mut self, key: K, value: V) where K: ToString, V: Into<Prop>, { self.insert(key, value); } #[inline] pub fn remove(&mut self, key: &str) -> Option<Prop> { self.0.remove(key) } #[inline] pub fn delete(&mut self, key: &str) -> Prop { if let Some(value) = self.0.remove(key) { value } else { Prop::Null } } #[inline] pub fn has(&self, key: &str) -> bool { if let Some(prop) = self.0.get(key) { prop != &Prop::Null } else { false } } #[inline] pub fn take(&self, key: &str) -> Option<Prop> { self.0.get(key).map(Clone::clone) } #[inline] pub fn get(&self, key: &str) -> &Prop { if let Some(prop) = self.0.get(key) { prop } else { &PROP_NULL } } #[inline] pub fn get_mut(&mut self, key: &str) -> &mut Prop { self.0.entry(key.into()).or_insert(Prop::Null) } #[inline] pub fn extend<I, K, V>(&mut self, iter: I) where I: IntoIterator<Item = (K, V)>, K: ToString, V: Into<Prop>, { for (k, v) in iter.into_iter() { self.insert(k, v); } } #[inline] pub fn update<F>(&mut self, key: &str, f: F) -> &mut Self where F: Fn(&mut Prop), { self.0.get_mut(key).map(f); self } #[inline] pub fn to_json(&self) -> Value { Value::Object(props_to_json(self)) } } impl<'a> Index<&'a str> for Props { type Output = Prop; #[inline] fn index(&self, key: &'a str) -> &Self::Output { self.get(key) } } impl<'a> IndexMut<&'a str> for Props { #[inline] fn index_mut(&mut self, key: &'a str) -> &mut Self::Output { self.get_mut(key) } } impl Deref for Props { type Target = FnvHashMap<String, Prop>; #[inline(always)] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Props { #[inline(always)] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Hash for Props { #[inline] fn hash<H>(&self, state: &mut H) where H: Hasher, { for (k, v) in self { k.hash(state); v.hash(state); } } } impl IntoIterator for Props { type Item = (String, Prop); type IntoIter = ::std::collections::hash_map::IntoIter<String, Prop>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } impl<'a> IntoIterator for &'a Props { type Item = (&'a String, &'a Prop); type IntoIter = ::std::collections::hash_map::Iter<'a, String, Prop>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { self.0.iter() } } impl<'a> IntoIterator for &'a mut Props { type Item = (&'a String, &'a mut Prop); type IntoIter = ::std::collections::hash_map::IterMut<'a, String, Prop>; #[inline(always)] fn into_iter(self) -> Self::IntoIter { self.0.iter_mut() } } impl<K, V> From<FnvHashMap<K, V>> for Props where K: Eq + Hash + ToString, V: Into<Prop>, { #[inline] fn from(map: FnvHashMap<K, V>) -> Self { let mut m = Props::new(); for (k, v) in map { m.insert(k, v); } m } } impl<K, V> FromIterator<(K, V)> for Props where K: Eq + Hash + ToString, V: Into<Prop>, { #[inline] fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self { let mut m = Props::new(); for (k, v) in iter { m.insert(k, v); } m } } impl Into<Map<String, Value>> for Props { #[inline] fn into(self) -> Map<String, Value> { props_to_json(&self) } } impl<'a> Into<Map<String, Value>> for &'a Props { #[inline] fn into(self) -> Map<String, Value> { props_to_json(self) } }