ore/state/
hash.rs

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
use std::{fmt, mem::transmute};

use bytemuck::{Pod, Zeroable};
use solana_program::keccak::{Hash as KeccakHash, HASH_BYTES};

use crate::impl_to_bytes;

/// Hash is an equivalent type to solana_program::keccak::Hash which supports bytemuck serialization.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Hash(pub [u8; HASH_BYTES]);

impl From<KeccakHash> for Hash {
    #[inline(always)]
    fn from(value: KeccakHash) -> Self {
        unsafe { transmute(value) }
    }
}

impl From<Hash> for KeccakHash {
    #[inline(always)]
    fn from(value: Hash) -> Self {
        unsafe { transmute(value) }
    }
}

impl fmt::Display for Hash {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", bs58::encode(self.0).into_string())
    }
}

impl_to_bytes!(Hash);