Is there a way to have a merge
function that uses &mut self
, consume inner enum value and push it onto a new Vector when merging? I keep fighting compiler on this -- PEBKAC, but where?
If this is not possible as is, can it be fixed with implementing Clone
trait on Val
? (but NOT the Copy
trait!)
struct Val();
enum Foo {
One(Val),
Many(Vec<Val>),
}
impl Foo {
pub fn merge(&mut self, other: Self) {
match (*self, other) {
// ^^^^^
// move occurs because `*self` has type `Foo`, which does not implement the `Copy` trait
// cannot move out of `*self` which is behind a mutable reference
//
(Self::One(a), Self::One(b)) => *self = Self::Many(vec![a, b]),
(Self::One(a), Self::Many(mut b)) => {
b.insert(0, a);
*self = Self::Many(b)
}
(Self::Many(mut a), Self::One(b)) => {
a.push(b);
}
(Self::Many(mut a), Self::Many(b)) => {
a.extend(b);
}
};
}
}