|
| 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