// SPDX-FileCopyrightText: 2017 - 2021 Kamila Borowska // SPDX-FileCopyrightText: 2021 Bruno CorrĂȘa Zimmermann // SPDX-FileCopyrightText: 2021 micycle // SPDX-FileCopyrightText: 2023 Nicolas Carranza // // SPDX-License-Identifier: MIT OR Apache-2.0 use crate::{enum_map, EnumArray, EnumMap}; use core::fmt::{self, Debug, Formatter}; use core::hash::{Hash, Hasher}; use core::iter::{Extend, FromIterator}; use core::ops::{Index, IndexMut}; impl + Debug, V: Debug> Debug for EnumMap { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_map().entries(self).finish() } } impl, V> Extend<(K, V)> for EnumMap { fn extend>(&mut self, iter: I) { for (key, value) in iter { self[key] = value; } } } impl<'a, K, V> Extend<(&'a K, &'a V)> for EnumMap where K: EnumArray + Copy, V: Copy, { fn extend>(&mut self, iter: I) { self.extend(iter.into_iter().map(|(&key, &value)| (key, value))); } } impl FromIterator<(K, V)> for EnumMap where Self: Default, K: EnumArray, { fn from_iter>(iter: I) -> Self { let mut map = EnumMap::default(); map.extend(iter); map } } impl, V> Index for EnumMap { type Output = V; #[inline] fn index(&self, key: K) -> &V { &self.as_slice()[key.into_usize()] } } impl, V> IndexMut for EnumMap { #[inline] fn index_mut(&mut self, key: K) -> &mut V { &mut self.as_mut_slice()[key.into_usize()] } } // Implementations provided by derive attribute are too specific, and put requirements on K. // This is caused by rust-lang/rust#26925. impl, V> Clone for EnumMap where K::Array: Clone, { #[inline] fn clone(&self) -> Self { EnumMap { array: self.array.clone(), } } } impl, V> Copy for EnumMap where K::Array: Copy {} impl, V: PartialEq> PartialEq for EnumMap { #[inline] fn eq(&self, other: &Self) -> bool { self.as_slice() == other.as_slice() } } impl, V: Eq> Eq for EnumMap {} impl, V: Hash> Hash for EnumMap { #[inline] fn hash(&self, state: &mut H) { self.as_slice().hash(state); } } impl, V: Default> Default for EnumMap { #[inline] fn default() -> Self { enum_map! { _ => V::default() } } } impl, V: PartialOrd> PartialOrd for EnumMap { fn partial_cmp(&self, other: &Self) -> Option { self.as_slice().partial_cmp(other.as_slice()) } } impl, V: Ord> Ord for EnumMap { fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.as_slice().cmp(other.as_slice()) } }