scsys_crypto/hash/h160/
impl_convert.rs

1/*
2    Appellation: impl_convert <module>
3    Contrib: @FL03
4*/
5use super::H160;
6use crate::GenericHash;
7
8impl FromIterator<u8> for H160 {
9    fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
10        let digest = iter.into_iter().collect::<Vec<u8>>();
11        crate::digest_to_hash::<20>(&digest).into()
12    }
13}
14
15impl IntoIterator for H160 {
16    type Item = u8;
17    type IntoIter = std::array::IntoIter<u8, 20>;
18
19    fn into_iter(self) -> Self::IntoIter {
20        self.0.into_iter()
21    }
22}
23
24impl<T> From<&T> for H160
25where
26    T: AsRef<[u8]>,
27{
28    fn from(data: &T) -> H160 {
29        let mut buffer: [u8; 20] = [0; 20];
30        buffer[..].copy_from_slice(data.as_ref());
31        H160(buffer)
32    }
33}
34
35impl From<[u8; 20]> for H160 {
36    #[inline]
37    fn from(value: [u8; 20]) -> Self {
38        Self(value)
39    }
40}
41
42impl From<Vec<u8>> for H160 {
43    fn from(input: Vec<u8>) -> H160 {
44        crate::digest_to_hash::<20>(&input).into()
45    }
46}
47
48impl From<GenericHash> for H160 {
49    fn from(data: GenericHash) -> H160 {
50        data.as_slice().to_owned().into()
51    }
52}
53
54impl From<H160> for [u8; 20] {
55    #[inline]
56    fn from(value: H160) -> Self {
57        value.0
58    }
59}
60
61impl<'a> From<&'a H160> for &'a [u8; 20] {
62    #[inline]
63    fn from(value: &'a H160) -> Self {
64        &value.0
65    }
66}
67
68impl From<H160> for Vec<u8> {
69    fn from(input: H160) -> Vec<u8> {
70        input.0.to_vec()
71    }
72}