comparable/
array.rs

1use crate::types::{Changed, Comparable};
2use std::convert::TryInto;
3
4fn convert_vec_to_array<T, const N: usize>(v: Vec<T>) -> [T; N] {
5	v.try_into().unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
6}
7
8impl<T: Comparable, const N: usize> Comparable for [T; N] {
9	type Desc = [T::Desc; N];
10
11	fn describe(&self) -> Self::Desc {
12		let v = self.iter().map(|v| v.describe()).collect::<Vec<_>>();
13		convert_vec_to_array(v)
14	}
15
16	type Change = [Changed<T::Change>; N];
17
18	fn comparison(&self, other: &Self) -> Changed<Self::Change> {
19		let mut result: Self::Change = [(); N].map(|_| Changed::Unchanged);
20		let mut has_change = false;
21		for i in 0..N {
22			match self[i].comparison(&other[i]) {
23				Changed::Unchanged => (),
24				Changed::Changed(v) => {
25					has_change = true;
26					result[i] = Changed::Changed(v)
27				}
28			}
29		}
30		if has_change {
31			Changed::Changed(result)
32		} else {
33			Changed::Unchanged
34		}
35	}
36}