forest_hash_utils/
key.rs

1use super::Hash;
2use serde::{Deserialize, Serialize};
3use std::borrow::Borrow;
4use std::hash::Hasher;
5use std::ops::Deref;
6
7/// Key type to be used to serialize as byte string instead of a `u8` array.
8/// This type is used as a default for the `Hamt` as this is the only allowed type
9/// with the go implementation.
10#[derive(Eq, PartialOrd, Clone, Debug, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct BytesKey(#[serde(with = "serde_bytes")] pub Vec<u8>);
13
14impl PartialEq for BytesKey {
15    fn eq(&self, other: &Self) -> bool {
16        self.0 == other.0
17    }
18}
19
20impl Hash for BytesKey {
21    fn hash<H: Hasher>(&self, state: &mut H) {
22        state.write(&self.0);
23    }
24}
25
26impl Borrow<[u8]> for BytesKey {
27    fn borrow(&self) -> &[u8] {
28        &self.0
29    }
30}
31
32impl Borrow<Vec<u8>> for BytesKey {
33    fn borrow(&self) -> &Vec<u8> {
34        &self.0
35    }
36}
37
38impl Deref for BytesKey {
39    type Target = Vec<u8>;
40    fn deref(&self) -> &Self::Target {
41        &self.0
42    }
43}
44
45impl From<Vec<u8>> for BytesKey {
46    fn from(bz: Vec<u8>) -> Self {
47        BytesKey(bz)
48    }
49}
50
51impl From<&[u8]> for BytesKey {
52    fn from(s: &[u8]) -> Self {
53        Self(s.to_vec())
54    }
55}
56
57impl From<&str> for BytesKey {
58    fn from(s: &str) -> Self {
59        Self::from(s.as_bytes())
60    }
61}