// SPDX-FileCopyrightText: 2017 - 2022 Kamila Borowska <kamila@borowska.pw> // SPDX-FileCopyrightText: 2020 Amanieu d'Antras <amanieu@gmail.com> // SPDX-FileCopyrightText: 2021 Bruno Corrêa Zimmermann <brunoczim@gmail.com> // // SPDX-License-Identifier: MIT OR Apache-2.0
usecrate::{EnumArray, EnumMap}; use core::iter::{Enumerate, FusedIterator}; use core::marker::PhantomData; use core::mem::ManuallyDrop; use core::ops::Range; use core::ptr; use core::slice;
/// Immutable enum map iterator /// /// This struct is created by `iter` method or `into_iter` on a reference /// to `EnumMap`. /// /// # Examples /// /// ``` /// use enum_map::{enum_map, Enum}; /// /// #[derive(Enum)] /// enum Example { /// A, /// B, /// C, /// } /// /// let mut map = enum_map! { Example::A => 3, _ => 0 }; /// assert_eq!(map[Example::A], 3); /// for (key, &value) in &map { /// assert_eq!(value, match key { /// Example::A => 3, /// _ => 0, /// }); /// } /// ``` #[derive(Debug)] pubstruct Iter<'a, K, V: 'a> {
_phantom: PhantomData<fn() -> K>,
iterator: Enumerate<slice::Iter<'a, V>>,
}
impl<K: EnumArray<V>, V> DoubleEndedIterator for IntoIter<K, V> { fn next_back(&mutself) -> Option<(K, V)> { let position = self.alive.next_back()?;
Some((K::from_usize(position), unsafe {
ptr::read(&self.map.as_slice()[position])
}))
}
}
impl<K: EnumArray<V>, V> ExactSizeIterator for IntoIter<K, V> {}
impl<K: EnumArray<V>, V> FusedIterator for IntoIter<K, V> {}
impl<K: EnumArray<V>, V> Drop for IntoIter<K, V> { #[inline] fn drop(&mutself) { unsafe {
ptr::drop_in_place(&mutself.map.as_mut_slice()[self.alive.clone()]);
}
}
}
impl<K: EnumArray<V>, V> IntoIterator for EnumMap<K, V> { type Item = (K, V); type IntoIter = IntoIter<K, V>; #[inline] fn into_iter(self) -> Self::IntoIter { let len = self.len();
IntoIter {
map: ManuallyDrop::new(self),
alive: 0..len,
}
}
}
impl<K: EnumArray<V>, V> EnumMap<K, V> { /// An iterator visiting all values. The iterator type is `&V`. /// /// # Examples /// /// ``` /// use enum_map::enum_map; /// /// let map = enum_map! { false => 3, true => 4 }; /// let mut values = map.values(); /// assert_eq!(values.next(), Some(&3)); /// assert_eq!(values.next(), Some(&4)); /// assert_eq!(values.next(), None); /// ``` #[inline] pubfn values(&self) -> Values<V> {
Values(self.as_slice().iter())
}
/// An iterator visiting all values mutably. The iterator type is `&mut V`. /// /// # Examples /// /// ``` /// use enum_map::enum_map; /// /// let mut map = enum_map! { _ => 2 }; /// for value in map.values_mut() { /// *value += 2; /// } /// assert_eq!(map[false], 4); /// assert_eq!(map[true], 4); /// ``` #[inline] pubfn values_mut(&mutself) -> ValuesMut<V> {
ValuesMut(self.as_mut_slice().iter_mut())
}
/// Creates a consuming iterator visiting all the values. The map /// cannot be used after calling this. The iterator element type /// is `V`. /// /// # Examples /// /// ``` /// use enum_map::enum_map; /// /// let mut map = enum_map! { false => "hello", true => "goodbye" }; /// assert_eq!(map.into_values().collect::<Vec<_>>(), ["hello", "goodbye"]); /// ``` #[inline] pubfn into_values(self) -> IntoValues<K, V> {
IntoValues {
inner: self.into_iter(),
}
}
}
/// An iterator over the values of `EnumMap`. /// /// This `struct` is created by the `values` method of `EnumMap`. /// See its documentation for more. pubstruct Values<'a, V: 'a>(slice::Iter<'a, V>);
impl<'a, V: 'a> ExactSizeIterator for Values<'a, V> {}
impl<'a, V: 'a> FusedIterator for Values<'a, V> {}
/// A mutable iterator over the values of `EnumMap`. /// /// This `struct` is created by the `values_mut` method of `EnumMap`. /// See its documentation for more. pubstruct ValuesMut<'a, V: 'a>(slice::IterMut<'a, V>);
impl<'a, V: 'a> ExactSizeIterator for ValuesMut<'a, V> {}
impl<'a, V: 'a> FusedIterator for ValuesMut<'a, V> {}
/// An owning iterator over the values of an `EnumMap`. /// /// This `struct` is created by the `into_values` method of `EnumMap`. /// See its documentation for more. pubstruct IntoValues<K: EnumArray<V>, V> {
inner: IntoIter<K, V>,
}
impl<K, V> Iterator for IntoValues<K, V> where
K: EnumArray<V>,
{ type Item = V;
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.