-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst_entry.rs
More file actions
66 lines (59 loc) · 1.67 KB
/
Copy pathmst_entry.rs
File metadata and controls
66 lines (59 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! MST Entry - represents an entry within an MST node.
//!
//! Each entry contains a key/value pair and optionally a right subtree.
use super::mst_node::MstNode;
/// Represents an entry in an MST node.
#[derive(Debug, Clone)]
pub struct MstEntry {
/// The record key
pub key: String,
/// The value (typically a CID in base32 format)
pub value: String,
/// Optional right subtree
pub right_tree: Option<Box<MstNode>>,
}
impl MstEntry {
/// Creates a new MstEntry with no right subtree.
pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
Self {
key: key.into(),
value: value.into(),
right_tree: None,
}
}
/// Creates a new MstEntry with a right subtree.
pub fn with_right_tree(
key: impl Into<String>,
value: impl Into<String>,
right_tree: MstNode,
) -> Self {
Self {
key: key.into(),
value: value.into(),
right_tree: Some(Box::new(right_tree)),
}
}
}
impl PartialEq for MstEntry {
fn eq(&self, other: &Self) -> bool {
if self.key != other.key {
return false;
}
if self.value != other.value {
return false;
}
match (&self.right_tree, &other.right_tree) {
(None, None) => true,
(Some(a), Some(b)) => a == b,
_ => false,
}
}
}
impl Eq for MstEntry {}
impl std::hash::Hash for MstEntry {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.key.hash(state);
self.value.hash(state);
// Note: right_tree is not hashed to avoid infinite recursion concerns
}
}