impl Dictionary { /// Makes a new empty `Dictionary`. #[inline] pubfn new() -> Self {
Dictionary {
map: IndexMap::new(),
}
}
/// Clears the dictionary, removing all values. #[inline] pubfn clear(&mutself) { self.map.clear()
}
/// Returns a reference to the value corresponding to the key. #[inline] pubfn get(&self, key: &str) -> Option<&Value> { self.map.get(key)
}
/// Returns true if the dictionary contains a value for the specified key. #[inline] pubfn contains_key(&self, key: &str) -> bool { self.map.contains_key(key)
}
/// Returns a mutable reference to the value corresponding to the key. #[inline] pubfn get_mut(&mutself, key: &str) -> Option<&mut Value> { self.map.get_mut(key)
}
/// Inserts a key-value pair into the dictionary. /// /// If the dictionary did not have this key present, `None` is returned. /// /// If the dictionary did have this key present, the value is updated, and the old value is /// returned. #[inline] pubfn insert(&mutself, k: String, v: Value) -> Option<Value> { self.map.insert(k, v)
}
/// Removes a key from the dictionary, returning the value at the key if the key was previously /// in the dictionary. #[inline] pubfn remove(&mutself, key: &str) -> Option<Value> { self.map.remove(key)
}
/// 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(&String, &mut Value) -> bool,
{ self.map.retain(keep)
}
/// Sort the dictionary keys. /// /// This uses the default ordering defined on [`str`]. /// /// This function is useful if you are serializing to XML, and wish to /// ensure a consistent key order. #[inline] pubfn sort_keys(&mutself) { self.map.sort_keys()
}
/// Gets the given key's corresponding entry in the dictionary for in-place manipulation. // Entry functionality is unstable until I can figure out how to use either Cow<str> or // T: AsRef<str> + Into<String> #[cfg(any(
test,
feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"
))] pubfn entry<S>(&mutself, key: S) -> Entry where
S: Into<String>,
{ matchself.map.entry(key.into()) {
map::Entry::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }),
map::Entry::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied }),
}
}
/// Returns the number of elements in the dictionary. #[inline] pubfn len(&self) -> usize { self.map.len()
}
/// Returns true if the dictionary contains no elements. #[inline] pubfn is_empty(&self) -> bool { self.map.is_empty()
}
/// Gets an iterator over the entries of the dictionary. #[inline] pubfn iter(&self) -> Iter {
Iter {
iter: self.map.iter(),
}
}
/// Gets a mutable iterator over the entries of the dictionary. #[inline] pubfn iter_mut(&mutself) -> IterMut {
IterMut {
iter: self.map.iter_mut(),
}
}
/// Gets an iterator over the keys of the dictionary. #[inline] pubfn keys(&self) -> Keys {
Keys {
iter: self.map.keys(),
}
}
/// Gets an iterator over the values of the dictionary. #[inline] pubfn values(&self) -> Values {
Values {
iter: self.map.values(),
}
}
/// Gets an iterator over mutable values of the dictionary. #[inline] pubfn values_mut(&mutself) -> ValuesMut {
ValuesMut {
iter: self.map.values_mut(),
}
}
}
/// Access an element of this dictionary. Panics if the given key is not present in the dictionary. /// /// ``` /// # use plist::Value; /// # /// # let val = &Value::String("".to_owned()); /// # let _ = /// match *val { /// Value::Array(ref arr) => arr[0].as_string(), /// Value::Dictionary(ref dict) => dict["type"].as_string(), /// Value::String(ref s) => Some(s.as_str()), /// _ => None, /// } /// # ; /// ``` impl<'a> ops::Index<&'a str> for Dictionary { type Output = Value;
/// Mutably access an element of this dictionary. Panics if the given key is not present in the /// dictionary. /// /// ``` /// # let mut dict = plist::Dictionary::new(); /// # dict.insert("key".to_owned(), plist::Value::Boolean(false)); /// # /// dict["key"] = "value".into(); /// ``` impl<'a> ops::IndexMut<&'a str> for Dictionary { fn index_mut(&mutself, index: &str) -> &mut Value { self.map.get_mut(index).expect("no entry found for key")
}
}
/// A view into a single entry in a dictionary, which may either be vacant or occupied. /// This enum is constructed from the [`entry`] method on [`Dictionary`]. /// /// [`entry`]: struct.Dictionary.html#method.entry /// [`Dictionary`]: struct.Dictionary.html #[cfg(any(
test,
feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"
))] pubenum Entry<'a> { /// A vacant Entry.
Vacant(VacantEntry<'a>), /// An occupied Entry.
Occupied(OccupiedEntry<'a>),
}
/// A vacant Entry. It is part of the [`Entry`] enum. /// /// [`Entry`]: enum.Entry.html #[cfg(any(
test,
feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"
))] pubstruct VacantEntry<'a> {
vacant: map::VacantEntry<'a, String, Value>,
}
/// An occupied Entry. It is part of the [`Entry`] enum. /// /// [`Entry`]: enum.Entry.html #[cfg(any(
test,
feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"
))] pubstruct OccupiedEntry<'a> {
occupied: map::OccupiedEntry<'a, String, Value>,
}
/// Ensures a value is in the entry by inserting the default if empty, and returns a mutable /// reference to the value in the entry. /// /// # Examples /// /// ``` /// let mut dict = plist::Dictionary::new(); /// dict.entry("serde").or_insert(12.into()); /// /// assert_eq!(dict["serde"], 12.into()); /// ``` 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. /// /// # Examples /// /// ``` /// let mut dict = plist::Dictionary::new(); /// dict.entry("serde").or_insert_with(|| "hoho".into()); /// /// assert_eq!(dict["serde"], "hoho".into()); /// ``` 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(),
}
}
}
#[cfg(any(
test,
feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"
))] impl<'a> VacantEntry<'a> { /// Gets a reference to the key that would be used when inserting a value through the /// VacantEntry. /// /// # Examples /// /// ``` /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// /// match dict.entry("serde") { /// Entry::Vacant(vacant) => assert_eq!(vacant.key(), &"serde"), /// Entry::Occupied(_) => unimplemented!(), /// } /// ``` #[inline] pubfn key(&self) -> &String { self.vacant.key()
}
/// Sets the value of the entry with the VacantEntry's key, and returns a mutable reference /// to it. /// /// # Examples /// /// ``` /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// /// match dict.entry("serde") { /// Entry::Vacant(vacant) => vacant.insert("hoho".into()), /// Entry::Occupied(_) => unimplemented!(), /// }; /// ``` #[inline] pubfn insert(self, value: Value) -> &'a mut Value { self.vacant.insert(value)
}
}
#[cfg(any(
test,
feature = "enable_unstable_features_that_may_break_with_minor_version_bumps"
))] impl<'a> OccupiedEntry<'a> { /// Gets a reference to the key in the entry. /// /// # Examples /// /// ``` /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// dict.insert("serde".to_owned(), 12.into()); /// /// match dict.entry("serde") { /// Entry::Occupied(occupied) => assert_eq!(occupied.key(), &"serde"), /// Entry::Vacant(_) => unimplemented!(), /// } /// ``` #[inline] pubfn key(&self) -> &String { self.occupied.key()
}
/// Gets a reference to the value in the entry. /// /// # Examples /// /// ``` /// use plist::Value; /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// dict.insert("serde".to_owned(), 12.into()); /// /// match dict.entry("serde") { /// Entry::Occupied(occupied) => assert_eq!(occupied.get(), &Value::from(12)), /// Entry::Vacant(_) => unimplemented!(), /// } /// ``` #[inline] pubfn get(&self) -> &Value { self.occupied.get()
}
/// Gets a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use plist::Value; /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// dict.insert("serde".to_owned(), Value::Array(vec![1.into(), 2.into(), 3.into()])); /// /// match dict.entry("serde") { /// Entry::Occupied(mut occupied) => { /// occupied.get_mut().as_array_mut().unwrap().push(4.into()); /// } /// Entry::Vacant(_) => unimplemented!(), /// } /// /// assert_eq!(dict["serde"].as_array().unwrap().len(), 4); /// ``` #[inline] pubfn get_mut(&mutself) -> &mut Value { self.occupied.get_mut()
}
/// Converts the entry into a mutable reference to its value. /// /// # Examples /// /// ``` /// use plist::Value; /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// dict.insert("serde".to_owned(), Value::Array(vec![1.into(), 2.into(), 3.into()])); /// /// match dict.entry("serde") { /// Entry::Occupied(mut occupied) => { /// occupied.into_mut().as_array_mut().unwrap().push(4.into()); /// } /// Entry::Vacant(_) => unimplemented!(), /// } /// /// assert_eq!(dict["serde"].as_array().unwrap().len(), 4); /// ``` #[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. /// /// # Examples /// /// ``` /// use plist::Value; /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// dict.insert("serde".to_owned(), 12.into()); /// /// match dict.entry("serde") { /// Entry::Occupied(mut occupied) => { /// assert_eq!(occupied.insert(13.into()), 12.into()); /// assert_eq!(occupied.get(), &Value::from(13)); /// } /// Entry::Vacant(_) => unimplemented!(), /// } /// ``` #[inline] pubfn insert(&mutself, value: Value) -> Value { self.occupied.insert(value)
}
/// Takes the value of the entry out of the dictionary, and returns it. /// /// # Examples /// /// ``` /// use plist::dictionary::Entry; /// /// let mut dict = plist::Dictionary::new(); /// dict.insert("serde".to_owned(), 12.into()); /// /// match dict.entry("serde") { /// Entry::Occupied(occupied) => assert_eq!(occupied.remove(), 12.into()), /// Entry::Vacant(_) => unimplemented!(), /// } /// ``` #[inline] pubfn remove(self) -> Value { self.occupied.remove()
}
}
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.