Skip to content

Commit

Permalink
Add TrieRoot<NodeHash> Option<[u8; 32]> From impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Jun 11, 2024
1 parent e420f65 commit 35367cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,17 @@ impl Display for NodeHash {
write!(f, "NodeHash({:?})", &self.bytes)
}
}

impl From<[u8; 32]> for NodeHash {
#[inline]
fn from(bytes: [u8; 32]) -> Self {
Self::new(bytes)
}
}

impl From<&[u8; 32]> for NodeHash {
#[inline]
fn from(bytes: &[u8; 32]) -> Self {
Self::new(*bytes)
}
}
20 changes: 20 additions & 0 deletions src/transaction/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ impl From<TrieRoot<NodeHash>> for Option<NodeHash> {
}
}

impl From<TrieRoot<NodeHash>> for Option<[u8; 32]> {
#[inline]
fn from(value: TrieRoot<NodeHash>) -> Self {
match value {
TrieRoot::Empty => None,
TrieRoot::Node(hash) => Some(hash.bytes),
}
}
}

impl From<Option<[u8; 32]>> for TrieRoot<NodeHash> {
#[inline]
fn from(hash: Option<[u8; 32]>) -> Self {
match hash {
Some(hash) => Self::Node(NodeHash::new(hash)),
None => Self::Empty,
}
}
}

/// A unmodified Node
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
Expand Down

0 comments on commit 35367cb

Please sign in to comment.