scsys_crypto/hash/traits/
raw_hash.rs

1/*
2    appellation: raw_hash <module>
3    authors: @FL03
4*/
5/// [`RawHash`] defines a common interface for raw hash types.
6pub trait RawHash {
7    private!();
8}
9/// [`SizedHash`] extends [`RawHash`] to include a constant size for the hash output.
10pub trait SizedHash: RawHash {
11    const N: usize;
12
13    fn size(&self) -> usize {
14        Self::N
15    }
16}
17
18/*
19 ************* Implementations *************
20*/
21use crate::hash::{GenericHash, H160, H256};
22
23macro_rules! impl_raw_hash {
24    ($($t:ty),* $(,)?) => {
25        $(
26            impl_raw_hash!(@impl $t);
27        )*
28    };
29    ($($t:ty: $n:literal),* $(,)?) => {
30        $(
31            impl_raw_hash!(@sized $t => $n);
32        )*
33    };
34    (@impl $type:ty) => {
35        impl $crate::hash::traits::RawHash for $type {
36            seal!();
37        }
38    };
39    (@sized $type:ty => $n:literal) => {
40        impl_raw_hash!(@impl $type);
41
42        impl $crate::hash::traits::SizedHash for $type {
43            const N: usize = $n;
44        }
45    };
46}
47
48impl_raw_hash! {
49    GenericHash,
50    [u8],
51}
52
53impl_raw_hash! {
54    [u8; 20]: 20,
55    [u8; 32]: 32,
56    H160: 20,
57    H256: 32,
58}
59
60#[cfg(feature = "alloc")]
61impl_raw_hash! {
62    alloc::vec::Vec<u8>,
63}