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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::account_address::AccountAddress;
#[cfg(any(test, feature = "fuzzing"))]
use rand::{rngs::OsRng, RngCore};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct EventKey {
creation_number: u64,
account_address: AccountAddress,
}
impl EventKey {
pub fn new(creation_number: u64, account_address: AccountAddress) -> Self {
Self {
creation_number,
account_address,
}
}
pub fn to_bytes(&self) -> Vec<u8> {
bcs::to_bytes(&self).unwrap()
}
pub fn get_creator_address(&self) -> AccountAddress {
self.account_address
}
pub fn get_creation_number(&self) -> u64 {
self.creation_number
}
#[cfg(any(test, feature = "fuzzing"))]
pub fn random() -> Self {
let mut rng = OsRng;
let salt = rng.next_u64();
EventKey::new(salt, AccountAddress::random())
}
}
impl fmt::LowerHex for EventKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
write!(f, "0x")?;
}
for byte in self.to_bytes() {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
impl fmt::Display for EventKey {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
write!(f, "{:x}", self)
}
}
#[derive(Clone, Copy, Debug)]
pub struct EventKeyParseError;
impl fmt::Display for EventKeyParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
write!(f, "unable to parse EventKey")
}
}
impl std::error::Error for EventKeyParseError {}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EventHandle {
count: u64,
key: EventKey,
}
impl EventHandle {
pub fn new(key: EventKey, count: u64) -> Self {
EventHandle { count, key }
}
pub fn key(&self) -> &EventKey {
&self.key
}
pub fn count(&self) -> u64 {
self.count
}
#[cfg(any(test, feature = "fuzzing"))]
pub fn random(count: u64) -> Self {
Self {
key: EventKey::random(),
count,
}
}
#[cfg(any(test, feature = "fuzzing"))]
pub fn count_mut(&mut self) -> &mut u64 {
&mut self.count
}
}