usecrate::Value; use indexmap::IndexMap; use serde::{Deserialize, Deserializer, Serialize}; use std::cmp::Ordering; use std::collections::hash_map::DefaultHasher; use std::fmt; use std::hash::{Hash, Hasher}; use std::iter::FromIterator; use std::ops::{Index, IndexMut};
/// A YAML mapping in which the keys and values are both `serde_yaml::Value`. #[derive(Clone, Debug, Default, Eq, PartialEq)] pubstruct Mapping {
map: IndexMap<Value, Value>,
}
/// Creates an empty YAML map with the given initial capacity. #[inline] pubfn with_capacity(capacity: usize) -> Self {
Mapping {
map: IndexMap::with_capacity(capacity),
}
}
/// Reserves capacity for at least `additional` more elements to be inserted /// into the map. The map may reserve more space to avoid frequent /// allocations. /// /// # Panics /// /// Panics if the new allocation size overflows `usize`. #[inline] pubfn reserve(&mutself, additional: usize) { self.map.reserve(additional);
}
/// Shrinks the capacity of the map as much as possible. It will drop down /// as much as possible while maintaining the internal rules and possibly /// leaving some space in accordance with the resize policy. #[inline] pubfn shrink_to_fit(&mutself) { self.map.shrink_to_fit();
}
/// Inserts a key-value pair into the map. If the key already existed, the /// old value is returned. #[inline] pubfn insert(&mutself, k: Value, v: Value) -> Option<Value> { self.map.insert(k, v)
}
/// Checks if the map contains the given key. #[inline] pubfn contains_key(&self, k: &Value) -> bool { self.map.contains_key(k)
}
/// Returns the value corresponding to the key in the map. #[inline] pubfn get(&self, k: &Value) -> Option<&Value> { self.map.get(k)
}
/// Returns the mutable reference corresponding to the key in the map. #[inline] pubfn get_mut(&mutself, k: &Value) -> Option<&>mut Value> { self.map.get_mut(k)
}
/// Gets the given key’s corresponding entry in the map for insertion and/or /// in-place manipulation. #[inline] pubfn entry(&mutself, k: Value) -> Entry { matchself.map.entry(k) {
indexmap::map::Entry::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied }),
indexmap::map::Entry::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }),
}
}
/// Removes and returns the value corresponding to the key from the map. #[inline] pubfn remove(&mutself, k: &Value) -> Option<Value> { self.map.remove(k)
}
/// Returns the maximum number of key-value pairs the map can hold without /// reallocating. #[inline] pubfn capacity(&self) -> usize { self.map.capacity()
}
/// Returns the number of key-value pairs in the map. #[inline] pubfn len(&self) -> usize { self.map.len()
}
/// Returns whether the map is currently empty. #[inline] pubfn is_empty(&self) -> bool { self.map.is_empty()
}
/// Clears the map of all key-value pairs. #[inline] pubfn clear(&mutself) { self.map.clear();
}
/// Returns a double-ended iterator visiting all key-value pairs in order of /// insertion. Iterator element type is `(&'a Value, &'a Value)`. #[inline] pubfn iter(&self) -> Iter {
Iter {
iter: self.map.iter(),
}
}
/// Returns a double-ended iterator visiting all key-value pairs in order of /// insertion. Iterator element type is `(&'a Value, &'a mut ValuE)`. #[inline] pubfn iter_mut(&mutself) -> IterMut {
IterMut {
iter: self.map.iter_mut(),
}
}
}
#[allow(clippy::derive_hash_xor_eq)] impl Hash for Mapping { fn hash<H: Hasher>(&self, state: &mut H) { // Hash the kv pairs in a way that is not sensitive to their order. letmut xor = 0; for (k, v) inself { letmut hasher = DefaultHasher::new();
k.hash(&mut hasher);
v.hash(&mut hasher);
xor ^= hasher.finish();
}
xor.hash(state);
}
}
// Sort in an arbitrary order that is consistent with Value's PartialOrd // impl. fn total_cmp(a: &Value, b: &Value) -> Ordering { match (a, b) {
(Value::Null, Value::Null) => Ordering::Equal,
(Value::Null, _) => Ordering::Less,
(_, Value::Null) => Ordering::Greater,
// While sorting by map key, we get to assume that no two keys are // equal, otherwise they wouldn't both be in the map. This is not a safe // assumption outside of this situation. let total_cmp = |&(a, _): &_, &(b, _): &_| total_cmp(a, b);
self_entries.sort_by(total_cmp);
other_entries.sort_by(total_cmp);
self_entries.partial_cmp(&other_entries)
}
}
/// Iterator over `serde_yaml::Mapping` by value. pubstruct IntoIter {
iter: indexmap::map::IntoIter<Value, Value>,
}
delegate_iterator!((IntoIter) => (Value, Value));
impl IntoIterator for Mapping { type Item = (Value, Value); type IntoIter = IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter {
IntoIter {
iter: self.map.into_iter(),
}
}
}
/// Entry for an existing key-value pair or a vacant location to insert one. pubenum Entry<'a> { /// Existing slot with equivalent key.
Occupied(OccupiedEntry<'a>), /// Vacant slot (no equivalent key in the map).
Vacant(VacantEntry<'a>),
}
/// A view into an occupied entry in a [`Mapping`]. It is part of the [`Entry`] /// enum. pubstruct OccupiedEntry<'a> {
occupied: indexmap::map::OccupiedEntry<'a, Value, Value>,
}
/// A view into a vacant entry in a [`Mapping`]. It is part of the [`Entry`] /// enum. pubstruct VacantEntry<'a> {
vacant: indexmap::map::VacantEntry<'a, Value, Value>,
}
impl<'a> Entry<'a> { /// Returns a reference to this entry's key. pubfn key(&self) -> &Value { matchself {
Entry::Vacant(e) => e.key(),
Entry::Occupied(e) => e.key(),
}
}
/// Ensures a value is in the entry by inserting the default if empty, and /// returns a mutable reference to the value in the entry. pubfn or_insert(self, default: Value) -> &'a mut Value { matchself {
Entry::Vacant(entry) => entry.insert(default),
Entry::Occupied(entry) => entry.into_mut(),
}
}
/// Ensures a value is in the entry by inserting the result of the default /// function if empty, and returns a mutable reference to the value in the /// entry. pubfn or_insert_with<F>(self, default: F) -> &'a mut Value where
F: FnOnce() -> Value,
{ matchself {
Entry::Vacant(entry) => entry.insert(default()),
Entry::Occupied(entry) => entry.into_mut(),
}
}
/// Provides in-place mutable access to an occupied entry before any /// potential inserts into the map. pubfn and_modify<F>(self, f: F) -> Self where
F: FnOnce(&mut Value),
{ matchself {
Entry::Occupied(mut entry) => {
f(entry.get_mut());
Entry::Occupied(entry)
}
Entry::Vacant(entry) => Entry::Vacant(entry),
}
}
}
impl<'a> OccupiedEntry<'a> { /// Gets a reference to the key in the entry. #[inline] pubfn key(&self) -> &Value { self.occupied.key()
}
/// Gets a reference to the value in the entry. #[inline] pubfn get(&self) -> &Value { self.occupied.get()
}
/// Gets a mutable reference to the value in the entry. #[inline] pubfn get_mut(&mutself) -> &mut Value { self.occupied.get_mut()
}
/// Converts the entry into a mutable reference to its value. #[inline] pubfn into_mut(self) -> &'a mut Value { self.occupied.into_mut()
}
/// Sets the value of the entry with the `OccupiedEntry`'s key, and returns /// the entry's old value. #[inline] pubfn insert(&mutself, value: Value) -> Value { self.occupied.insert(value)
}
/// Takes the value of the entry out of the map, and returns it. #[inline] pubfn remove(self) -> Value { self.occupied.swap_remove()
}
}
impl<'a> VacantEntry<'a> { /// Gets a reference to the key that would be used when inserting a value /// through the VacantEntry. #[inline] pubfn key(&self) -> &Value { self.vacant.key()
}
/// Sets the value of the entry with the VacantEntry's key, and returns a /// mutable reference to it. #[inline] pubfn insert(self, value: Value) -> &'a mut Value { self.vacant.insert(value)
}
}
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.