impls.rs 6.51 KB
Newer Older
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
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::StorageLayout;
use crate::{
    env::{
        AccountId,
        Hash,
    },
    storage2::traits::{
        ExtKeyPtr as _,
        KeyPtr,
        SpreadLayout,
    },
};
use ink_abi::layout2::{
    ArrayLayout,
    CellLayout,
    Discriminant,
    EnumLayout,
    FieldLayout,
    Layout,
    LayoutKey,
    StructLayout,
};
use ink_prelude::{
    boxed::Box,
    collections::BTreeMap,
    string::String,
    vec::Vec,
};
use ink_primitives::Key;
44
use scale_info::TypeInfo;
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

macro_rules! impl_storage_layout_for_primitives {
    ( $($name:ty),* $(,)? ) => {
        $(
            impl StorageLayout for $name {
                fn layout(key_ptr: &mut KeyPtr) -> Layout {
                    Layout::Cell(CellLayout::new::<$name>(LayoutKey::from(key_ptr.advance_by(1))))
                }
            }
        )*
    };
}
#[rustfmt::skip]
impl_storage_layout_for_primitives!(
    Key, Hash, AccountId, String,
    bool, char, (),
    u8, u16, u32, u64, u128,
    i8, i16, i32, i64, i128,
);

macro_rules! impl_storage_layout_for_arrays {
    ( $($len:literal),* $(,)? ) => {
        $(
            impl<T> StorageLayout for [T; $len]
            where
                T: StorageLayout + SpreadLayout,
            {
                fn layout(key_ptr: &mut KeyPtr) -> Layout {
                    let len: u32 = $len;
                    let elem_footprint = <T as SpreadLayout>::FOOTPRINT;
                    Layout::Array(ArrayLayout::new(
                        LayoutKey::from(key_ptr.next_for::<[T; $len]>()),
                        len,
                        elem_footprint,
                        <T as StorageLayout>::layout(&mut key_ptr.clone()),
                    ))
                }
            }
        )*
    };
}
#[rustfmt::skip]
impl_storage_layout_for_arrays!(
         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,
);

macro_rules! impl_layout_for_tuple {
    ( $($frag:ident),* $(,)? ) => {
        impl<$($frag),*> StorageLayout for ($($frag),* ,)
        where
            $(
                $frag: StorageLayout,
            )*
        {
            fn layout(key_ptr: &mut KeyPtr) -> Layout {
                Layout::Struct(
                    StructLayout::new(vec![
                        $(
                            FieldLayout::new(None, <$frag as StorageLayout>::layout(key_ptr)),
                        )*
                    ])
                )
            }
        }
    }
}
impl_layout_for_tuple!(A);
impl_layout_for_tuple!(A, B);
impl_layout_for_tuple!(A, B, C);
impl_layout_for_tuple!(A, B, C, D);
impl_layout_for_tuple!(A, B, C, D, E);
impl_layout_for_tuple!(A, B, C, D, E, F);
impl_layout_for_tuple!(A, B, C, D, E, F, G);
impl_layout_for_tuple!(A, B, C, D, E, F, G, H);
impl_layout_for_tuple!(A, B, C, D, E, F, G, H, I);
impl_layout_for_tuple!(A, B, C, D, E, F, G, H, I, J);

impl<T> StorageLayout for Box<T>
where
    T: StorageLayout,
{
    fn layout(key_ptr: &mut KeyPtr) -> Layout {
        <T as StorageLayout>::layout(key_ptr)
    }
}

impl<T> StorageLayout for Option<T>
where
    T: StorageLayout,
{
    fn layout(key_ptr: &mut KeyPtr) -> Layout {
        let dispatch_key = key_ptr.advance_by(1);
        Layout::Enum(EnumLayout::new(
141
            *dispatch_key,
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
            vec![
                (
                    Discriminant::from(0),
                    StructLayout::new(vec![FieldLayout::new(
                        None,
                        <T as StorageLayout>::layout(&mut key_ptr.clone()),
                    )]),
                ),
                (Discriminant::from(1), StructLayout::new(Vec::new())),
            ],
        ))
    }
}

impl<T, E> StorageLayout for Result<T, E>
where
    T: StorageLayout,
    E: StorageLayout,
{
    fn layout(key_ptr: &mut KeyPtr) -> Layout {
        let dispatch_key = key_ptr.advance_by(1);
        Layout::Enum(EnumLayout::new(
164
            *dispatch_key,
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
            vec![
                (
                    Discriminant::from(0),
                    StructLayout::new(vec![FieldLayout::new(
                        None,
                        <T as StorageLayout>::layout(&mut key_ptr.clone()),
                    )]),
                ),
                (
                    Discriminant::from(1),
                    StructLayout::new(vec![FieldLayout::new(
                        None,
                        <E as StorageLayout>::layout(&mut key_ptr.clone()),
                    )]),
                ),
            ],
        ))
    }
}

impl<T> StorageLayout for Vec<T>
where
187
    T: TypeInfo + 'static,
188
189
190
191
192
193
194
195
196
197
{
    fn layout(key_ptr: &mut KeyPtr) -> Layout {
        Layout::Cell(CellLayout::new::<Self>(LayoutKey::from(
            key_ptr.advance_by(1),
        )))
    }
}

impl<K, V> StorageLayout for BTreeMap<K, V>
where
198
199
    K: TypeInfo + 'static,
    V: TypeInfo + 'static,
200
201
202
203
204
205
206
207
208
209
{
    fn layout(key_ptr: &mut KeyPtr) -> Layout {
        Layout::Cell(CellLayout::new::<Self>(LayoutKey::from(
            key_ptr.advance_by(1),
        )))
    }
}

// impl<T> StorageLayout for BinaryHeap<T>
// where
210
//     T: TypeInfo + 'static,
211
212
213
214
215
216
217
218
// {
//     fn layout(key_ptr: &mut KeyPtr) -> Layout {
//         Layout::Cell(CellLayout::new::<Self>(LayoutKey::from(key_ptr.advance_by(1))))
//     }
// }

// impl<T> StorageLayout for BTreeSet<T>
// where
219
//     T: TypeInfo + 'static,
220
221
222
223
224
225
226
227
// {
//     fn layout(key_ptr: &mut KeyPtr) -> Layout {
//         Layout::Cell(CellLayout::new::<Self>(LayoutKey::from(key_ptr.advance_by(1))))
//     }
// }

// impl<T> StorageLayout for VecDeque<T>
// where
228
//     T: TypeInfo + 'static,
229
230
231
232
233
234
235
236
// {
//     fn layout(key_ptr: &mut KeyPtr) -> Layout {
//         Layout::Cell(CellLayout::new::<Self>(LayoutKey::from(key_ptr.advance_by(1))))
//     }
// }

// impl<T> StorageLayout for LinkedList<T>
// where
237
//     T: TypeInfo + 'static,
238
239
240
241
242
// {
//     fn layout(key_ptr: &mut KeyPtr) -> Layout {
//         Layout::Cell(CellLayout::new::<Self>(LayoutKey::from(key_ptr.advance_by(1))))
//     }
// }