mod entry;
pub use self::entry::Entry;
pub(crate) mod storage;
pub use self::storage::{MapStorage, OccupiedEntry, VacantEntry};
use core::cmp::{Ord, Ordering, PartialOrd};
use core::fmt;
use core::hash::{Hash, Hasher};
use crate::key::Key;
pub type Iter<'a, K, V> = <<K as Key>::MapStorage<V> as MapStorage<K, V>>::Iter<'a>;
pub type Keys<'a, K, V> = <<K as Key>::MapStorage<V> as MapStorage<K, V>>::Keys<'a>;
pub type Values<'a, K, V> = <<K as Key>::MapStorage<V> as MapStorage<K, V>>::Values<'a>;
pub type IterMut<'a, K, V> = <<K as Key>::MapStorage<V> as MapStorage<K, V>>::IterMut<'a>;
pub type ValuesMut<'a, K, V> = <<K as Key>::MapStorage<V> as MapStorage<K, V>>::ValuesMut<'a>;
pub type IntoIter<K, V> = <<K as Key>::MapStorage<V> as MapStorage<K, V>>::IntoIter;
#[repr(transparent)]
pub struct Map<K, V>
where
K: Key,
{
storage: K::MapStorage<V>,
}
impl<K, V> Map<K, V>
where
K: Key,
{
#[inline]
#[must_use]
pub fn new() -> Map<K, V> {
Map {
storage: K::MapStorage::empty(),
}
}
#[inline]
pub fn iter(&self) -> Iter<'_, K, V> {
self.storage.iter()
}
#[inline]
pub fn keys(&self) -> Keys<'_, K, V> {
self.storage.keys()
}
#[inline]
pub fn values(&self) -> Values<'_, K, V> {
self.storage.values()
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
self.storage.iter_mut()
}
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
self.storage.values_mut()
}
#[inline]
pub fn contains_key(&self, key: K) -> bool {
self.storage.contains_key(key)
}
#[inline]
pub fn get(&self, key: K) -> Option<&V> {
self.storage.get(key)
}
#[inline]
pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
self.storage.get_mut(key)
}
#[inline]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.storage.insert(key, value)
}
#[inline]
pub fn remove(&mut self, key: K) -> Option<V> {
self.storage.remove(key)
}
#[inline]
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(K, &mut V) -> bool,
{
self.storage.retain(f);
}
#[inline]
pub fn clear(&mut self) {
self.storage.clear();
}
#[inline]
pub fn is_empty(&self) -> bool {
self.storage.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.storage.len()
}
#[inline]
pub fn entry(&mut self, key: K) -> Entry<'_, K::MapStorage<V>, K, V> {
K::MapStorage::entry(&mut self.storage, key)
}
}
impl<K, V> Clone for Map<K, V>
where
K: Key,
K::MapStorage<V>: Clone,
{
#[inline]
fn clone(&self) -> Map<K, V> {
Map {
storage: self.storage.clone(),
}
}
}
impl<K, V> Copy for Map<K, V>
where
K: Key,
K::MapStorage<V>: Copy,
{
}
impl<K, V> Default for Map<K, V>
where
K: Key,
{
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<K, V> fmt::Debug for Map<K, V>
where
K: Key + fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
impl<K, V> PartialEq for Map<K, V>
where
K: Key,
K::MapStorage<V>: PartialEq,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.storage == other.storage
}
}
impl<K, V> Eq for Map<K, V>
where
K: Key,
K::MapStorage<V>: Eq,
{
}
impl<K, V> Hash for Map<K, V>
where
K: Key,
K::MapStorage<V>: Hash,
{
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.storage.hash(state);
}
}
impl<K, V> PartialOrd for Map<K, V>
where
K: Key,
K::MapStorage<V>: PartialOrd,
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.storage.partial_cmp(&other.storage)
}
#[inline]
fn lt(&self, other: &Self) -> bool {
self.storage.lt(&other.storage)
}
#[inline]
fn le(&self, other: &Self) -> bool {
self.storage.le(&other.storage)
}
#[inline]
fn gt(&self, other: &Self) -> bool {
self.storage.gt(&other.storage)
}
#[inline]
fn ge(&self, other: &Self) -> bool {
self.storage.ge(&other.storage)
}
}
impl<K, V> Ord for Map<K, V>
where
K: Key,
K::MapStorage<V>: Ord,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.storage.cmp(&other.storage)
}
#[inline]
fn max(self, other: Self) -> Self {
Self {
storage: self.storage.max(other.storage),
}
}
#[inline]
fn min(self, other: Self) -> Self {
Self {
storage: self.storage.min(other.storage),
}
}
#[inline]
fn clamp(self, min: Self, max: Self) -> Self {
Self {
storage: self.storage.clamp(min.storage, max.storage),
}
}
}
impl<'a, K, V> IntoIterator for &'a Map<K, V>
where
K: Key,
{
type Item = (K, &'a V);
type IntoIter = Iter<'a, K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, K, V> IntoIterator for &'a mut Map<K, V>
where
K: Key,
{
type Item = (K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<K, V> IntoIterator for Map<K, V>
where
K: Key,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.storage.into_iter()
}
}
impl<K, V> FromIterator<(K, V)> for Map<K, V>
where
K: Key,
{
#[inline]
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (K, V)>,
{
let mut map = Self::new();
for (k, v) in iter {
map.insert(k, v);
}
map
}
}
#[cfg(feature = "serde")]
impl<K, V> serde::Serialize for Map<K, V>
where
K: Key + serde::Serialize,
V: serde::Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeMap as _;
let mut map = serializer.serialize_map(Some(self.len()))?;
for (k, v) in self.iter() {
map.serialize_entry(&k, v)?;
}
map.end()
}
}
#[cfg(feature = "serde")]
impl<'de, K, V> serde::de::Deserialize<'de> for Map<K, V>
where
K: Key + serde::de::Deserialize<'de>,
V: serde::Deserialize<'de>,
{
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct MapVisitor<K, V>(core::marker::PhantomData<(K, V)>);
impl<'de, K, V> serde::de::Visitor<'de> for MapVisitor<K, V>
where
K: Key + serde::de::Deserialize<'de>,
V: serde::Deserialize<'de>,
{
type Value = Map<K, V>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a map")
}
#[inline]
fn visit_map<T>(self, mut visitor: T) -> Result<Self::Value, T::Error>
where
T: serde::de::MapAccess<'de>,
{
let mut map = Map::new();
while let Some((key, value)) = visitor.next_entry()? {
map.insert(key, value);
}
Ok(map)
}
}
deserializer.deserialize_map(MapVisitor(core::marker::PhantomData))
}
}