impl<K, V> Map<K, V> { /// Creates a new empty [`Map`]. #[inline] pubfn new() -> Self { Self::default()
}
/// Clears the [`Map`], removing all elements. #[inline] pubfn clear(&mutself) { self.inner.clear()
}
/// Returns the number of elements in the [`Map`]. #[inline] pubfn len(&self) -> usize { self.inner.len()
}
/// Returns `true` if the [`Map`] contains no elements. #[inline] pubfn is_empty(&self) -> bool { self.inner.is_empty()
}
/// Returns an iterator that yields the items in the [`Map`]. #[inline] pubfn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.iter(),
}
}
/// Returns a mutable iterator that yields the items in the [`Map`]. #[inline] pubfn iter_mut(&mutself) -> IterMut<'_, K, V> {
IterMut {
inner: self.inner.iter_mut(),
}
}
/// Returns an iterator that yields the keys in the [`Map`]. #[inline] pubfn keys(&self) -> Keys<'_, K, V> {
Keys {
inner: self.inner.keys(),
}
}
/// Creates a consuming iterator visiting all the keys in arbitrary order. /// /// The [`Map`] cannot be used after calling this. /// The iterator element type is `K`. #[inline] pubfn into_keys(self) -> IntoKeys<K, V> {
IntoKeys {
inner: self.inner.into_keys(),
}
}
/// Returns an iterator that yields the values in the [`Map`]. #[inline] pubfn values(&self) -> Values<'_, K, V> {
Values {
inner: self.inner.values(),
}
}
/// Creates a consuming iterator visiting all the values in arbitrary order. /// /// The [`Map`] cannot be used after calling this. /// The iterator element type is `V`. #[inline] pubfn into_values(self) -> IntoValues<K, V> {
IntoValues {
inner: self.inner.into_values(),
}
}
/// Returns a mutable iterator that yields the values in the [`Map`]. #[inline] pubfn values_mut(&mutself) -> ValuesMut<'_, K, V> {
ValuesMut {
inner: self.inner.values_mut(),
}
}
}
impl<K, V> Map<K, V> where
K: Hash + Eq + Ord,
{ /// Reserves capacity for at least `additional` more elements to be inserted in the [`Map`]. #[inline] pubfn reserve(&mutself, additional: usize) { #[cfg(not(feature = "no-hash-maps"))] self.inner.reserve(additional); #[cfg(feature = "no-hash-maps")] let _ = additional;
}
/// Returns true if `key` is contains in the [`Map`]. #[inline] pubfn contains_key<Q>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.contains_key(key)
}
/// Returns a reference to the value corresponding to the `key`. #[inline] pubfn get<Q>(&self, key: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.get(key)
}
/// Returns the key-value pair corresponding to the supplied key. /// /// The supplied key may be any borrowed form of the map's key type, but the ordering /// on the borrowed form *must* match the ordering on the key type. #[inline] pubfn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)> where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.get_key_value(key)
}
/// Returns a mutable reference to the value corresponding to the key. #[inline] pubfn get_mut<Q>(&mutself, key: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.get_mut(key)
}
/// Inserts a key-value pair into the [`Map`]. /// /// If the map did not have this key present, `None` is returned. /// /// If the map did have this key present, the value is updated, and the old /// value is returned. The key is not updated, though; this matters for /// types that can be `==` without being identical. #[inline] pubfn insert(&mutself, key: K, value: V) -> Option<V> { self.inner.insert(key, value)
}
/// Removes a key from the [`Map`], returning the value at the key if the key was previously in the map. #[inline] pubfn remove<Q>(&mutself, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{ self.inner.remove(key)
}
/// Removes a key from the [`Map`], returning the stored key and value if the key /// was previously in the map. /// /// The key may be any borrowed form of the map's key type, but the ordering /// on the borrowed form *must* match the ordering on the key type. #[inline] pubfn remove_entry<Q>(&mutself, key: &Q) -> Option<(K, V)> where
K: Borrow<Q>,
Q: ?Sized + Hash + Ord,
{ self.inner.remove_entry(key)
}
/// Gets the given key's corresponding entry in the [`Map`] for in-place manipulation. #[inline] pubfn entry(&mutself, key: K) -> Entry<'_, K, V> { matchself.inner.entry(key) {
detail::EntryImpl::Occupied(entry) => Entry::Occupied(OccupiedEntry { inner: entry }),
detail::EntryImpl::Vacant(entry) => Entry::Vacant(VacantEntry { inner: entry }),
}
}
/// Retains only the elements specified by the predicate. /// /// In other words, remove all pairs `(k, v)` for which `f(&k, &mut v)` returns `false`. /// The elements are visited in ascending key order. #[inline] pubfn retain<F>(&mutself, f: F) where
F: FnMut(&K, &mut V) -> bool,
{ self.inner.retain(f)
}
}
/// A view into a single entry in a [`Map`], which may either be vacant or occupied. /// /// This enum is constructed from the entry method on [`Map`]. #[derive(Debug)] pubenum Entry<'a, K: Ord, V> { /// An occupied entry.
Occupied(OccupiedEntry<'a, K, V>), /// A vacant entry.
Vacant(VacantEntry<'a, K, V>),
}
impl<'a, K, V> Entry<'a, K, V> where
K: Hash + Ord,
{ /// Ensures a value is in the entry by inserting the default if empty, and returns /// a mutable reference to the value in the entry. #[inline] pubfn or_insert(self, default: V) -> &'a mut V { matchself { Self::Occupied(entry) => entry.into_mut(), Self::Vacant(entry) => entry.insert(default),
}
}
/// 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. #[inline] pubfn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V { matchself { Self::Occupied(entry) => entry.into_mut(), Self::Vacant(entry) => entry.insert(default()),
}
}
/// Ensures a value is in the [`Entry`] by inserting, if empty, the result of the default function. /// This method allows for generating key-derived values for insertion by providing the default /// function a reference to the key that was moved during the `.entry(key)` method call. /// /// The reference to the moved key is provided so that cloning or copying the key is /// unnecessary, unlike with `.or_insert_with(|| ... )`. #[inline] pubfn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V { matchself { Self::Occupied(entry) => entry.into_mut(), Self::Vacant(entry) => { let value = default(entry.key());
entry.insert(value)
}
}
}
/// Returns a reference to this [`Entry`]'s key. #[inline] pubfn key(&self) -> &K { match *self { Self::Occupied(ref entry) => entry.key(), Self::Vacant(ref entry) => entry.key(),
}
}
/// Provides in-place mutable access to an occupied [`Entry`] before any /// potential inserts into the map. #[inline] pubfn and_modify<F>(self, f: F) -> Self where
F: FnOnce(&mut V),
{ matchself { Self::Occupied(mut entry) => {
f(entry.get_mut()); Self::Occupied(entry)
} Self::Vacant(entry) => Self::Vacant(entry),
}
}
}
impl<'a, K, V> Entry<'a, K, V> where
K: Hash + Ord,
V: Default,
{ /// Ensures a value is in the [`Entry`] by inserting the default value if empty, /// and returns a mutable reference to the value in the entry. #[inline] pubfn or_default(self) -> &'a mut V { matchself { Self::Occupied(entry) => entry.into_mut(), Self::Vacant(entry) => entry.insert(Default::default()),
}
}
}
/// A view into an occupied entry in a [`Map`]. /// /// It is part of the [`Entry`] enum. pubstruct OccupiedEntry<'a, K, V> {
inner: detail::OccupiedEntryImpl<'a, K, V>,
}
impl<'a, K, V> OccupiedEntry<'a, K, V> where
K: Ord + 'a,
V: 'a,
{ /// Gets a reference to the key in the entry. #[inline] pubfn key(&self) -> &K { self.inner.key()
}
/// Gets a reference to the value in the entry. #[inline] pubfn get(&self) -> &V { self.inner.get()
}
/// Gets a mutable reference to the value in the entry. #[inline] pubfn get_mut(&mutself) -> &mut V { self.inner.get_mut()
}
/// Sets the value of the entry with the [`OccupiedEntry`]'s key, and returns the entry's old value. #[inline] pubfn insert(&mutself, value: V) -> V { self.inner.insert(value)
}
/// Converts the [`OccupiedEntry`] into a mutable reference to the value in the entry /// with a lifetime bound to the map itself. #[inline] pubfn into_mut(self) -> &'a mut V { self.inner.into_mut()
}
/// Take ownership of the key and value from the [`Map`]. #[inline] pubfn remove_entry(self) -> (K, V) { self.inner.remove_entry()
}
/// Takes the value of the entry out of the [`Map`], and returns it. #[inline] pubfn remove(self) -> V { self.inner.remove()
}
}
/// A view into a vacant entry in a [`Map`]. /// /// It is part of the [`Entry`] enum. pubstruct VacantEntry<'a, K, V> {
inner: detail::VacantEntryImpl<'a, K, V>,
}
impl<'a, K, V> VacantEntry<'a, K, V> where
K: Ord + 'a,
V: 'a,
{ /// Gets a reference to the key in the entry. #[inline] pubfn key(&self) -> &K { self.inner.key()
}
/// Take ownership of the key. #[inline] pubfn into_key(self) -> K { self.inner.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: V) -> &'a mut V where
K: Hash,
{ self.inner.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.