Better Hash and PartialEq for Keys and AnchoredKeys (#277)

This commit is contained in:
Rob Knight 2025-06-11 13:25:08 +02:00 committed by GitHub
parent 273d803ebd
commit 3ea0d5be71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -443,7 +443,7 @@ impl From<&Value> for Hash {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, Eq)]
pub struct Key { pub struct Key {
name: String, name: String,
hash: Hash, hash: Hash,
@ -466,6 +466,18 @@ impl Key {
} }
} }
impl hash::Hash for Key {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.hash.hash(state);
}
}
impl PartialEq for Key {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
}
// A Key can easily be created from a string-like type // A Key can easily be created from a string-like type
impl<T> From<T> for Key impl<T> From<T> for Key
where where
@ -531,7 +543,7 @@ impl JsonSchema for Key {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Debug, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct AnchoredKey { pub struct AnchoredKey {
pub pod_id: PodId, pub pod_id: PodId,
@ -544,6 +556,19 @@ impl AnchoredKey {
} }
} }
impl hash::Hash for AnchoredKey {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.pod_id.hash(state);
self.key.hash.hash(state);
}
}
impl PartialEq for AnchoredKey {
fn eq(&self, other: &Self) -> bool {
self.pod_id == other.pod_id && self.key.hash == other.key.hash
}
}
impl fmt::Display for AnchoredKey { impl fmt::Display for AnchoredKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.pod_id.fmt(f)?; self.pod_id.fmt(f)?;