From a5bc39d025661ed94b8de69f53fc8092b9939214 Mon Sep 17 00:00:00 2001 From: Mac L Date: Fri, 4 Aug 2023 15:49:29 +1000 Subject: [PATCH] Implement `TreeHash` for `Option` --- tree_hash/src/impls.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tree_hash/src/impls.rs b/tree_hash/src/impls.rs index 277aedf..69657d3 100644 --- a/tree_hash/src/impls.rs +++ b/tree_hash/src/impls.rs @@ -205,6 +205,31 @@ impl TreeHash for Arc { } } +impl TreeHash for Option +where + T: TreeHash, +{ + fn tree_hash_type() -> TreeHashType { + TreeHashType::Container + } + + fn tree_hash_packed_encoding(&self) -> PackedEncoding { + unreachable!("Option should never be packed") + } + + fn tree_hash_packing_factor() -> usize { + unreachable!("Option should never be packed") + } + + fn tree_hash_root(&self) -> Hash256 { + let (selector, root) = match self { + None => (0, Hash256::zero()), + Some(t) => (1, t.tree_hash_root()), + }; + mix_in_selector(&root, selector).expect("an option cannot overflow the selector") + } +} + #[cfg(test)] mod test { use super::*; @@ -227,6 +252,18 @@ mod test { assert_eq!(one_arc.tree_hash_root(), one.tree_hash_root()); } + #[test] + fn option() { + assert_eq!( + None::.tree_hash_root(), + mix_in_selector(&Hash256::zero(), 0).unwrap() + ); + assert_eq!( + Some(42_u64).tree_hash_root(), + mix_in_selector(&42_u64.tree_hash_root(), 1).unwrap() + ); + } + #[test] fn int_to_bytes() { assert_eq!(int_to_hash256(0).as_bytes(), &[0; 32]);