usecrate::{private, Value}; use indexmap::IndexMap; use serde::{Deserialize, Deserializer, Serialize}; use std::cmp::Ordering; use std::collections::hash_map::DefaultHasher; use std::fmt::{self, Display}; use std::hash::{Hash, Hasher}; use std::mem;
/// A YAML mapping in which the keys and values are both `serde_yaml::Value`. #[derive(Clone, 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<I: Index>(&self, index: I) -> bool {
index.is_key_into(self)
}
/// Returns the value corresponding to the key in the map. #[inline] pubfn get<I: Index>(&self, index: I) -> Option<&Value> {
index.index_into(self)
}
/// Returns the mutable reference corresponding to the key in the map. #[inline] pubfn get_mut<I: Index>(&mutself, index: I) -> Option<&mut Value> {
index.index_into_mut(self)
}
/// 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. /// /// This is equivalent to [`.swap_remove(index)`][Self::swap_remove], /// replacing this entry's position with the last element. If you need to /// preserve the relative order of the keys in the map, use /// [`.shift_remove(key)`][Self::shift_remove] instead. #[inline] pubfn remove<I: Index>(&mutself, index: I) -> Option<Value> { self.swap_remove(index)
}
/// Remove and return the key-value pair. /// /// This is equivalent to [`.swap_remove_entry(index)`][Self::swap_remove_entry], /// replacing this entry's position with the last element. If you need to /// preserve the relative order of the keys in the map, use /// [`.shift_remove_entry(key)`][Self::shift_remove_entry] instead. #[inline] pubfn remove_entry<I: Index>(&mutself, index: I) -> Option<(Value, Value)> { self.swap_remove_entry(index)
}
/// Removes and returns the value corresponding to the key from the map. /// /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the /// last element of the map and popping it off. This perturbs the position /// of what used to be the last element! #[inline] pubfn swap_remove<I: Index>(&mutself, index: I) -> Option<Value> {
index.swap_remove_from(self)
}
/// Remove and return the key-value pair. /// /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the /// last element of the map and popping it off. This perturbs the position /// of what used to be the last element! #[inline] pubfn swap_remove_entry<I: Index>(&mutself, index: I) -> Option<(Value, Value)> {
index.swap_remove_entry_from(self)
}
/// Removes and returns the value corresponding to the key from the map. /// /// Like [`Vec::remove`], the entry is removed by shifting all of the /// elements that follow it, preserving their relative order. This perturbs /// the index of all of those elements! #[inline] pubfn shift_remove<I: Index>(&mutself, index: I) -> Option<Value> {
index.shift_remove_from(self)
}
/// Remove and return the key-value pair. /// /// Like [`Vec::remove`], the entry is removed by shifting all of the /// elements that follow it, preserving their relative order. This perturbs /// the index of all of those elements! #[inline] pubfn shift_remove_entry<I: Index>(&mutself, index: I) -> Option<(Value, Value)> {
index.shift_remove_entry_from(self)
}
/// Scan through each key-value pair in the map and keep those where the /// closure `keep` returns true. #[inline] pubfn retain<F>(&mutself, keep: F) where
F: FnMut(&Value, &mut Value) -> bool,
{ self.map.retain(keep);
}
/// 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(),
}
}
/// Return an iterator over the keys of the map. pubfn keys(&self) -> Keys {
Keys {
iter: self.map.keys(),
}
}
/// Return an owning iterator over the keys of the map. pubfn into_keys(self) -> IntoKeys {
IntoKeys {
iter: self.map.into_keys(),
}
}
/// Return an iterator over the values of the map. pubfn values(&self) -> Values {
Values {
iter: self.map.values(),
}
}
/// Return an iterator over mutable references to the values of the map. pubfn values_mut(&mutself) -> ValuesMut {
ValuesMut {
iter: self.map.values_mut(),
}
}
/// Return an owning iterator over the values of the map. pubfn into_values(self) -> IntoValues {
IntoValues {
iter: self.map.into_values(),
}
}
}
/// A type that can be used to index into a `serde_yaml::Mapping`. See the /// methods `get`, `get_mut`, `contains_key`, and `remove` of `Value`. /// /// This trait is sealed and cannot be implemented for types outside of /// `serde_yaml`. pubtrait Index: private::Sealed { #[doc(hidden)] fn is_key_into(&self, v: &Mapping) -> bool;
#[allow(clippy::derived_hash_with_manual_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)
}
}
impl<I> std::ops::Index<I> for Mapping where
I: Index,
{ type Output = Value;
/// Iterator of the values of a `serde_yaml::Mapping`. pubstruct IntoValues {
iter: indexmap::map::IntoValues<Value, Value>,
}
delegate_iterator!((IntoValues) => Value);
/// 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()
}
/// Remove and return the key, value pair stored in the map for this entry. #[inline] pubfn remove_entry(self) -> (Value, Value) { self.occupied.swap_remove_entry()
}
}
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()
}
/// Takes ownership of the key, leaving the entry vacant. #[inline] pubfn into_key(self) -> Value { self.vacant.into_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.