Skip to content

Commit 0f7b0e1

Browse files
committed
[core] initial implementation and setup of the new storage traits
1 parent 321fe03 commit 0f7b0e1

File tree

5 files changed

+511
-0
lines changed

5 files changed

+511
-0
lines changed

core/src/storage/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub mod cell;
7474
pub mod chunk;
7575
mod collections;
7676
mod flush;
77+
mod traits;
7778
mod value;
7879

7980
pub use self::{
@@ -100,6 +101,12 @@ pub use self::{
100101
},
101102
},
102103
flush::Flush,
104+
traits::{
105+
KeyPtr,
106+
Pull,
107+
Push,
108+
StorageSize,
109+
},
103110
};
104111

105112
#[doc(inline)]

core/src/storage/traits/mod.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#[rustfmt::skip]
16+
macro_rules! forward_supported_array_lens {
17+
( $mac:ident ) => {
18+
$mac! {
19+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
20+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
21+
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
22+
30, 31, 32,
23+
64, 96, 128, 256, 512, 1024, 2048, 4096,
24+
8192, 16384,
25+
}
26+
};
27+
}
28+
29+
mod push;
30+
mod pull;
31+
mod storage_size;
32+
33+
use ink_primitives::Key;
34+
35+
pub use self::{
36+
storage_size::StorageSize,
37+
push::Push,
38+
pull::Pull,
39+
};
40+
41+
/// A key pointer.
42+
///
43+
/// Mainly used by [`Pull`] and [`Push`] traits in order to provide
44+
/// a streamlined interface for accessing the underlying [`Key`].
45+
pub struct KeyPtr {
46+
/// The underlying key.
47+
key: Key,
48+
}
49+
50+
impl KeyPtr {
51+
/// Advances the key by the given amount derive by `T` and its `StorageSize`
52+
/// and returns the next `Key` for usage by the storage element.
53+
pub fn next_for<T>(&mut self) -> Key
54+
where
55+
T: StorageSize,
56+
{
57+
let copy = self.key;
58+
self.key += <T as StorageSize>::SIZE;
59+
copy
60+
}
61+
}

