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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use super::{StorageClearable, StorageMapper};
use crate::abi::{TypeAbi, TypeDescriptionContainer, TypeName};
use crate::api::{EndpointFinishApi, ErrorApi, StorageReadApi, StorageWriteApi};
use crate::io::EndpointResult;
use crate::storage::{storage_get, storage_set};
use crate::types::{BoxedBytes, MultiResultVec};
use alloc::vec::Vec;
use core::{marker::PhantomData, usize};
use elrond_codec::{TopDecode, TopEncode};
const ITEM_SUFFIX: &[u8] = b".item";
const LEN_SUFFIX: &[u8] = b".len";
fn compute_item_key(prefix: &[u8], index: usize) -> BoxedBytes {
let index_bytes = (index as u32).to_be_bytes();
BoxedBytes::from_concat(&[prefix, ITEM_SUFFIX, &index_bytes[..]])
}
pub struct VecMapper<SA, T>
where
SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
T: TopEncode + TopDecode + 'static,
{
api: SA,
main_key: BoxedBytes,
_phantom: core::marker::PhantomData<T>,
}
impl<SA, T> StorageMapper<SA> for VecMapper<SA, T>
where
SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
T: TopEncode + TopDecode,
{
fn new(api: SA, main_key: BoxedBytes) -> Self {
VecMapper {
api,
main_key,
_phantom: PhantomData,
}
}
}
impl<SA, T> StorageClearable for VecMapper<SA, T>
where
SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
T: TopEncode + TopDecode,
{
fn clear(&mut self) {
self.clear();
}
}
impl<SA, T> VecMapper<SA, T>
where
SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
T: TopEncode + TopDecode,
{
fn item_key(&self, index: usize) -> BoxedBytes {
compute_item_key(self.main_key.as_slice(), index)
}
fn len_key(&self) -> BoxedBytes {
BoxedBytes::from_concat(&[self.main_key.as_slice(), LEN_SUFFIX])
}
fn save_count(&self, new_len: usize) {
storage_set(self.api.clone(), self.len_key().as_slice(), &new_len);
}
pub fn len(&self) -> usize {
storage_get(self.api.clone(), self.len_key().as_slice())
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn push(&mut self, item: &T) -> usize {
let mut len = self.len();
len += 1;
storage_set(self.api.clone(), self.item_key(len).as_slice(), item);
self.save_count(len);
len
}
pub fn extend_from_slice(&mut self, items: &[T]) -> usize {
let mut len = self.len();
for item in items {
len += 1;
storage_set(self.api.clone(), self.item_key(len).as_slice(), item);
}
self.save_count(len);
len
}
pub fn get(&self, index: usize) -> T {
if index == 0 || index > self.len() {
self.api.signal_error(&b"index out of range"[..]);
}
self.get_unchecked(index)
}
pub fn get_unchecked(&self, index: usize) -> T {
storage_get(self.api.clone(), self.item_key(index).as_slice())
}
pub fn get_or_else<F: FnOnce() -> T>(self, index: usize, or_else: F) -> T {
if index == 0 || index > self.len() {
or_else()
} else {
self.get_unchecked(index)
}
}
pub fn item_is_empty_unchecked(&self, index: usize) -> bool {
self.api.storage_load_len(self.item_key(index).as_slice()) == 0
}
pub fn item_is_empty(&self, index: usize) -> bool {
if index == 0 || index > self.len() {
self.api.signal_error(&b"index out of range"[..]);
}
self.item_is_empty_unchecked(index)
}
pub fn set(&self, index: usize, item: &T) {
if index == 0 || index > self.len() {
self.api.signal_error(&b"index out of range"[..]);
}
self.set_unchecked(index, item);
}
fn set_unchecked(&self, index: usize, item: &T) {
storage_set(self.api.clone(), self.item_key(index).as_slice(), item);
}
pub fn clear_entry(&self, index: usize) {
if index == 0 || index > self.len() {
self.api.signal_error(&b"index out of range"[..]);
}
self.clear_entry_unchecked(index)
}
pub fn clear_entry_unchecked(&self, index: usize) {
self.api
.storage_store_slice_u8(self.item_key(index).as_slice(), &[]);
}
pub fn load_as_vec(&self) -> Vec<T> {
let len = self.len();
let mut result = Vec::with_capacity(len);
for i in 1..=len {
result.push(self.get(i));
}
result
}
pub fn clear(&mut self) {
let len = self.len();
for i in 1..=len {
self.api
.storage_store_slice_u8(self.item_key(i).as_slice(), &[]);
}
self.save_count(0);
}
}
impl<SA, T> EndpointResult for VecMapper<SA, T>
where
SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
T: TopEncode + TopDecode + EndpointResult,
{
type DecodeAs = MultiResultVec<T::DecodeAs>;
fn finish<FA>(&self, api: FA)
where
FA: EndpointFinishApi + Clone + 'static,
{
let v = self.load_as_vec();
MultiResultVec::<T>::from(v).finish(api);
}
}
impl<SA, T> TypeAbi for VecMapper<SA, T>
where
SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
T: TopEncode + TopDecode + TypeAbi,
{
fn type_name() -> TypeName {
crate::types::MultiResultVec::<T>::type_name()
}
fn provide_type_descriptions<TDC: TypeDescriptionContainer>(accumulator: &mut TDC) {
T::provide_type_descriptions(accumulator);
}
fn is_multi_arg_or_result() -> bool {
true
}
}