1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::BoneKind;
use core::ops::{Index, IndexMut};
use derive_more::From;
use stackvec::{error::IncompleteArrayError, TryCollect, TryFromIterator};
use std::collections::HashMap;
use std::iter::{Enumerate, Map};
#[derive(Debug, Default, Clone, Copy, From, Eq, PartialEq)]
pub struct BoneMap<T>([T; BoneKind::num_types()]);
impl<T> BoneMap<T> {
pub fn new(map: [T; BoneKind::num_types()]) -> Self {
Self(map)
}
pub fn iter(&self) -> Iter<'_, T> {
self.into_iter()
}
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
self.into_iter()
}
pub fn map<U>(self, mut f: impl FnMut(BoneKind, T) -> U) -> BoneMap<U> {
let it = self.into_iter().map(|(kind, item)| (kind, f(kind, item)));
it.try_collect().unwrap()
}
}
impl<T> TryFrom<HashMap<BoneKind, T>> for BoneMap<T> {
type Error = IncompleteArrayError;
fn try_from(other: HashMap<BoneKind, T>) -> Result<Self, Self::Error> {
other.into_iter().try_collect()
}
}
impl<T> TryFromIterator<(BoneKind, T)> for BoneMap<T> {
type Error = IncompleteArrayError;
fn try_from_iter<I>(iter: I) -> Result<Self, Self::Error>
where
I: IntoIterator<Item = (BoneKind, T)>,
{
let mut bmap: BoneMap<Option<T>> = BoneMap::default();
for (kind, item) in iter.into_iter().take(BoneKind::NUM_TYPES) {
bmap[kind] = Some(item);
}
if bmap.iter().any(|(_kind, item)| item.is_none()) {
return Err(IncompleteArrayError);
}
Ok(BoneMap::new(bmap.0.map(|item| item.unwrap())))
}
}
impl<T> Index<BoneKind> for BoneMap<T> {
type Output = T;
fn index(&self, index: BoneKind) -> &Self::Output {
&self.0[usize::from(index)]
}
}
impl<T> IndexMut<BoneKind> for BoneMap<T> {
fn index_mut(&mut self, index: BoneKind) -> &mut Self::Output {
&mut self.0[usize::from(index)]
}
}
type MapIdxFnType<T> = fn((usize, T)) -> (BoneKind, T);
pub type Iter<'a, T> = Map<Enumerate<std::slice::Iter<'a, T>>, MapIdxFnType<&'a T>>;
pub type IterMut<'a, T> =
Map<Enumerate<std::slice::IterMut<'a, T>>, MapIdxFnType<&'a mut T>>;
pub type IntoIter<T> =
Map<Enumerate<std::array::IntoIter<T, { BoneKind::NUM_TYPES }>>, MapIdxFnType<T>>;
impl<T> IntoIterator for BoneMap<T> {
type Item = (BoneKind, T);
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter().enumerate().map(map_idx)
}
}
impl<'a, T> IntoIterator for &'a BoneMap<T> {
type Item = (BoneKind, &'a T);
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter().enumerate().map(map_idx)
}
}
impl<'a, T> IntoIterator for &'a mut BoneMap<T> {
type Item = (BoneKind, &'a mut T);
type IntoIter = IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter_mut().enumerate().map(map_idx)
}
}
fn map_idx<T>(item: (usize, T)) -> (BoneKind, T) {
(BoneKind::try_from(item.0).unwrap(), item.1)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let zeros = BoneMap::new([0u8; BoneKind::NUM_TYPES]);
let ones = BoneMap::new([1u8; BoneKind::NUM_TYPES]);
assert_eq!(zeros, BoneMap::default());
assert_ne!(ones, BoneMap::default());
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
struct Foo;
let nones: BoneMap<Option<Foo>> = BoneMap::new([None; BoneKind::NUM_TYPES]);
assert_eq!(nones, BoneMap::default())
}
#[test]
fn test_map() {
let zeros = BoneMap::new([0u8; BoneKind::NUM_TYPES]);
let as_u8 = zeros.map(|kind, _| kind as u8);
for (kind, _) in zeros.iter() {
assert_eq!(kind as u8, as_u8[kind]);
}
}
}