From 7225960363f7d66176178c63ddd21f9500887bc9 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 31 Oct 2022 21:36:05 +0100 Subject: [PATCH] Compound PartialEq --- implementations/rust/oo/src/lib.rs | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/implementations/rust/oo/src/lib.rs b/implementations/rust/oo/src/lib.rs index b724062..3e356ff 100644 --- a/implementations/rust/oo/src/lib.rs +++ b/implementations/rust/oo/src/lib.rs @@ -173,6 +173,21 @@ impl<'a, D: Domain> Hash for dyn Value + 'a { } } +fn iters_eq<'a, D: Domain>( + mut i1: Box> + 'a>, + mut i2: Box> + 'a>, +) -> bool { + loop { + match i1.next() { + None => return i2.next().is_none(), + Some(v1) => match i2.next() { + None => return false, + Some(v2) => if v1 != v2 { return false; }, + } + } + } +} + impl<'a, D: Domain> PartialEq for dyn Value + 'a { fn eq(&self, other: &Self) -> bool { let cls = self.value_class(); @@ -195,7 +210,23 @@ impl<'a, D: Domain> PartialEq for dyn Value + 'a { self.as_symbol().unwrap() == other.as_symbol().unwrap(), } ValueClass::Compound(c) => match c { - _ => todo!(), + CompoundClass::Record => { + if self.label().unwrap() != other.label().unwrap() { return false; } + iters_eq(self.iter().unwrap(), other.iter().unwrap()) + } + CompoundClass::Sequence => { + iters_eq(self.iter().unwrap(), other.iter().unwrap()) + } + CompoundClass::Set => { + let s1 = self.iter().unwrap().collect::>(); + let s2 = other.iter().unwrap().collect::>(); + s1 == s2 + } + CompoundClass::Dictionary => { + let d1 = self.entries().unwrap().collect::>(); + let d2 = other.entries().unwrap().collect::>(); + d1 == d2 + } } ValueClass::Embedded => self.embedded().unwrap() == other.embedded().unwrap(), }