core/src/storage/traits/pull.rs

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use super::{
16+
KeyPtr,
17+
StorageSize,
18+
};
19+
20+
/// Pulls the associated key values from the contract storage and forms `Self`.
21+
pub trait Pull {
22+
fn pull(key_ptr: &mut KeyPtr) -> Self;
23+
}
24+
25+
fn pull_single_cell<T>(key_ptr: &mut KeyPtr) -> T
26+
where
27+
T: StorageSize + scale::Decode,
28+
{
29+
crate::env::get_contract_storage::<T>(key_ptr.next_for::<T>())
30+
.expect("storage entry was empty")
31+
.expect("could not properly decode storage entry")
32+
}
33+
34+
macro_rules! impl_pull_for_primitive {
35+
( $($ty:ty),* $(,)? ) => {
36+
$(
37+
impl Pull for $ty {
38+
fn pull(key_ptr: &mut KeyPtr) -> Self {
39+
pull_single_cell::<$ty>(key_ptr)
40+
}
41+
}
42+
)*
43+
};
44+
}
45+
impl_pull_for_primitive!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
46+
47+
macro_rules! impl_pull_for_array {
48+
( $($len:literal),* $(,)? ) => {
49+
$(
50+
impl<T> Pull for [T; $len]
51+
where
52+
[T; $len]: scale::Decode,
53+
{
54+
fn pull(key_ptr: &mut KeyPtr) -> Self {
55+
pull_single_cell::<[T; $len]>(key_ptr)
56+
}
57+
}
58+
)*
59+
}
60+
}
61+
forward_supported_array_lens!(impl_pull_for_array);
62+
63+
macro_rules! impl_pull_tuple {
64+
( $($frag:ident),* $(,)? ) => {
65+
impl<$($frag),*> Pull for ($($frag),* ,)
66+
where
67+
( $($frag),* ,): scale::Decode,
68+
{
69+
fn pull(key_ptr: &mut KeyPtr) -> Self {
70+
pull_single_cell::<($($frag),* ,)>(key_ptr)
71+
}
72+
}
73+
}
74+
}
75+
impl_pull_tuple!(A);
76+
impl_pull_tuple!(A, B);
77+
impl_pull_tuple!(A, B, C);
78+
impl_pull_tuple!(A, B, C, D);
79+
impl_pull_tuple!(A, B, C, D, E);
80+
impl_pull_tuple!(A, B, C, D, E, F);
81+
impl_pull_tuple!(A, B, C, D, E, F, G);
82+
impl_pull_tuple!(A, B, C, D, E, F, G, H);
83+
impl_pull_tuple!(A, B, C, D, E, F, G, H, I);
84+
impl_pull_tuple!(A, B, C, D, E, F, G, H, I, J);
85+
86+
impl Pull for () {
87+
fn pull(_key_ptr: &mut KeyPtr) -> Self {
88+
()
89+
}
90+
}
91+
92+
impl<T> Pull for core::marker::PhantomData<T> {
93+
fn pull(_key_ptr: &mut KeyPtr) -> Self {
94+
Default::default()
95+
}
96+
}
97+
98+
impl<T> Pull for Option<T>
99+
where
100+
Self: scale::Decode,
101+
{
102+
fn pull(key_ptr: &mut KeyPtr) -> Self {
103+
pull_single_cell::<Self>(key_ptr)
104+
}
105+
}
106+
107+
impl<T, E> Pull for Result<T, E>
108+
where
109+
Self: scale::Decode,
110+
{
111+
fn pull(key_ptr: &mut KeyPtr) -> Self {
112+
pull_single_cell::<Self>(key_ptr)
113+
}
114+
}
115+
116+
impl<T> Pull for ink_prelude::vec::Vec<T>
117+
where
118+
Self: scale::Decode,
119+
{
120+
fn pull(key_ptr: &mut KeyPtr) -> Self {
121+
pull_single_cell::<Self>(key_ptr)
122+
}
123+
}
124+
125+
impl Pull for ink_prelude::string::String
126+
where
127+
Self: scale::Decode,
128+
{
129+
fn pull(key_ptr: &mut KeyPtr) -> Self {
130+
pull_single_cell::<Self>(key_ptr)
131+
}
132+
}
133+
134+
impl<T> Pull for ink_prelude::boxed::Box<T>
135+
where
136+
Self: scale::Decode,
137+
{
138+
fn pull(key_ptr: &mut KeyPtr) -> Self {
139+
pull_single_cell::<Self>(key_ptr)
140+
}
141+
}
142+
143+
impl<T> Pull for ink_prelude::collections::BTreeSet<T>
144+
where
145+
Self: scale::Decode,
146+
{
147+
fn pull(key_ptr: &mut KeyPtr) -> Self {
148+
pull_single_cell::<Self>(key_ptr)
149+
}
150+
}
151+
152+
impl<T> Pull for ink_prelude::collections::BinaryHeap<T>
153+
where
154+
Self: scale::Decode,
155+
{
156+
fn pull(key_ptr: &mut KeyPtr) -> Self {
157+
pull_single_cell::<Self>(key_ptr)
158+
}
159+
}
160+
161+
impl<T> Pull for ink_prelude::collections::LinkedList<T>
162+
where
163+
Self: scale::Decode,
164+
{
165+
fn pull(key_ptr: &mut KeyPtr) -> Self {
166+
pull_single_cell::<Self>(key_ptr)
167+
}
168+
}
169+
170+
impl<T> Pull for ink_prelude::collections::VecDeque<T>
171+
where
172+
Self: scale::Decode,
173+
{
174+
fn pull(key_ptr: &mut KeyPtr) -> Self {
175+
pull_single_cell::<Self>(key_ptr)
176+
}
177+
}

0 commit comments

Comments
 (0)