feat: implement get_size_hint for Smt (#331)

This commit is contained in:
Philipp Gackstatter
2024-09-26 18:13:50 +02:00
committed by GitHub
parent c2eb38c236
commit 8adc0ab418
2 changed files with 12 additions and 2 deletions

View File

@@ -314,6 +314,14 @@ impl Serializable for Smt {
target.write(value);
}
}
fn get_size_hint(&self) -> usize {
let entries_count = self.entries().count();
// Each entry is the size of a digest plus a word.
entries_count.get_size_hint()
+ entries_count * (RpoDigest::SERIALIZED_SIZE + EMPTY_WORD.get_size_hint())
}
}
impl Deserializable for Smt {
@@ -339,6 +347,7 @@ fn test_smt_serialization_deserialization() {
let smt_default = Smt::default();
let bytes = smt_default.to_bytes();
assert_eq!(smt_default, Smt::read_from_bytes(&bytes).unwrap());
assert_eq!(bytes.len(), smt_default.get_size_hint());
// Smt with values
let smt_leaves_2: [(RpoDigest, Word); 2] = [
@@ -355,4 +364,5 @@ fn test_smt_serialization_deserialization() {
let bytes = smt.to_bytes();
assert_eq!(smt, Smt::read_from_bytes(&bytes).unwrap());
assert_eq!(bytes.len(), smt.get_size_hint());
}