Skip to content

Commit

Permalink
A few more Eq fixes (#340)
Browse files Browse the repository at this point in the history
* Derive Eq,Hash for HashableValue and ContainerValue

* Don't derive Eq for CustomSerialized; implement equal_consts

* Test equality
  • Loading branch information
acl-cqc authored Aug 3, 2023
1 parent a7502a2 commit c77e661
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ pub trait CustomConst:
impl_downcast!(CustomConst);
impl_box_clone!(CustomConst, CustomConstBoxClone);

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
// Don't derive Eq here - the yaml could contain floats etc.
// (Perhaps we could derive Eq if-and-only-if "typ.tag() == TypeTag::Hashable"!)
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
struct CustomSerialized {
typ: CustomType,
value: serde_yaml::Value,
Expand All @@ -325,6 +327,10 @@ impl CustomConst for CustomSerialized {
Err(CustomCheckFail::TypeMismatch(typ.clone(), self.typ.clone()))
}
}

fn equal_consts(&self, other: &dyn CustomConst) -> bool {
Some(self) == other.downcast_ref()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -444,5 +450,7 @@ mod test {
let t: SimpleType = typ_float.clone().into();
assert_matches!(val.check_type(&t.try_into().unwrap()),
Err(ConstTypeError::CustomCheckFail(CustomCheckFail::TypeMismatch(a, b))) => a == typ_int && b == typ_float);

assert_eq!(val, val);
}
}
4 changes: 2 additions & 2 deletions src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{

/// A constant value/instance of a [HashableType]. Note there is no
/// equivalent of [HashableType::Variable]; we can't have instances of that.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum HashableValue {
/// A string, i.e. corresponding to [HashableType::String]
String(String),
Expand Down Expand Up @@ -97,7 +97,7 @@ impl ValueOfType for HashableValue {
/// resolved to concrete types in order to create instances (values),
/// nor to [Container::Opaque], which is left to classes for broader
/// sets of values (see e.g. [ConstValue::Opaque])
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum ContainerValue<T> {
/// A [Container::Array] or [Container::Tuple] or [Container::List]
Sequence(Vec<T>),
Expand Down

0 comments on commit c77e661

Please sign in to comment.