Skip to main content

hara_native/lang/protocol/
ihashcached.rs

1use super::ihash::{HashType, IHash};
2use hara_protocol_macros::hara_protocol;
3
4#[hara_protocol(
5    namespace = "std.protocol.ihashcached",
6    name = "IHashCached",
7    parents = ["IHash"]
8)]
9pub trait IHashCached: IHash {
10    #[hara_method(value = "hash-current", arity = 1)]
11    fn hash_current(&self) -> u64;
12    #[hara_method(value = "hash-put", arity = 2)]
13    fn hash_put(&self, hash: u64);
14
15    fn hash_cached(&self) -> u64 {
16        let current = self.hash_current();
17        if current != 0 {
18            return current;
19        }
20        let hash = self.hash();
21        self.hash_put(hash);
22        hash
23    }
24
25    fn hash_cached_as(&self, hash_type: HashType) -> u64 {
26        if hash_type == HashType::Rapid {
27            self.hash_cached()
28        } else {
29            self.hash_calc(hash_type)
30        }
31    }
32}