use core::{
marker::PhantomData,
slice::{self, Iter},
};
use alloc::vec::{self, IntoIter};
use crate::allocator::Allocator;
#[derive(Debug)]
#[must_use]
pub struct Drain<'a, K, V, A: Allocator> {
#[cfg(feature = "allocator_api")]
pub(crate) iter: vec::Drain<'a, (K, V), A>,
#[cfg(not(feature = "allocator_api"))]
pub(crate) iter: vec::Drain<'a, (K, V)>,
pub(crate) phantom: PhantomData<A>,
}
impl<K, V, A: Allocator> Iterator for Drain<'_, K, V, A> {
type Item = (K, V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[must_use]
pub struct IterMut<'a, K, V>(pub(crate) slice::IterMut<'a, (K, V)>);
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(key, value)| (&*key, value))
}
}
#[derive(Debug)]
#[must_use]
pub struct Keys<'a, K, V>(pub(crate) Iter<'a, (K, V)>);
impl<'a, K, V> Iterator for Keys<'a, K, V> {
type Item = &'a K;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(key, _value)| key)
}
}
#[derive(Debug)]
#[must_use]
pub struct IntoKeys<K, V, A: Allocator> {
#[cfg(feature = "allocator_api")]
pub(crate) iter: IntoIter<(K, V), A>,
#[cfg(not(feature = "allocator_api"))]
pub(crate) iter: IntoIter<(K, V)>,
pub(crate) phantom: PhantomData<A>,
}
impl<K, V, A: Allocator> Iterator for IntoKeys<K, V, A> {
type Item = K;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(key, _value)| key)
}
}
#[derive(Debug)]
#[must_use]
pub struct Values<'a, K, V>(pub(crate) Iter<'a, (K, V)>);
impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(_key, value)| value)
}
}
#[derive(Debug)]
#[must_use]
pub struct ValuesMut<'a, K, V>(pub(crate) slice::IterMut<'a, (K, V)>);
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(_key, value)| value)
}
}
#[derive(Debug)]
#[must_use]
pub struct IntoValues<K, V, A: Allocator> {
#[cfg(feature = "allocator_api")]
pub(crate) iter: IntoIter<(K, V), A>,
#[cfg(not(feature = "allocator_api"))]
pub(crate) iter: IntoIter<(K, V)>,
pub(crate) phantom: PhantomData<A>,
}
impl<K, V, A: Allocator> Iterator for IntoValues<K, V, A> {
type Item = V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(_key, value)| value)
}
}