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
use std::marker::PhantomData;
use borsh::{BorshDeserialize, BorshSerialize};
use crate::env;
use crate::IntoStorageKey;
const ERR_VALUE_SERIALIZATION: &[u8] = b"Cannot serialize value with Borsh";
const ERR_VALUE_DESERIALIZATION: &[u8] = b"Cannot deserialize value with Borsh";
#[derive(BorshSerialize, BorshDeserialize)]
pub struct LazyOption<T> {
storage_key: Vec<u8>,
#[borsh_skip]
el: PhantomData<T>,
}
impl<T> LazyOption<T> {
pub fn is_some(&self) -> bool {
env::storage_has_key(&self.storage_key)
}
pub fn is_none(&self) -> bool {
!self.is_some()
}
fn get_raw(&self) -> Option<Vec<u8>> {
env::storage_read(&self.storage_key)
}
fn remove_raw(&mut self) -> bool {
env::storage_remove(&self.storage_key)
}
fn take_raw(&mut self) -> Option<Vec<u8>> {
if self.remove_raw() {
Some(env::storage_get_evicted().unwrap())
} else {
None
}
}
fn set_raw(&mut self, raw_value: &[u8]) -> bool {
env::storage_write(&self.storage_key, &raw_value)
}
fn replace_raw(&mut self, raw_value: &[u8]) -> Option<Vec<u8>> {
if self.set_raw(raw_value) {
Some(env::storage_get_evicted().unwrap())
} else {
None
}
}
}
impl<T> LazyOption<T>
where
T: BorshSerialize + BorshDeserialize,
{
pub fn new<S>(storage_key: S, value: Option<&T>) -> Self
where
S: IntoStorageKey,
{
let mut this = Self { storage_key: storage_key.into_storage_key(), el: PhantomData };
if let Some(value) = value {
this.set(&value);
}
this
}
fn serialize_value(value: &T) -> Vec<u8> {
match value.try_to_vec() {
Ok(x) => x,
Err(_) => env::panic(ERR_VALUE_SERIALIZATION),
}
}
fn deserialize_value(raw_value: &[u8]) -> T {
match T::try_from_slice(&raw_value) {
Ok(x) => x,
Err(_) => env::panic(ERR_VALUE_DESERIALIZATION),
}
}
pub fn remove(&mut self) -> bool {
self.remove_raw()
}
pub fn take(&mut self) -> Option<T> {
self.take_raw().map(|v| Self::deserialize_value(&v))
}
pub fn get(&self) -> Option<T> {
self.get_raw().map(|v| Self::deserialize_value(&v))
}
pub fn set(&mut self, value: &T) -> bool {
self.set_raw(&Self::serialize_value(value))
}
pub fn replace(&mut self, value: &T) -> Option<T> {
self.replace_raw(&Self::serialize_value(value)).map(|v| Self::deserialize_value(&v))
}
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::test_env;
#[test]
pub fn test_all() {
test_env::setup();
let mut a = LazyOption::new(b"a", None);
assert!(a.is_none());
a.set(&42u32);
assert!(a.is_some());
assert_eq!(a.get(), Some(42));
assert!(a.is_some());
assert_eq!(a.replace(&95), Some(42));
assert!(a.is_some());
assert_eq!(a.take(), Some(95));
assert!(a.is_none());
assert_eq!(a.replace(&105), None);
assert!(a.is_some());
assert_eq!(a.get(), Some(105));
assert!(a.remove());
assert!(a.is_none());
assert_eq!(a.get(), None);
assert_eq!(a.take(), None);
assert!(a.is_none());
}
#[test]
pub fn test_multi() {
test_env::setup();
let mut a = LazyOption::new(b"a", None);
let mut b = LazyOption::new(b"b", None);
assert!(a.is_none());
assert!(b.is_none());
a.set(&42u32);
assert!(b.is_none());
assert!(a.is_some());
assert_eq!(a.get(), Some(42));
b.set(&32u32);
assert!(a.is_some());
assert!(b.is_some());
assert_eq!(a.get(), Some(42));
assert_eq!(b.get(), Some(32));
}
#[test]
pub fn test_init_value() {
test_env::setup();
let a = LazyOption::new(b"a", Some(&42u32));
assert!(a.is_some());
assert_eq!(a.get(), Some(42));
}
}