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
#![allow(clippy::from_over_into)]
use crate::{
hash::{hasher, hashes::H256, Hashable},
GenericHash,
};
use std::string::ToString;
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct Hash(pub GenericHash);
impl Hash {
pub fn new<T: ToString>(data: T) -> Self {
Self(hasher(&data))
}
}
impl Hashable for Hash {
fn hash(&self) -> H256 {
self.clone().into()
}
}
impl std::convert::From<GenericHash> for Hash {
fn from(data: GenericHash) -> Self {
Self(data)
}
}
impl std::convert::From<&Hash> for Hash {
fn from(data: &Hash) -> Self {
Self::new(data.clone())
}
}
impl std::convert::Into<Vec<u8>> for Hash {
fn into(self) -> Vec<u8> {
self.0.as_slice().to_owned()
}
}
impl std::convert::Into<H256> for Hash {
fn into(self) -> H256 {
H256::from(self.0.as_slice().to_owned())
}
}
impl std::fmt::Display for Hash {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:x}", self.0)
}
}