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
use iter_fixed::IntoIteratorFixed;
use crate::storage::Handle;
pub trait MergeWith: Sized {
fn merge_with(self, other: impl Into<Self>) -> Self;
}
pub struct Mergeable<T>(pub T);
impl<T, const N: usize> MergeWith for [T; N]
where
T: MergeWith,
{
fn merge_with(self, other: impl Into<Self>) -> Self {
self.into_iter_fixed()
.zip(other.into())
.collect::<[_; N]>()
.map(|(a, b)| a.merge_with(b))
}
}
impl<T> MergeWith for Option<T>
where
T: PartialEq,
{
fn merge_with(self, other: impl Into<Self>) -> Self {
let other = other.into();
if self == other {
return self;
}
if self.is_some() && other.is_some() {
panic!("Can't merge two `Option`s that are both `Some`")
}
self.xor(other)
}
}
impl<T> MergeWith for Mergeable<Option<T>>
where
T: MergeWith,
{
fn merge_with(self, other: impl Into<Self>) -> Self {
let merged = match (self.0, other.into().0) {
(Some(a), Some(b)) => Some(a.merge_with(b)),
(a, b) => a.xor(b),
};
Self(merged)
}
}
impl<T> MergeWith for Vec<T> {
fn merge_with(self, other: impl Into<Self>) -> Self {
let other = other.into();
match (self.is_empty(), other.is_empty()) {
(true, true) => {
panic!("Can't merge `PartialHalfEdge`, if both have half-edges")
}
(true, false) => other,
(false, true) => self,
(false, false) => self, }
}
}
impl<T> MergeWith for Mergeable<Vec<T>> {
fn merge_with(mut self, other: impl Into<Self>) -> Self {
self.0.extend(other.into().0);
self
}
}
impl<T> MergeWith for Handle<T> {
fn merge_with(self, other: impl Into<Self>) -> Self {
if self.id() == other.into().id() {
return self;
}
panic!("Can't merge two distinct objects")
}
}