enum_table/
impls.rs

1use std::ops::{Index, IndexMut};
2
3use crate::{EnumTable, Enumable};
4
5impl<K: Enumable, V: core::fmt::Debug, const N: usize> core::fmt::Debug for EnumTable<K, V, N> {
6    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
7        f.debug_struct("EnumTable")
8            .field("table", &self.table)
9            .finish()
10    }
11}
12
13impl<K: Enumable, V: Clone, const N: usize> Clone for EnumTable<K, V, N> {
14    fn clone(&self) -> Self {
15        Self {
16            table: self.table.clone(),
17            _phantom: core::marker::PhantomData,
18        }
19    }
20}
21
22impl<K: Enumable, V: Copy, const N: usize> Copy for EnumTable<K, V, N> {}
23
24impl<K: Enumable, V: PartialEq, const N: usize> PartialEq for EnumTable<K, V, N> {
25    fn eq(&self, other: &Self) -> bool {
26        self.table.eq(&other.table)
27    }
28}
29
30impl<K: Enumable, V: Eq, const N: usize> Eq for EnumTable<K, V, N> {}
31
32impl<K: Enumable, V: std::hash::Hash, const N: usize> std::hash::Hash for EnumTable<K, V, N> {
33    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
34        self.table.hash(state);
35    }
36}
37
38impl<K: Enumable, V: Default, const N: usize> Default for EnumTable<K, V, N> {
39    fn default() -> Self {
40        EnumTable::new_with_fn(|_| Default::default())
41    }
42}
43
44impl<K: Enumable, V, const N: usize> Index<K> for EnumTable<K, V, N> {
45    type Output = V;
46
47    fn index(&self, index: K) -> &Self::Output {
48        self.get(&index)
49    }
50}
51
52impl<K: Enumable, V, const N: usize> IndexMut<K> for EnumTable<K, V, N> {
53    fn index_mut(&mut self, index: K) -> &mut Self::Output {
54        self.get_mut(&index)
55    }
56}