macro_rules! do_impl {
(
MapIter for $name:ident, $impl_for:ty,
($key:ty, $key_item:ty, $keys_iter:ty),
($value:ty, $value_item:ty, $values_iter:ty); $($gen:tt)*
) => {
impl<'a, $($gen)*> $crate::map::HasMapData for $impl_for {
type Key = $key;
type Value = $value;
}
impl<'a, $($gen)*> $crate::map::MapIter<'a> for $impl_for {
type KeyRef = $key_item;
type ValueRef = $value_item;
type IterKeysRef = $keys_iter;
type IterValuesRef = $values_iter;
fn keys(&'a self) -> $keys_iter {
$name::keys(self)
}
fn values(&'a self) -> $values_iter {
$name::values(self)
}
}
};
(
MapIterMut for $name:ident, $impl_for:ty,
($key:ty, $key_item:ty, $keys_iter:ty),
($value:ty, $value_item:ty, $values_iter:ty),
($value_item_mut:ty, $values_mut_iter:ty); $($gen:tt)*
) => {
do_impl!(MapIter for $name, $impl_for,
($key, $key_item, $keys_iter),
($value, $value_item, $values_iter); $($gen)*);
impl<'a, $($gen)*> $crate::map::MapIterMut<'a> for $impl_for {
type ValueMut = $value_item_mut;
type IterValuesMut = $values_mut_iter;
fn values_mut(&'a mut self) -> $values_mut_iter {
$name::values_mut(self)
}
}
};
}
impl<T> super::HasData for [T] {
type Item = T;
}
impl<T> super::HasMapData for [T] {
type Key = usize;
type Value = T;
}
cfg_if! {
if #[cfg(feature = "alloc")] {
cfg_if! {
if #[cfg(feature = "std")] {
use std::collections;
} else {
use alloc::{self as collections, Vec};
}
}
use self::collections::{BTreeMap, BTreeSet, VecDeque, btree_map};
impl<T: Ord> super::HasMapData for BTreeSet<T> {
type Key = T;
type Value = T;
}
impl<T> super::HasMapData for Vec<T> {
type Key = usize;
type Value = T;
}
impl<T> super::HasMapData for VecDeque<T> {
type Key = usize;
type Value = T;
}
do_impl!(MapIterMut for BTreeMap, BTreeMap<K, V>,
(K, &'a K, btree_map::Keys<'a, K, V>),
(V, &'a V, btree_map::Values<'a, K, V>),
(&'a mut V, btree_map::ValuesMut<'a, K, V>); K: 'a + Ord, V: 'a);
}
}
cfg_if! {
if #[cfg(feature = "std")] {
use std::collections::{HashMap, HashSet, hash_map};
use std::hash::Hash;
impl<T: Hash + Eq> super::HasMapData for HashSet<T> {
type Key = T;
type Value = T;
}
do_impl!(MapIterMut for HashMap, HashMap<K, V>,
(K, &'a K, hash_map::Keys<'a, K, V>),
(V, &'a V, hash_map::Values<'a, K, V>),
(&'a mut V, hash_map::ValuesMut<'a, K, V>); K: 'a + Hash + Eq, V: 'a);
}
}