Skip to content

Commit 3ea0d5b

Browse files
authored
Better Hash and PartialEq for Keys and AnchoredKeys (#277)
1 parent 273d803 commit 3ea0d5b

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/middleware/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl From<&Value> for Hash {
443443
}
444444
}
445445

446-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
446+
#[derive(Clone, Debug, Eq)]
447447
pub struct Key {
448448
name: String,
449449
hash: Hash,
@@ -466,6 +466,18 @@ impl Key {
466466
}
467467
}
468468

469+
impl hash::Hash for Key {
470+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
471+
self.hash.hash(state);
472+
}
473+
}
474+
475+
impl PartialEq for Key {
476+
fn eq(&self, other: &Self) -> bool {
477+
self.hash == other.hash
478+
}
479+
}
480+
469481
// A Key can easily be created from a string-like type
470482
impl<T> From<T> for Key
471483
where
@@ -531,7 +543,7 @@ impl JsonSchema for Key {
531543
}
532544
}
533545

534-
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
546+
#[derive(Clone, Debug, Eq, Serialize, Deserialize, JsonSchema)]
535547
#[serde(rename_all = "camelCase")]
536548
pub struct AnchoredKey {
537549
pub pod_id: PodId,
@@ -544,6 +556,19 @@ impl AnchoredKey {
544556
}
545557
}
546558

559+
impl hash::Hash for AnchoredKey {
560+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
561+
self.pod_id.hash(state);
562+
self.key.hash.hash(state);
563+
}
564+
}
565+
566+
impl PartialEq for AnchoredKey {
567+
fn eq(&self, other: &Self) -> bool {
568+
self.pod_id == other.pod_id && self.key.hash == other.key.hash
569+
}
570+
}
571+
547572
impl fmt::Display for AnchoredKey {
548573
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
549574
self.pod_id.fmt(f)?;

0 commit comments

Comments
 (0)