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
67
68
69
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use std::fmt;
use std::hash::{Hash, Hasher};
use crate::stable_hasher;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Svh {
hash: u64,
}
impl Svh {
pub fn new(hash: u64) -> Svh {
Svh { hash }
}
pub fn as_u64(&self) -> u64 {
self.hash
}
pub fn to_string(&self) -> String {
format!("{:016x}", self.hash)
}
}
impl Hash for Svh {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.hash.to_le().hash(state);
}
}
impl fmt::Display for Svh {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&self.to_string())
}
}
impl<S: Encoder> Encodable<S> for Svh {
fn encode(&self, s: &mut S) {
s.emit_u64(self.as_u64().to_le());
}
}
impl<D: Decoder> Decodable<D> for Svh {
fn decode(d: &mut D) -> Svh {
Svh::new(u64::from_le(d.read_u64()))
}
}
impl<T> stable_hasher::HashStable<T> for Svh {
#[inline]
fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) {
let Svh { hash } = *self;
hash.hash_stable(ctx, hasher);
}
}