use core::{
cmp::{Eq, Ordering},
hash::{Hash, Hasher},
iter::FromIterator,
ops::{Index, IndexMut},
};
use serde_derive::{Deserialize, Serialize};
usesuper::Value;
/// A [`Value`] to [`Value`] map. /// /// This structure either uses a [`BTreeMap`](alloc::collections::BTreeMap) or the /// [`IndexMap`](indexmap::IndexMap) internally. /// The latter can be used by enabling the `indexmap` feature. This can be used /// to preserve the order of the parsed map. #[derive(Clone, Debug, Default, Deserialize, Serialize)] #[serde(transparent)] pubstruct Map(pub(crate) MapInner);
#[cfg(not(feature = "indexmap"))] type MapInner = alloc::collections::BTreeMap<Value, Value>; #[cfg(feature = "indexmap")] type MapInner = indexmap::IndexMap<Value, Value, std::collections::hash_map::RandomState>;
/// Immutably looks up an element by its `key`. #[must_use] pubfn get(&self, key: &Value) -> Option<&Value> { self.0.get(key)
}
/// Mutably looks up an element by its `key`. pubfn get_mut(&mutself, key: &Value) -> Option<&mut Value> { self.0.get_mut(key)
}
/// Inserts a new element, returning the previous element with this `key` if /// there was any. pubfn insert(&mutself, key: impl Into<Value>, value: impl Into<Value>) -> Option<Value> { self.0.insert(key.into(), value.into())
}
/// Removes an element by its `key`. pubfn remove(&mutself, key: &Value) -> Option<Value> { #[cfg(feature = "indexmap")]
{ self.0.shift_remove(key)
} #[cfg(not(feature = "indexmap"))]
{ self.0.remove(key)
}
}
/// Retains only the elements specified by the `keep` predicate. /// /// In other words, remove all pairs `(k, v)` for which `keep(&k, &mut v)` /// returns `false`. /// /// The elements are visited in iteration order. pubfn retain<F>(&mutself, keep: F) where
F: FnMut(&Value, &mut Value) -> bool,
{ self.0.retain(keep);
}
}
impl Index<&Value> for Map { type Output = Value;
#[allow(clippy::expect_used)] fn index(&self, index: &Value) -> &Self::Output { self.get(index).expect("no entry found for key")
}
}
impl IndexMut<&Value> for Map { #[allow(clippy::expect_used)] fn index_mut(&mutself, index: &Value) -> &mutSelf::Output { self.get_mut(index).expect("no entry found for key")
}
}
impl IntoIterator for Map { type Item = (Value, Value);
type IntoIter = <MapInner as IntoIterator>::IntoIter;
/// Note: equality is only given if both values and order of values match impl PartialEq for Map { fn eq(&self, other: &Map) -> bool { self.cmp(other).is_eq()
}
}
/// Note: equality is only given if both values and order of values match impl Eq for Map {}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.