storage.rs 4.01 KB
Newer Older
1
// Copyright 2018-2021 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// 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::Box as StorageBox;
16
use crate::{
17
18
19
20
21
22
23
24
25
26
27
28
29
    alloc::DynamicAllocation,
    traits::{
        forward_clear_packed,
        forward_pull_packed,
        forward_push_packed,
        KeyPtr,
        PackedLayout,
        SpreadLayout,
    },
};
use ink_prelude::vec::Vec;
use ink_primitives::Key;

30
31
#[cfg(feature = "std")]
const _: () = {
32
    use crate::traits::StorageLayout;
33
    use ink_metadata::layout::{
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        CellLayout,
        Layout,
        LayoutKey,
    };

    impl<T> StorageLayout for StorageBox<T>
    where
        T: SpreadLayout,
    {
        fn layout(key_ptr: &mut KeyPtr) -> Layout {
            Layout::Cell(CellLayout::new::<DynamicAllocation>(LayoutKey::from(
                key_ptr.advance_by(1),
            )))
        }
    }
Hero Bird's avatar
Hero Bird committed
49
50
51

    impl<T> scale_info::TypeInfo for StorageBox<T>
    where
52
        T: SpreadLayout + 'static,
Hero Bird's avatar
Hero Bird committed
53
    {
54
55
        type Identity = Self;

Hero Bird's avatar
Hero Bird committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        fn type_info() -> scale_info::Type {
            scale_info::Type::builder()
                .path(
                    scale_info::Path::from_segments(vec!["ink_storage", "alloc", "Box"])
                        .expect("encountered invalid Rust path"),
                )
                // Unfortunately we cannot encode the type parameters of the box since they
                // have to be `T: scale::Codec`. However, them not requiring to be encodable
                // is the purpose of the storage `Box<T>`.
                // Until we found a solution to this problem we cannot uncomment the below
                // line of code:
                //
                // .type_params(vec![scale_info::MetaType::new::<T>()])
                .composite(
                    scale_info::build::Fields::named()
71
                        .field_of::<DynamicAllocation>("allocation", "DynamicAllocation"),
Hero Bird's avatar
Hero Bird committed
72
73
74
                )
        }
    }
75
76
};

77
78
79
80
81
82
83
84
85
86
87
impl<T> SpreadLayout for StorageBox<T>
where
    T: SpreadLayout,
{
    const FOOTPRINT: u64 = 1;

    fn pull_spread(ptr: &mut KeyPtr) -> Self {
        forward_pull_packed::<Self>(ptr)
    }

    fn push_spread(&self, ptr: &mut KeyPtr) {
88
        forward_push_packed::<Self>(self, ptr)
89
90
91
    }

    fn clear_spread(&self, ptr: &mut KeyPtr) {
92
        forward_clear_packed::<Self>(self, ptr)
93
94
95
96
97
98
99
100
101
102
103
    }
}

impl<T> scale::Encode for StorageBox<T>
where
    T: SpreadLayout,
{
    fn size_hint(&self) -> usize {
        <DynamicAllocation as scale::Encode>::size_hint(&self.allocation)
    }

104
    fn encode_to<O: scale::Output + ?Sized>(&self, dest: &mut O) {
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
        <DynamicAllocation as scale::Encode>::encode_to(&self.allocation, dest)
    }

    fn encode(&self) -> Vec<u8> {
        <DynamicAllocation as scale::Encode>::encode(&self.allocation)
    }

    fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
        <DynamicAllocation as scale::Encode>::using_encoded(&self.allocation, f)
    }
}

impl<T> scale::Decode for StorageBox<T>
where
    T: SpreadLayout,
{
    fn decode<I: scale::Input>(value: &mut I) -> Result<Self, scale::Error> {
        Ok(StorageBox::lazy(
            <DynamicAllocation as scale::Decode>::decode(value)?,
        ))
    }
}

impl<T> PackedLayout for StorageBox<T>
where
    T: SpreadLayout,
{
    fn pull_packed(&mut self, _at: &Key) {}

    fn push_packed(&self, _at: &Key) {
        <T as SpreadLayout>::push_spread(Self::get(self), &mut KeyPtr::from(self.key()))
    }

    fn clear_packed(&self, _at: &Key) {
        <T as SpreadLayout>::clear_spread(Self::get(self), &mut KeyPtr::from(self.key()))
    }
}