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
use crate::traits::{
pull_packed_root_opt,
push_packed_root,
ExtKeyPtr,
KeyPtr,
PackedLayout,
SpreadAllocate,
SpreadLayout,
};
use core::marker::PhantomData;
use ink_env::hash::{
Blake2x256,
HashOutput,
};
use ink_primitives::Key;
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
#[derive(Default)]
pub struct Mapping<K, V> {
offset_key: Key,
_marker: PhantomData<fn() -> (K, V)>,
}
impl<K, V> core::fmt::Debug for Mapping<K, V> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct("Mapping")
.field("offset_key", &self.offset_key)
.finish()
}
}
impl<K, V> Mapping<K, V> {
fn new(offset_key: Key) -> Self {
Self {
offset_key,
_marker: Default::default(),
}
}
}
impl<K, V> Mapping<K, V>
where
K: PackedLayout,
V: PackedLayout,
{
#[inline]
pub fn insert<Q, R>(&mut self, key: Q, value: &R)
where
Q: scale::EncodeLike<K>,
R: scale::EncodeLike<V> + PackedLayout,
{
push_packed_root(value, &self.storage_key(key));
}
#[inline]
pub fn get<Q>(&self, key: Q) -> Option<V>
where
Q: scale::EncodeLike<K>,
{
pull_packed_root_opt(&self.storage_key(key))
}
fn storage_key<Q>(&self, key: Q) -> Key
where
Q: scale::EncodeLike<K>,
{
let encodedable_key = (&self.offset_key, key);
let mut output = <Blake2x256 as HashOutput>::Type::default();
ink_env::hash_encoded::<Blake2x256, _>(&encodedable_key, &mut output);
output.into()
}
}
impl<K, V> SpreadLayout for Mapping<K, V> {
const FOOTPRINT: u64 = 1;
const REQUIRES_DEEP_CLEAN_UP: bool = false;
#[inline]
fn pull_spread(ptr: &mut KeyPtr) -> Self {
Self::new(*ExtKeyPtr::next_for::<Self>(ptr))
}
#[inline]
fn push_spread(&self, ptr: &mut KeyPtr) {
ptr.advance_by(Self::FOOTPRINT);
}
#[inline]
fn clear_spread(&self, ptr: &mut KeyPtr) {
ptr.advance_by(Self::FOOTPRINT);
}
}
impl<K, V> SpreadAllocate for Mapping<K, V> {
#[inline]
fn allocate_spread(ptr: &mut KeyPtr) -> Self {
Self::new(*ExtKeyPtr::next_for::<Self>(ptr))
}
}
#[cfg(feature = "std")]
const _: () = {
use crate::traits::StorageLayout;
use ink_metadata::layout::{
CellLayout,
Layout,
LayoutKey,
};
impl<K, V> StorageLayout for Mapping<K, V>
where
K: scale_info::TypeInfo + 'static,
V: scale_info::TypeInfo + 'static,
{
fn layout(key_ptr: &mut KeyPtr) -> Layout {
Layout::Cell(CellLayout::new::<Self>(LayoutKey::from(
key_ptr.advance_by(1),
)))
}
}
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn insert_and_get_work() {
ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
let mut mapping: Mapping<u8, _> = Mapping::new([0u8; 32].into());
mapping.insert(&1, &2);
assert_eq!(mapping.get(&1), Some(2));
Ok(())
})
.unwrap()
}
#[test]
fn gets_default_if_no_key_set() {
ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
let mapping: Mapping<u8, u8> = Mapping::new([0u8; 32].into());
assert_eq!(mapping.get(&1), None);
Ok(())
})
.unwrap()
}
}