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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use byteorder::{LittleEndian};
use rand::Rng;
use serde::{Serialize, Deserialize, de::DeserializeOwned};
use crate::serialization::cdr_serializer::to_bytes;
pub trait Keyed {
type K;
fn get_key(&self) -> Self::K;
fn get_hash(&self) -> u64
where
Self::K: Key,
{
let mut hasher = DefaultHasher::new();
self.get_key().hash(&mut hasher);
hasher.finish()
}
}
pub trait Key:
Eq + PartialEq + PartialOrd + Ord + Hash + Clone + Serialize + DeserializeOwned
{
fn into_hash_key(&self) -> u128 {
let cdr_bytes = match to_bytes::<Self, LittleEndian>(&self) {
Ok(b) => b,
_ => Vec::new(),
};
let digest = if cdr_bytes.len() > 16 {
md5::compute(&cdr_bytes).to_vec()
} else {
cdr_bytes
};
let mut digarr: [u8; 16] = [0; 16];
for i in 0..digest.len() {
digarr[i] = digest[i];
}
u128::from_le_bytes(digarr)
}
}
impl Key for () {
fn into_hash_key(&self) -> u128 {
0
}
}
impl<D: Keyed> Keyed for &D {
type K = D::K;
fn get_key(&self) -> Self::K {
(*self).get_key()
}
}
impl Key for bool {}
impl Key for char {}
impl Key for i8 {}
impl Key for i16 {}
impl Key for i32 {}
impl Key for i64 {}
impl Key for i128 {}
impl Key for isize {}
impl Key for u8 {}
impl Key for u16 {}
impl Key for u32 {}
impl Key for u64 {}
impl Key for u128 {}
impl Key for usize {}
impl Key for String {}
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct BuiltInTopicKey {
value: [i32; 3],
}
impl BuiltInTopicKey {
pub fn get_random_key() -> BuiltInTopicKey {
let mut rng = rand::thread_rng();
BuiltInTopicKey {
value: [rng.gen(), rng.gen(), rng.gen()],
}
}
pub fn default() -> BuiltInTopicKey {
BuiltInTopicKey { value: [0, 0, 0] }
}
}