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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
mod impls;
use borsh::{BorshDeserialize, BorshSerialize};
use once_cell::unsync::OnceCell;
use crate::env;
use crate::store::lazy::{load_and_deserialize, serialize_and_store};
use crate::utils::{CacheEntry, EntryState};
use crate::IntoStorageKey;
#[derive(BorshSerialize, BorshDeserialize)]
pub struct LazyOption<T>
where
T: BorshSerialize,
{
prefix: Box<[u8]>,
#[borsh_skip]
cache: OnceCell<CacheEntry<T>>,
}
impl<T> LazyOption<T>
where
T: BorshSerialize,
{
pub fn new<S>(prefix: S, value: Option<T>) -> Self
where
S: IntoStorageKey,
{
let cache = match value {
Some(value) => CacheEntry::new_modified(Some(value)),
None => CacheEntry::new_cached(None),
};
Self { prefix: prefix.into_storage_key().into_boxed_slice(), cache: OnceCell::from(cache) }
}
pub fn set(&mut self, value: Option<T>) {
if let Some(v) = self.cache.get_mut() {
*v.value_mut() = value;
} else {
self.cache
.set(CacheEntry::new_modified(value))
.unwrap_or_else(|_| env::abort());
}
}
pub fn flush(&mut self) {
if let Some(v) = self.cache.get_mut() {
if !v.is_modified() {
return;
}
match v.value().as_ref() {
Some(value) => serialize_and_store(&self.prefix, value),
None => {
env::storage_remove(&self.prefix);
}
}
v.replace_state(EntryState::Cached);
}
}
}
impl<T> LazyOption<T>
where
T: BorshSerialize + BorshDeserialize,
{
pub fn get(&self) -> &Option<T> {
let entry = self.cache.get_or_init(|| load_and_deserialize(&self.prefix));
entry.value()
}
pub fn get_mut(&mut self) -> &mut Option<T> {
self.cache.get_or_init(|| load_and_deserialize(&self.prefix));
let entry = self.cache.get_mut().unwrap_or_else(|| env::abort());
entry.value_mut()
}
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_lazy_option() {
let mut a = LazyOption::new(b"a", None);
assert!(a.is_none());
assert!(!env::storage_has_key(b"a"));
a.set(Some(42u32));
assert!(a.is_some());
assert_eq!(a.get(), &Some(42));
a.flush();
assert!(env::storage_has_key(b"a"));
assert_eq!(u32::try_from_slice(&env::storage_read(b"a").unwrap()).unwrap(), 42);
*a = Some(49u32);
assert!(a.is_some());
assert_eq!(a.get(), &Some(49));
let old = a.replace(69u32);
assert!(a.is_some());
assert_eq!(old, Some(49));
let taken = a.take();
assert!(a.is_none());
assert_eq!(taken, Some(69));
drop(a);
assert!(!env::storage_has_key(b"a"));
}
#[test]
pub fn test_debug() {
let mut lazy_option = LazyOption::new(b"m", None);
if cfg!(feature = "expensive-debug") {
assert_eq!(format!("{:?}", lazy_option), "None");
} else {
assert_eq!(
format!("{:?}", lazy_option),
"LazyOption { storage_key: [109], cache: Some(CacheEntry { value: None, state: Cached }) }"
);
}
*lazy_option = Some(1u8);
if cfg!(feature = "expensive-debug") {
assert_eq!(format!("{:?}", lazy_option), "Some(1)");
} else {
assert_eq!(
format!("{:?}", lazy_option),
"LazyOption { storage_key: [109], cache: Some(CacheEntry { value: Some(1), state: Modified }) }"
);
}
let serialized = borsh::to_vec(&lazy_option).unwrap();
drop(lazy_option);
let lazy_option = LazyOption::<u8>::try_from_slice(&serialized).unwrap();
if cfg!(feature = "expensive-debug") {
assert_eq!(format!("{:?}", lazy_option), "Some(1)");
} else {
assert_eq!(
format!("{:?}", lazy_option),
"LazyOption { storage_key: [109], cache: None }"
);
}
}
}